diff --git a/day22.py b/day22.py new file mode 100644 index 0000000..a085022 --- /dev/null +++ b/day22.py @@ -0,0 +1,26 @@ +with open("res/inputs/day22.txt", "r") as f: + values = list(map(int, f.read().split("\n"))) + +calls = 0 + +def step(a, shift): + global calls + calls += 1 + s = (a >> -shift) if shift < 0 else (a << shift) + return (s ^ a) & 0xffffff + +def rand(value): + b = step(value, 6) + c = step(b, -5) + d = step(c, 11) + return d + + +total = 0 +for v in values: + for _ in range(2000): + v = rand(v) + total += v + +print(total) +print(calls) \ No newline at end of file diff --git a/progress.png b/progress.png index 540ecbc..a03f169 100644 Binary files a/progress.png and b/progress.png differ diff --git a/progress.yaml b/progress.yaml index ff71352..0ef3bad 100644 --- a/progress.yaml +++ b/progress.yaml @@ -39,4 +39,6 @@ 20: stars: 1 21: - stars: 0 \ No newline at end of file + stars: 0 +22: + stars: 1 \ No newline at end of file diff --git a/res/examples/day22.txt b/res/examples/day22.txt new file mode 100644 index 0000000..d800886 --- /dev/null +++ b/res/examples/day22.txt @@ -0,0 +1 @@ +123 \ No newline at end of file diff --git a/src/day22/puzzle1.typ b/src/day22/puzzle1.typ new file mode 100644 index 0000000..3778c72 --- /dev/null +++ b/src/day22/puzzle1.typ @@ -0,0 +1,61 @@ +#import "/src/utils.typ": * + +#let random(prev) = { + let step(a, shift) = { + let s = if shift < 0 { + a.bit-rshift(-shift) + } else { + a.bit-lshift(shift) + } + return s.bit-xor(a).bit-and(0xffffff) + } + let b = step(prev, 6) + let c = step(b, -5) + let d = step(c, 11) + return d +} + +#let solve(input, steps: 1) = { + let values = input.split("\n").map(int) + let total = 0 + for value in values { + for _ in range(steps) { + value = random(value) + } + total += value + } + return total +} + +#let examples = () +#let results = ( + 15887950, + 16495136, + 527345, + 704524, + 1553684, + 12683156, + 11100544, + 12249484, + 7753432, + 5908254 +) +#for (i, res) in results.enumerate() { + examples.push(( + result: res, + args: (steps: i + 1) + )) +} + +#show-puzzle( + 22, 1, + solve.with(steps: 2000), + example: examples, + only-example: true +) + +#show-result(18261820068) + +//B = ((A << 6) ^ A) & 0xffffff\ +//C = ((B >> 5) ^ B) & 0xffffff\ +//D = ((C << 11) ^ C) & 0xffffff \ No newline at end of file diff --git a/src/day22/puzzle2.typ b/src/day22/puzzle2.typ new file mode 100644 index 0000000..e69de29 diff --git a/src/main.pdf b/src/main.pdf index 808edb3..b943987 100644 Binary files a/src/main.pdf and b/src/main.pdf differ