-
Notifications
You must be signed in to change notification settings - Fork 0
/
d17.go
152 lines (137 loc) · 2.45 KB
/
d17.go
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
package main
import (
"math"
"strconv"
"strings"
)
type d17puter struct {
a, b, c int64
program []int
ip int64
out []int64
quine bool
quineCnt int
}
func (p *d17puter) load(input []string) {
for i, l := range input {
if i == 3 {
continue
}
_, l, _ = strings.Cut(l, ": ")
if i == 4 {
nums := strings.Split(l, ",")
for _, num := range nums {
n, _ := strconv.Atoi(num)
p.program = append(p.program, n)
}
continue
}
n, _ := strconv.ParseInt(l, 10, 64)
switch i {
case 0:
p.a = n
case 1:
p.b = n
case 2:
p.c = n
}
}
p.quine = true
p.quineCnt = 0
}
func (p *d17puter) combo(val int64) int64 {
if val < 4 {
return val
}
switch val {
case 4:
return p.a
case 5:
return p.b
case 6:
return p.c
}
panic("invalid program")
}
func (p *d17puter) cycle() bool {
if p.ip >= int64(len(p.program)) || p.ip+1 >= int64(len(p.program)) {
if p.quineCnt < len(p.program) {
p.quine = false
}
return false
}
op := int64(p.program[p.ip])
val := int64(p.program[p.ip+1])
switch op {
case 0: // adv
val = p.combo(val)
p.a = int64(float64(p.a) / math.Pow(2, float64(val)))
case 1: // bxl
p.b ^= val
case 2: // bst
val = p.combo(val)
p.b = val % 8
case 3: // jnz
if p.a == 0 {
break
}
p.ip = val
return true
case 4: // bxc
p.b ^= p.c
case 5: // out
val = p.combo(val)
p.out = append(p.out, val%8)
if p.quine && len(p.out) <= len(p.program) && p.out[len(p.out)-1] == int64(p.program[len(p.out)-1]) {
p.quineCnt++
} else {
p.quine = false
}
case 6: // bdv
val = p.combo(val)
p.b = int64(float64(p.a) / math.Pow(2, float64(val)))
case 7: // cdv
val = p.combo(val)
p.c = int64(float64(p.a) / math.Pow(2, float64(val)))
}
p.ip += 2
return true
}
func (p *d17puter) run(quine bool) bool {
for {
running := p.cycle()
if quine && !p.quine {
return false
}
if !running {
return true
}
}
}
func (p *d17puter) reset() {
p.a = 0
p.b = 0
p.c = 0
p.ip = 0
p.quine = true
p.out = nil
p.quineCnt = 0
}
func (p *d17puter) printOutput() string {
var outs []string
for _, o := range p.out {
outs = append(outs, strconv.FormatInt(o, 10))
}
return strings.Join(outs, ",")
}
func (*methods) D17P1(input string) string {
lines := strings.Split(strings.TrimSpace(input), "\n")
if len(lines) != 5 {
return "invalid input (unexpected amount of lines)"
}
p := &d17puter{}
p.load(lines)
p.run(false)
return p.printOutput()
}
//TODO: D17P2