-
Notifications
You must be signed in to change notification settings - Fork 0
/
day17.jl
48 lines (42 loc) · 992 Bytes
/
day17.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
48
module Day17
using AdventOfCode2017
function day17(input::String = readInput(joinpath(@__DIR__, "..", "data", "day17.txt")))
nsteps = parse(Int, input)
next = build_circular_list(nsteps, 2017)
p1 = next[next[findfirst(next .== 2018)]] - 1
return [p1, part2(nsteps)]
end
function build_circular_list(nsteps::Int, m::Int)
next = zeros(Int, m + 1)
next[1] = 1
p = 1
len = 1
for n = 1:m
for _ = 1:mod(nsteps, len)
p = next[p]
end
next[n+1] = next[p]
next[p] = n+1
p = next[p]
len += 1
end
return next
end
function part2(nsteps::Int)
# Do not build the circular list explicitely.
# Instead, only keep track of the values that are
# inserted after the 0.
cp = 0
cn = 0
l = 1
for n = 1:50_000_000
cp = mod(cp + nsteps, l)
if cp == 0
cn = n
end
l += 1
cp = mod(cp + 1, l)
end
return cn
end
end # module