-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday7_2.nim
114 lines (108 loc) · 3.09 KB
/
day7_2.nim
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
import sequtils
import strutils
from algorithm import nextPermutation
let initialTape = readLine(stdin).split(",").map(parseInt)
type
Stop = object
pos, output: int
halt: bool
proc runAmp(tape: var seq[int], position: int, inputs: seq[int]): Stop =
var inputPos = 0
var pos = position
while true:
var codelen: int
let instr = tape[pos]
let opcode = instr mod 100
let p1mode = (instr div 100) mod 10
let p2mode = (instr div 1000) mod 10
# let p3mode = (instr div 10000) mod 10
case opcode:
# Add
of 1:
codelen = 4
let p1 = if p1mode == 0: tape[pos+1] else: pos+1
let p2 = if p2mode == 0: tape[pos+2] else: pos+2
tape[tape[pos+3]] = tape[p1] + tape[p2]
# Multiply
of 2:
codelen = 4
let p1 = if p1mode == 0: tape[pos+1] else: pos+1
let p2 = if p2mode == 0: tape[pos+2] else: pos+2
tape[tape[pos+3]] = tape[p1] * tape[p2]
# Input
of 3:
codelen = 2
let value = inputs[inputPos]
tape[tape[pos+1]] = value
inputPos += 1
# Output
of 4:
codelen = 2
let p1 = if p1mode == 0: tape[pos+1] else: pos+1
return Stop(pos: pos + codelen, output: tape[p1], halt: false)
# Jump if true
of 5:
codelen = 3
let p1 = if p1mode == 0: tape[pos+1] else: pos+1
let p2 = if p2mode == 0: tape[pos+2] else: pos+2
if tape[p1] > 0:
codelen = 0
pos = tape[p2]
# Jump if false
of 6:
codelen = 3
let p1 = if p1mode == 0: tape[pos+1] else: pos+1
let p2 = if p2mode == 0: tape[pos+2] else: pos+2
if tape[p1] == 0:
codelen = 0
pos = tape[p2]
# Less than
of 7:
codelen = 4
let p1 = if p1mode == 0: tape[pos+1] else: pos+1
let p2 = if p2mode == 0: tape[pos+2] else: pos+2
if tape[p1] < tape[p2]:
tape[tape[pos+3]] = 1
else:
tape[tape[pos+3]] = 0
# Equals
of 8:
codelen = 4
let p1 = if p1mode == 0: tape[pos+1] else: pos+1
let p2 = if p2mode == 0: tape[pos+2] else: pos+2
if tape[p1] == tape[p2]:
tape[tape[pos+3]] = 1
else:
tape[tape[pos+3]] = 0
# Halt
of 99:
return Stop(pos: pos, output: -1, halt: true)
else:
echo("You done goofed at pos " & $pos)
break
if codelen > 0:
pos += codelen
var phases = @[5, 6, 7, 8, 9]
var thrusters = 0
while phases.nextPermutation():
var mem = newSeqWith(phases.len, initialTape)
var stopPos = newSeqWith(5, 0)
var started = newSeqWith(5, false)
var haltCount = 0
var output = 0
while haltCount < 5:
for i in 0..<phases.len:
var inputs: seq[int]
if not started[i]:
inputs.add(phases[i])
started[i] = true
inputs.add(output)
let stop = runAmp(mem[i], stopPos[i], inputs)
if stop.halt:
haltCount += 1
else:
output = stop.output
stopPos[i] = stop.pos
if output > thrusters:
thrusters = output
echo("Max thrusters: " & $thrusters)