From d7ed94345981b81fd3b57fb612e42ee16d210a38 Mon Sep 17 00:00:00 2001 From: Tim Kurvers Date: Sun, 22 Dec 2024 17:10:04 +0100 Subject: [PATCH] feat(js): add solutions for 2024 day 22 part 1 --- js/src/2024/22/index.js | 30 ++++++++++++++++++++++++++++++ puzzles/2024/22/examples.yaml | 7 +++++++ 2 files changed, 37 insertions(+) create mode 100644 js/src/2024/22/index.js create mode 100644 puzzles/2024/22/examples.yaml diff --git a/js/src/2024/22/index.js b/js/src/2024/22/index.js new file mode 100644 index 0000000..7ebebe2 --- /dev/null +++ b/js/src/2024/22/index.js @@ -0,0 +1,30 @@ +import { mod, solution } from '../../utils/index.js'; + +const parse = (input) => input.trim().split('\n').map(Number); + +const prune = (value) => mod(value, 16777216); + +const mix = (value, secret) => value ^ secret; + +const next = (value) => { + let result = prune(mix(value * 64, value)); + result = prune(mix((result / 32) | 0, result)); + result = prune(mix(result * 2048, result)); + return result; +}; + +export const partOne = solution((input) => { + const numbers = parse(input); + + let sum = 0; + for (const number of numbers) { + let result = number; + for (let i = 0; i < 2000; ++i) { + result = next(result); + } + sum += result; + } + return sum; +}); + +// TODO: Ain't nobody got time for part two ;) diff --git a/puzzles/2024/22/examples.yaml b/puzzles/2024/22/examples.yaml new file mode 100644 index 0000000..7e0dfd9 --- /dev/null +++ b/puzzles/2024/22/examples.yaml @@ -0,0 +1,7 @@ +part-one: + - input: | + 1 + 10 + 100 + 2024 + answer: 37327623