-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday14.jl
47 lines (42 loc) · 1.27 KB
/
day14.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
using DataStructures: DefaultDict
function setup(seq)
counts = DefaultDict{String, Int}(0)
for i ∈ 1:(length(seq)-1)
counts[seq[i:i+1]] += 1
end
return counts, seq[1], seq[end]
end
function sim!(state, insertions, steps)
state = DefaultDict(0, state) # defensive copy since we are mutating
for _ ∈ 1:steps
for (pair, count) ∈ collect(pairs(state)) # collect() to make a copy
if haskey(insertions, pair)
state[pair[1] * insertions[pair]] += count
state[insertions[pair] * pair[2]] += count
state[pair] -= count
end
end
end
return state
end
function count(state, s, e)
counts = DefaultDict{Char, Int}(0)
counts[s] = 1
counts[e] = 1
for (pair, count) ∈ state
counts[pair[1]] += count
counts[pair[2]] += count
end
map!(x -> x /= 2, values(counts))
return counts
end
f = open("data/day14.txt")
state, s, e = setup(readline(f))
readline(f)
insertions = Dict(map(x -> split(x, " -> "), eachline(f)))
# Part 1 & 2 - What do you get if you take the quantity of the most common element and subtract the quantity of the least common element?
for steps ∈ [10, 40]
result = sim!(state, insertions, steps)
counts = count(result, s, e)
println("partX = ", maximum(values(counts)) - minimum(values(counts)))
end