-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path12_2.jl
104 lines (87 loc) · 2.81 KB
/
12_2.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
#!/usr/bin/env julia
"""
https://adventofcode.com/2019/day/2
"""
function init_positions!(positions, noun=12, verb=2)
# Julia is 1-indexed so these are slightly different than the instructions
positions[2] = noun # Noun
positions[3] = verb # Verg
end
function resolve_opcode_1!(positions, window)
# Windows 2, 3, and 4 are memory address positions
total = positions[window[2]+1] + positions[window[3]+1]
positions[window[4]+1] = total
end
function resolve_opcode_2!(positions, window)
total = positions[window[2]+1] * positions[window[3]+1]
positions[window[4]+1] = total
end
function resolve_opcode_99(positions)
return positions[1]
end
function main1()
input_file = joinpath(pwd(), "files", "12_2_input.txt")
intcode = open(input_file, "r") do ifh
read(ifh, String)
end
#intcode = "1,9,10,3,2,3,11,0,99,30,40,50" # Test string
intcode = strip(intcode)
str_positions = split(intcode, ",")
positions = map(x -> parse(Int, x), str_positions)
init_positions!(positions)
final_position = length(positions) - 3
for i in 1:4:final_position
window = positions[i:i+3]
if window[1] == 1
resolve_opcode_1!(positions, window)
elseif window[1] == 2
resolve_opcode_2!(positions, window)
elseif window[1] == 99
println("Part 1 Answer:")
@show resolve_opcode_99(positions)
return
end
end
# In case we never have opcode 99
println("Part 1 Answer:")
@show positions[1]
end
function main2()
input_file = joinpath(pwd(), "files", "12_2_input.txt")
intcode = open(input_file, "r") do ifh
read(ifh, String)
end
intcode = strip(intcode)
str_positions = split(intcode, ",")
positions = map(x -> parse(Int, x), str_positions)
orig_positions = copy(positions)
DESIRED_OUTPUT = 19690720
final_position = length(positions) - 3
# Compute output over range of nouns and verbs until the combination
# that gives the desired output is come across
for noun in 0:99, verb in 0:99
init_positions!(positions, noun, verb)
output = zero(Int)
for i in 1:4:final_position
window = positions[i:i+3]
if window[1] == 1
resolve_opcode_1!(positions, window)
elseif window[1] == 2
resolve_opcode_2!(positions, window)
elseif window[1] == 99
output = resolve_opcode_99(positions)
break
end
end
if output == DESIRED_OUTPUT
println("Part 2 Answer:")
@show (100 * noun + verb)
exit()
end
positions = copy(orig_positions)
end
# In case we don't find it...
println("Didn't find the combination :-(")
end
main1()
main2()