發表文章

Day 3 using Kotlin - Advent of Code 2025

  https://adventofcode.com/2025/day/3   var max: Char var maxIndex: Int var startSub: String var trailSub: String var startMax: String var trailMax: String readInput ( "Day03" ). sumOf { line -> max = line. max () maxIndex = line. indexOf ( max ) startSub = line. take ( maxIndex ) trailSub = line. substring ( maxIndex + 1 ) startMax = if ( startSub. isEmpty ()) max.toString() else startSub. max ().toString() + max trailMax = if ( trailSub. isEmpty ()) max.toString() else max.toString() + trailSub. max () startMax. toInt (). coerceAtLeast ( trailMax. toInt ()) } . println ()      --- Part 2 ---   val digitCount = 12 var indexCharPairList: MutableList<Pair<Int, Char>> readInput ( "Day03" ). sumOf { line -> var remainCount = digitCount indexCharPairList = mutableListOf () fun findMax ( subLine: String, startIndex: Int ) { max = subLine. max () maxIndex = subLine. indexOf ...

Day 2 using Kotlin - Advent of Code 2025

https://adventofcode.com/2025/day/2 1. 找出不合法的產品識別碼並加總。 2. 不合法的識別碼為連續的數字出現兩次。例:55(5 出現兩次),6464(64 出現兩次)等。   SA、SD: 1.  產品識別碼偶數位數的要判斷,奇數的不用。 val start = 0 val end = 1 var strId: String var halfLength: Int // 1. 檔案只有一行 // 2. 每個範圍以逗號分隔 // 3. 結果為加總,故使用 sumOf readInput ( "Day02" ). first (). split ( "," ). sumOf { rangeStr -> // 如果長度不相等,要找出其中不合法的識別碼: 95-115 // 如果長度相等,而長度為偶數,也要進行判斷: 11-22 rangeStr. split ( "-" ). takeIf { strList -> strList[ start ]. length != strList[ end ]. length || strList[ start ]. length % 2 == 0 } ?. let { startEnd -> startEnd[ start ]. toLong ().rangeTo( startEnd[ end ]. toLong () ). filter { longId -> strId = longId.toString() halfLength = strId. length / 2 strId. length % 2 == 0 && strId. take ( halfLength ) == strId. substring ( halfLength ) } . sum () } ?: 0 } . println ()   --- P...

Day 1 using Kotlin - Advent of Code 2025

https://adventofcode.com/2025/day/1 1. 密碼鎖 環形 0~99 2. 往右轉數字變大,往左轉數字減小。 3. 當 99 往右轉 1,會到 0。當 0 往左轉,會到 99。 4. 密碼為每次轉動後停在 0 的次數。   SA、SD: 1. 當下位置會隨著每次轉動而更新。故宣告為 var。 2. R 為正,;L 為負:用此決定數字變大或變小(正負號)。 3. 當轉動時,會有機會超過 99 或 0,故以餘數來求得轉動後的指針位置。 4. 但當餘數小於 0 時,得將指針位置對應到其實際所代表的數字。 5. 應要求得每次轉動後停在 0 的次數,以 currPos == 0 來判斷,並搭配使用 count。    val totalClicks = 100 var currPos = 50 readInput ( "Day01" ). count { line -> currPos += ( if ( line. first () == 'R' ) 1 else - 1 ) * line. substring ( 1 ). toInt () currPos %= totalClicks if ( currPos < 0 ) { currPos += totalClicks } currPos == 0 } . println ()   --- Part 2 --- 1. 只要經過刻度 0,都算一次。 2. 密碼為 經過或停在刻度 0 的總次數。   SA、SD: 1. 先前位置 + 轉動距離 = 帶有正負號的新位置。 2. 當新位置為負時,表示有經過 0;次數加 1。  3. 當新位置為 0 時,次數加 1。 4. 把每次轉動經過或停在 0 的次數加總,使用 sumOf。    var prevPos = 50 readInput ( "Day01" ). sumOf { line -> currPos = prevPos + ( if ( line . first () == 'R' ) 1 else - 1 ) * ...

Unresolved reference: BuildConfig

為了簡化 Debug 與 Release 的參數設定,所以採用 BuildConfig。 一開始就遇到 Unresolved reference: BuildConfig。 嘗試了各種 Gemini 提出的解決方案如下,就是無法自動產出 BuildConfig。 1. Sync with Gradle 2. Invalid Cache 3. 刪除 .gradle 與 .idea 再重新開啟專案 4. 建立新專案 5. 重新安裝 Android Studio 最後找到 StackOverflow 以下這篇 https://stackoverflow.com/questions/76215947/buildconfig-could-not-resolve-after-upgrading-android-studio-to-flamingo-2022/76451943 在 app 的 build.gradle.kts 中加入 buildConfig = true buildFeatures { buildConfig = true } 網路上找了一下,都說 buildConfig 預設為 true;但為啥要加上 buildConfig = true 這行咧!?  於是請教 Gemini 的 Deep Research 我提出的問題是 "你知道 gradle plugin 的 buildFeatures 之 buildConfig 的預設值哪時被改為 false 嗎? " 跑了一陣子後,找出了在 AGP 8.0.0 時,buildConfig 的預設值就變為 false 了。 AGP 8.0.0 的連結如下 https://developer.android.com/build/releases/past-releases/agp-8-0-0-release-notes?hl=zh-tw 檢舉 新的預設值 先前的預設值 附註 android. defaults. buildfeatures. buildconfig false true 根據預設,AGP 8.0 不會產生 Build Config 。您需要在需要的專...