-
Notifications
You must be signed in to change notification settings - Fork 0
/
day23.jl
116 lines (101 loc) · 2.65 KB
/
day23.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
module Day23
using AdventOfCode2017
using Primes
function day23(input::String = readInput(joinpath(@__DIR__, "..", "data", "day23.txt")))
instructions = parse_input(input)
p1 = part1(instructions)
return [p1, part2()]
end
function parse_input(input::String)
instructions = []
for line in split(rstrip(input), "\n")
instr, a, b = split(line, " ")
aa = tryparse(Int, a)
if aa !== nothing
a = aa
else
a = a[1]
end
bb = tryparse(Int, b)
if bb !== nothing
b = bb
else
b = b[1]
end
if instr == "set"
push!(instructions, (set, (a, b)))
elseif instr == "sub"
push!(instructions, (sub, (a, b)))
elseif instr == "mul"
push!(instructions, (mul, (a, b)))
elseif instr == "jnz"
push!(instructions, (jnz, (a, b)))
end
end
return instructions
end
function set(registers::Dict{Char,Int}, pc::Int, x::Char, y::Int)
registers[x] = y
return pc + 1
end
function set(registers::Dict{Char,Int}, pc::Int, x::Char, y::Char)
registers[x] = registers[y]
return pc + 1
end
function sub(registers::Dict{Char,Int}, pc::Int, x::Char, y::Int)
registers[x] -= y
return pc + 1
end
function sub(registers::Dict{Char,Int}, pc::Int, x::Char, y::Char)
registers[x] -= registers[y]
return pc + 1
end
function mul(registers::Dict{Char,Int}, pc::Int, x::Char, y::Int)
registers[x] *= y
return pc + 1
end
function mul(registers::Dict{Char,Int}, pc::Int, x::Char, y::Char)
registers[x] *= registers[y]
return pc + 1
end
function jnz(registers::Dict{Char,Int}, pc::Int, x::Char, y::Int)
registers[x] != 0 && return pc + y
return pc + 1
end
function jnz(registers::Dict{Char,Int}, pc::Int, x::Int, y::Int)
x != 0 && return pc + y
return pc + 1
end
function part1(instructions)
p1 = 0
len = length(instructions)
pc = 1
registers = Dict{Char,Int}()
for c ∈ ('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h')
registers[c] = 0
end
while pc ∈ 1:len
if instructions[pc][1] == mul
p1 += 1
end
pc = instructions[pc][1](registers, pc, instructions[pc][2]...)
end
return p1
end
function part2()
# Note: Since this is a reverse-engeneering puzzle,
# you need to analyze the code from the input file,
# so this solution only works for my specific input!
nonprimes = 0
b = 106700
c = 123700
while true
if !isprime(b)
nonprimes += 1
end
b == c && break
b += 17
end
return nonprimes
end
end # module