Skip to content

Commit 739e5c8

Browse files
committed
[Day 22] Add solution
1 parent a0aa086 commit 739e5c8

File tree

5 files changed

+78
-3
lines changed

5 files changed

+78
-3
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ This Julia package contains my solutions for [Advent of Code 2024](https://adven
3030
| 18 | [:white_check_mark:](https://adventofcode.com/2024/day/18) |2.964 ms | 5.64 MiB | [:white_check_mark:](https://github.com/goggle/AdventOfCode2024.jl/blob/main/src/day18.jl) |
3131
| 19 | [:white_check_mark:](https://adventofcode.com/2024/day/19) | 59.678 ms | 3.41 MiB| [:white_check_mark:](https://github.com/goggle/AdventOfCode2024.jl/blob/main/src/day19.jl) |
3232
| 20 | [:white_check_mark:](https://adventofcode.com/2024/day/20) | 227.453 ms | 224.97 MiB| [:white_check_mark:](https://github.com/goggle/AdventOfCode2024.jl/blob/main/src/day20.jl) |
33+
| 22 | [:white_check_mark:](https://adventofcode.com/2024/day/22) | 241.724 ms | 78.80 MiB | [:white_check_mark:](https://github.com/goggle/AdventOfCode2024.jl/blob/main/src/day22.jl) |
3334
<!-- | 21 | [:white_check_mark:](https://adventofcode.com/2024/day/21) | 9.675 ms | 7.19 MiB | [:white_check_mark:](https://github.com/goggle/AdventOfCode2024.jl/blob/main/src/day21.jl) | -->
34-
<!-- | 22 | [:white_check_mark:](https://adventofcode.com/2024/day/22) | 790.712 ms | 631.26 MiB | [:white_check_mark:](https://github.com/goggle/AdventOfCode2024.jl/blob/main/src/day22.jl) | -->
3535
<!-- | 23 | [:white_check_mark:](https://adventofcode.com/2024/day/23) | 2.979 s | 9.69 MiB | [:white_check_mark:](https://github.com/goggle/AdventOfCode2024.jl/blob/main/src/day23.jl) | -->
3636
<!-- | 24 | [:white_check_mark:](https://adventofcode.com/2024/day/24) | 43.214 ms | 49.77 MiB | [:white_check_mark:](https://github.com/goggle/AdventOfCode2024.jl/blob/main/src/day24.jl) | -->
3737
<!-- | 25 | [:white_check_mark:](https://adventofcode.com/2024/day/25) | 69.476 ms | 62.03 MiB | [:white_check_mark:](https://github.com/goggle/AdventOfCode2024.jl/blob/main/src/day25.jl) | -->

data

Submodule data updated from 0774578 to 4340d98

src/AdventOfCode2024.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ module AdventOfCode2024
33
using BenchmarkTools
44
using Printf
55

6-
solvedDays = 1:21
6+
solvedDays = 1:22
77
# Include the source files:
88
for day in solvedDays
99
ds = @sprintf("%02d", day)

src/day22.jl

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
module Day22
2+
3+
using AdventOfCode2024
4+
using DataStructures
5+
6+
function day22(input::String = readInput(joinpath(@__DIR__, "..", "data", "day22.txt")))
7+
secrets = parse.(Int, split(rstrip(input), '\n'))
8+
p1 = 0
9+
10+
nbananas = Dict{Int,Int}()
11+
for i1 0:18
12+
for i2 0:18
13+
for i3 0:18
14+
for i4 0:18
15+
nbananas[(i1<<15, i2<<10, i3<<5, i4) |> sum] = 0
16+
end
17+
end
18+
end
19+
end
20+
21+
for secret secrets
22+
seen = Set{Int}()
23+
cb = CircularBuffer{Int8}(4)
24+
nsecret = secret
25+
price = nsecret % 10
26+
for _ 1:3
27+
nsecret = next_secret_number(nsecret)
28+
nprice = nsecret % 10
29+
push!(cb, nprice - price)
30+
price = nprice
31+
end
32+
for _ 4:2000
33+
nsecret = next_secret_number(nsecret)
34+
nprice = nsecret % 10
35+
push!(cb, nprice - price)
36+
price = nprice
37+
38+
diffseq = ((cb[1] + 9) << 15, (cb[2] + 9) << 10, (cb[3] + 9) << 5, cb[4] + 9) |> sum
39+
if diffseq seen
40+
push!(seen, diffseq)
41+
nbananas[diffseq] += price
42+
end
43+
end
44+
p1 += nsecret
45+
end
46+
47+
return [p1, maximum(values(nbananas))]
48+
end
49+
50+
function next_secret_number(secret::Int)
51+
step1 = mix(secret << 6, secret) |> prune
52+
step2 = mix(step1 >> 5, step1) |> prune
53+
return mix(step2 << 11, step2) |> prune
54+
end
55+
56+
mix(number::Int, secret::Int) = xor(number, secret)
57+
prune(secret::Int) = secret % 16777216
58+
59+
end # module

test/runtests.jl

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -326,4 +326,20 @@ end
326326

327327
@testset "Day 20" begin
328328
@testset AdventOfCode2024.Day20.day20() == [1411, 1010263]
329+
end
330+
331+
@testset "Day 22" begin
332+
sample = "1\n" *
333+
"10\n" *
334+
"100\n" *
335+
"2024\n"
336+
@test AdventOfCode2024.Day22.day22(sample) == [37327623, 24]
337+
338+
sample2 = "1\n" *
339+
"2\n" *
340+
"3\n" *
341+
"2024\n"
342+
@test AdventOfCode2024.Day22.day22(sample2) == [37990510, 23]
343+
344+
@test AdventOfCode2024.Day22.day22() == [13185239446, 1501]
329345
end

0 commit comments

Comments
 (0)