@@ -7,12 +7,10 @@ import (
7
7
)
8
8
9
9
type d17puter struct {
10
- a , b , c int64
11
- program []int
12
- ip int64
13
- out []int64
14
- quine bool
15
- quineCnt int
10
+ a , b , c int64
11
+ program []int
12
+ ip int64
13
+ out []int64
16
14
}
17
15
18
16
func (p * d17puter ) load (input []string ) {
@@ -39,8 +37,6 @@ func (p *d17puter) load(input []string) {
39
37
p .c = n
40
38
}
41
39
}
42
- p .quine = true
43
- p .quineCnt = 0
44
40
}
45
41
46
42
func (p * d17puter ) combo (val int64 ) int64 {
@@ -60,9 +56,6 @@ func (p *d17puter) combo(val int64) int64 {
60
56
61
57
func (p * d17puter ) cycle () bool {
62
58
if p .ip >= int64 (len (p .program )) || p .ip + 1 >= int64 (len (p .program )) {
63
- if p .quineCnt < len (p .program ) {
64
- p .quine = false
65
- }
66
59
return false
67
60
}
68
61
op := int64 (p .program [p .ip ])
@@ -88,11 +81,6 @@ func (p *d17puter) cycle() bool {
88
81
case 5 : // out
89
82
val = p .combo (val )
90
83
p .out = append (p .out , val % 8 )
91
- if p .quine && len (p .out ) <= len (p .program ) && p .out [len (p .out )- 1 ] == int64 (p .program [len (p .out )- 1 ]) {
92
- p .quineCnt ++
93
- } else {
94
- p .quine = false
95
- }
96
84
case 6 : // bdv
97
85
val = p .combo (val )
98
86
p .b = int64 (float64 (p .a ) / math .Pow (2 , float64 (val )))
@@ -106,12 +94,9 @@ func (p *d17puter) cycle() bool {
106
94
return true
107
95
}
108
96
109
- func (p * d17puter ) run (quine bool ) bool {
97
+ func (p * d17puter ) run () bool {
110
98
for {
111
99
running := p .cycle ()
112
- if quine && ! p .quine {
113
- return false
114
- }
115
100
if ! running {
116
101
return true
117
102
}
@@ -123,9 +108,7 @@ func (p *d17puter) reset() {
123
108
p .b = 0
124
109
p .c = 0
125
110
p .ip = 0
126
- p .quine = true
127
111
p .out = nil
128
- p .quineCnt = 0
129
112
}
130
113
131
114
func (p * d17puter ) printOutput () string {
@@ -144,9 +127,42 @@ func (*methods) D17P1(input string) string {
144
127
145
128
p := & d17puter {}
146
129
p .load (lines )
147
- p .run (false )
130
+ p .run ()
148
131
149
132
return p .printOutput ()
150
133
}
151
134
152
- //TODO: D17P2
135
+ // 000 - 7
136
+ // 001 - 6
137
+ // 010 - 4
138
+ // 011 - 7
139
+ // 100 - 3
140
+ // 101 - 2
141
+ // 110 - 1
142
+ // 111 - 0
143
+ func (* methods ) D17P2 (input string ) string {
144
+ lines := strings .Split (strings .TrimSpace (input ), "\n " )
145
+ if len (lines ) != 5 {
146
+ return "invalid input (unexpected amount of lines)"
147
+ }
148
+
149
+ p := & d17puter {}
150
+ p .load (lines )
151
+
152
+ a := int64 (0b111 ) // this produces 0 in the end
153
+ for i := len (p .program ) - 2 ; i >= 0 ; i -- {
154
+ a <<= 3
155
+ d := p .program [i ]
156
+ for j := 0 ; j < 8 ; j ++ {
157
+ p .reset ()
158
+ p .a = a + int64 (j )
159
+ p .run ()
160
+ if int64 (d ) == p .out [0 ] {
161
+ a += int64 (j )
162
+ break
163
+ }
164
+ }
165
+ }
166
+
167
+ return strconv .FormatInt (a , 10 )
168
+ }
0 commit comments