diff --git a/2024/README.md b/2024/README.md index db6be96..95d8fee 100644 --- a/2024/README.md +++ b/2024/README.md @@ -19,12 +19,12 @@ | [13](./days/day-13) | ![gold-star](../assets/star-gold.svg) ![gold-star](../assets/star-gold.svg) | 1.27ms \| 0.15ms | [Open on AoC](https://adventofcode.com/2024/day/13) | | [14](./days/day-14) | ![gold-star](../assets/star-gold.svg) ![gold-star](../assets/star-gold.svg) | 0.19ms \| 645.41ms | [Open on AoC](https://adventofcode.com/2024/day/14) | | [16](./days/day-16) | ![gold-star](../assets/star-gold.svg) ![gold-star](../assets/star-gold.svg) | 10.97ms \| 8.328s | [Open on AoC](https://adventofcode.com/2024/day/16) | -| [17](./days/day-17) | ![gold-star](../assets/star-gold.svg) ![gold-star](../assets/star-gold.svg) | 6.09ms \| 2.35ms | [Open on AoC](https://adventofcode.com/2024/day/17) | +| [17](./days/day-17) | ![gold-star](../assets/star-gold.svg) ![gold-star](../assets/star-gold.svg) | 11.14ms \| 5.96ms | [Open on AoC](https://adventofcode.com/2024/day/17) | > Autogenerated using [Custom GitHub Action Workflow](https://github.com/letelete/advent-of-code/blob/496913f895327f7755c5f03117730239d2b912eb/.github/workflows/update-year-readme.yml). > Made by [Bruno Kawka](https://kawka.me). -> Last update at Tue Dec 17 11:05:30 UTC 2024 +> Last update at Tue Dec 17 11:09:32 UTC 2024 ## AoC Bash Utility diff --git a/2024/days/day-17/README.md b/2024/days/day-17/README.md index e816bbe..e3ba30d 100644 --- a/2024/days/day-17/README.md +++ b/2024/days/day-17/README.md @@ -8,7 +8,7 @@ Memory: 18.00 GB ## Answer -| part | time (~) | μs | -| ---- | -------- | ------------------ | -| 1 | 6.09ms | 6.088292000000003 | -| 2 | 2.35ms | 2.3469580000000008 | +| part | time (~) | μs | +| ---- | -------- | ----------------- | +| 1 | 11.50ms | 11.49562499999999 | +| 2 | 5.88ms | 5.879374999999996 | diff --git a/2024/days/day-17/main.js b/2024/days/day-17/main.js index 95d562b..d75aa64 100644 --- a/2024/days/day-17/main.js +++ b/2024/days/day-17/main.js @@ -80,32 +80,27 @@ function Computer({ reg: { a, b, c }, verbose }) { } function findMinAToOutputSelf({ reg: { b, c }, program }) { - const cache = new Map(); - - const lookup = (lowerBound, ptr) => { - const hash = `${lowerBound},${ptr}`; - if (cache.has(hash)) { - return cache.get(hash); - } + // If a1 >= a0 * 8, cmp.run with a=a1 retains the last digit of cmp.out for cmp.run with a=a0. + const lowerBound = (a) => 8 * a; + const upperBound = (a) => 8 * (a + 1) - 1; + const lookup = (a, ptr) => { if (ptr < 0) { - return lowerBound; + return a; } - for (let a = lowerBound * 8; a < (lowerBound + 1) * 8; ++a) { - const cmp = Computer({ reg: { a, b, c } }); + for (let _a = lowerBound(a); _a <= upperBound(a); ++_a) { + const cmp = Computer({ reg: { a: _a, b, c } }); cmp.run(program); if (cmp.state.out[0] === program[ptr]) { - const match = lookup(a, ptr - 1); - if (match >= 0) { - cache.set(hash, match); - return match; + const _alookup = lookup(_a, ptr - 1); + if (_alookup >= 0) { + return _alookup; } } } - cache.set(hash, -1); return -1; };