-
Notifications
You must be signed in to change notification settings - Fork 0
/
day18.jl
168 lines (149 loc) · 5.07 KB
/
day18.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
module Day18
using AdventOfCode2017
using DataStructures
function day18(input::String = readInput(joinpath(@__DIR__, "..", "data", "day18.txt")))
instructions = parse_input(input)
p1 = run_part1(instructions)
p2 = run_part2(instructions)
return [p1, p2]
end
function parse_input(input)
lines = split(rstrip(input), "\n")
instructions = []
for line ∈ lines
elems = split(line)
if elems[1] == "snd"
push!(instructions, (snd, (elems[2][1], '0')))
elseif elems[1] == "set"
arg = tryparse(Int, elems[3])
if arg === nothing
push!(instructions, (set, (elems[2][1], elems[3][1])))
else
push!(instructions, (set, (elems[2][1], arg)))
end
elseif elems[1] == "add"
arg = tryparse(Int, elems[3])
if arg === nothing
push!(instructions, (add, (elems[2][1], elems[3][1])))
else
push!(instructions, (add, (elems[2][1], arg)))
end
elseif elems[1] == "mul"
arg = tryparse(Int, elems[3])
if arg === nothing
push!(instructions, (mul, (elems[2][1], elems[3][1])))
else
push!(instructions, (mul, (elems[2][1], arg)))
end
elseif elems[1] == "mod"
arg = tryparse(Int, elems[3])
if arg === nothing
push!(instructions, (mod, (elems[2][1], elems[3][1])))
else
push!(instructions, (mod, (elems[2][1], arg)))
end
elseif elems[1] == "rcv"
push!(instructions, (rcv, (elems[2][1], '0')))
elseif elems[1] == "jgz"
arg1 = tryparse(Int, elems[2])
arg2 = tryparse(Int, elems[3])
if arg1 === nothing
arg1 = elems[2][1]
end
if arg2 === nothing
arg2 = elems[3][1]
end
push!(instructions, (jgz, (arg1, arg2)))
end
end
return instructions
end
function snd(registers::DefaultDict{Char,Int}, lastsound::Vector{Int}, pc::Int, x::Char, ::Char)
push!(lastsound, registers[x])
return pc + 1
end
function set(registers::DefaultDict{Char,Int}, lastsound::Vector{Int}, pc::Int, x::Char, y::Int)
registers[x] = y
return pc + 1
end
function set(registers::DefaultDict{Char,Int}, lastsound::Vector{Int}, pc::Int, x::Char, y::Char)
registers[x] = registers[y]
return pc + 1
end
function add(registers::DefaultDict{Char,Int}, lastsound::Vector{Int}, pc::Int, x::Char, y::Int)
registers[x] += y
return pc + 1
end
function add(registers::DefaultDict{Char,Int}, lastsound::Vector{Int}, pc::Int, x::Char, y::Char)
registers[x] += registers[y]
return pc + 1
end
function mul(registers::DefaultDict{Char,Int}, lastsound::Vector{Int}, pc::Int, x::Char, y::Int)
registers[x] *= y
return pc + 1
end
function mul(registers::DefaultDict{Char,Int}, lastsound::Vector{Int}, pc::Int, x::Char, y::Char)
registers[x] *= registers[y]
return pc + 1
end
function mod(registers::DefaultDict{Char,Int}, lastsound::Vector{Int}, pc::Int, x::Char, y::Int)
registers[x] %= y
return pc + 1
end
function mod(registers::DefaultDict{Char,Int}, lastsound::Vector{Int}, pc::Int, x::Char, y::Char)
registers[x] %= registers[y]
return pc + 1
end
function rcv(registers::DefaultDict{Char,Int}, lastsound::Vector{Int}, pc::Int, x::Char, ::Char)
registers[x] != 0 && return -1
return pc + 1
end
function jgz(registers::DefaultDict{Char,Int}, lastsound::Vector{Int}, pc::Int, x::Char, y::Int)
registers[x] > 0 && return pc + y
return pc + 1
end
function jgz(registers::DefaultDict{Char,Int}, lastsound::Vector{Int}, pc::Int, x::Char, y::Char)
registers[x] > 0 && return pc + registers[y]
return pc + 1
end
function jgz(registers::DefaultDict{Char,Int}, lastsound::Vector{Int}, pc::Int, x::Int, y::Int)
x > 0 && return pc + y
return pc + 1
end
function run_part1(instructions)
registers = DefaultDict{Char,Int}(0)
lastsound = Vector{Int}()
pc = 1
while pc ∈ 1:length(instructions)
pc = instructions[pc][1](registers, lastsound, pc, instructions[pc][2]...)
end
return lastsound[end]
end
function run_part2(instructions)
p2 = 0
registers = (DefaultDict{Char,Int}(0), DefaultDict{Char,Int}(0))
registers[1]['p'] = 0
registers[2]['p'] = 1
lastsound = (Vector{Int}(), Vector{Int}())
pc = [1, 1]
while true
for i ∈ 1:2
while instructions[pc[i]][1] != rcv
if i == 2 && instructions[pc[i]][1] == snd
p2 += 1
end
pc[i] = instructions[pc[i]][1](registers[i], lastsound[i], pc[i], instructions[pc[i]][2]...)
end
end
if all(a -> isempty(a), lastsound)
return p2
end
for (i, r) ∈ zip(1:2, 2:-1:1)
if !isempty(lastsound[r])
registers[i][instructions[pc[i]][2][1]] = popfirst!(lastsound[r])
pc[i] += 1
end
end
end
end
end # module