Skip to content

Commit

Permalink
Add day 22 of year 2024 πŸ§‘β€πŸŽ„
Browse files Browse the repository at this point in the history
  • Loading branch information
letelete committed Dec 26, 2024
1 parent 315baaa commit dc6dad7
Show file tree
Hide file tree
Showing 5 changed files with 111 additions and 1 deletion.
3 changes: 2 additions & 1 deletion 2024/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,15 @@
| [18](./days/day-18) | ![gold-star](../assets/star-gold.svg) ![gold-star](../assets/star-gold.svg) | 7.46ms \| 7.82ms | [Open on AoC](https://adventofcode.com/2024/day/18) |
| [19](./days/day-19) | ![gold-star](../assets/star-gold.svg) ![gold-star](../assets/star-gold.svg) | 74.96ms \| 51.48ms | [Open on AoC](https://adventofcode.com/2024/day/19) |
| [20](./days/day-20) | ![gold-star](../assets/star-gold.svg) ![gold-star](../assets/star-gold.svg) | 103.39ms \| 402.32ms | [Open on AoC](https://adventofcode.com/2024/day/20) |
| [22](./days/day-22) | ![gold-star](../assets/star-gold.svg) ![gold-star](../assets/star-gold.svg) | 8.46ms \| 692.41ms | [Open on AoC](https://adventofcode.com/2024/day/22) |
| [23](./days/day-23) | ![gold-star](../assets/star-gold.svg) ![gold-star](../assets/star-gold.svg) | 22.48ms \| 10.45ms | [Open on AoC](https://adventofcode.com/2024/day/23) |
| [24](./days/day-24) | ![gold-star](../assets/star-gold.svg) ![silver-star](../assets/star-silver.svg) | 29.45ms | [Open on AoC](https://adventofcode.com/2024/day/24) |
| [25](./days/day-25) | ![gold-star](../assets/star-gold.svg) ![silver-star](../assets/star-silver.svg) | 2.01ms | [Open on AoC](https://adventofcode.com/2024/day/25) |

> 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 Wed Dec 25 08:53:03 UTC 2024
> Last update at Thu Dec 26 00:33:25 UTC 2024
## AoC Bash Utility

Expand Down
28 changes: 28 additions & 0 deletions 2024/days/day-22/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Benchmark

```
Platform: darwin arm64
CPU: Apple M3 Pro 11 Cores
Memory: 18.00 GB
```

## Sample 1

| part | time (~) | ΞΌs |
| ---- | -------- | ------------------- |
| 1 | 0.49ms | 0.49287499999999973 |
| 2 | 4.43ms | 4.432334000000001 |

## Sample 2

| part | time (~) | ΞΌs |
| ---- | -------- | -------------------- |
| 1 | 0.02ms | 0.024167000000002048 |
| 2 | 3.28ms | 3.280583 |

## Answer

| part | time (~) | ΞΌs |
| ---- | -------- | ----------------- |
| 1 | 8.46ms | 8.462292000000001 |
| 2 | 692.41ms | 692.41475 |
4 changes: 4 additions & 0 deletions 2024/days/day-22/in.sample.1.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
1
10
100
2024
4 changes: 4 additions & 0 deletions 2024/days/day-22/in.sample.2.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
1
2
3
2024
73 changes: 73 additions & 0 deletions 2024/days/day-22/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
function parse(source) {
return source.trim().split('\n').map(Number);
}

const N = 2000;

function gen(seed) {
const offset = 16777215;
seed ^= (seed << 6) & offset;
seed ^= (seed >> 5) & offset;
seed ^= (seed << 11) & offset;
return seed;
}

function genNth(seed, n) {
while (n--) {
seed = gen(seed);
}
return seed;
}

const next = (seed) => {
const newSeed = gen(seed);
return {
seed: newSeed,
price: newSeed % 10,
change: (newSeed % 10) - (seed % 10),
};
};

function genSequences(seed, N) {
const m = new Map();

const window = new Array(4).fill(null);
for (let i = 0; i < window.length; ++i) {
window[i] = next(window[i - 1]?.seed ?? seed);
}

for (let i = 4; i < N - 1; ++i) {
const hash = window.map(({ change }) => change).join(',');
const prev = window.at(-1);
m.set(hash, Math.max(m.get(hash) ?? 0, prev.price));
window.push(next(prev.seed));
window.shift();
}

return m;
}

function getMaxBananas(seeds, N) {
const m = new Map();
let max = 0;
seeds.forEach((seed) =>
genSequences(seed, N)
.entries()
.forEach(([hash, value]) => {
const sum = (m.get(hash) ?? 0) + value;
max = Math.max(sum, max);
m.set(hash, sum);
})
);
return max;
}

function part1(data) {
return Number(data.reduce((sum, seed) => sum + genNth(seed, N), 0));
}

function part2(data) {
return getMaxBananas(data, N);
}

module.exports = { parse, part1, part2 };

0 comments on commit dc6dad7

Please sign in to comment.