Skip to content

Commit 4429875

Browse files
author
Teiva Harsanyi
committed
First
1 parent 6411b2e commit 4429875

File tree

13 files changed

+670
-0
lines changed

13 files changed

+670
-0
lines changed

2018/day21-go/go.mod

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
module day21-go
2+
3+
go 1.18
4+
5+
require (
6+
github.com/davecgh/go-spew v1.1.1 // indirect
7+
github.com/pmezard/go-difflib v1.0.0 // indirect
8+
github.com/stretchr/objx v0.5.0 // indirect
9+
github.com/stretchr/testify v1.8.1 // indirect
10+
github.com/teivah/advent-of-code v0.0.6 // indirect
11+
gopkg.in/yaml.v3 v3.0.1 // indirect
12+
)

2018/day21-go/go.sum

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
2+
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
3+
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
4+
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
5+
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
6+
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
7+
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
8+
github.com/stretchr/objx v0.5.0 h1:1zr/of2m5FGMsad5YfcqgdqdWrIhu+EBEJRhR1U7z/c=
9+
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
10+
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
11+
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
12+
github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=
13+
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
14+
github.com/teivah/advent-of-code v0.0.6 h1:eVO1MZZSqQmsydjdRzJXSudIDBiRYNvWuZlBAQf5Qlc=
15+
github.com/teivah/advent-of-code v0.0.6/go.mod h1:65W3/TlV/tOdtAKvZHawTrSlBP4giocTo0jYJOp0F2k=
16+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
17+
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
18+
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
19+
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

2018/day21-go/input.txt

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#ip 2
2+
seti 123 0 4
3+
bani 4 456 4
4+
eqri 4 72 4
5+
addr 4 2 2
6+
seti 0 0 2
7+
seti 0 5 4
8+
bori 4 65536 5
9+
seti 1765573 9 4
10+
bani 5 255 1
11+
addr 4 1 4
12+
bani 4 16777215 4
13+
muli 4 65899 4
14+
bani 4 16777215 4
15+
gtir 256 5 1
16+
addr 1 2 2
17+
addi 2 1 2
18+
seti 27 0 2
19+
seti 0 8 1
20+
addi 1 1 3
21+
muli 3 256 3
22+
gtrr 3 5 3
23+
addr 3 2 2
24+
addi 2 1 2
25+
seti 25 1 2
26+
addi 1 1 1
27+
seti 17 7 2
28+
setr 1 4 5
29+
seti 7 6 2
30+
eqrr 4 0 1
31+
addr 1 2 2
32+
seti 5 2 2

2018/day21-go/main.go

Lines changed: 272 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,272 @@
1+
package main
2+
3+
import (
4+
"bufio"
5+
"io"
6+
7+
lib "github.com/teivah/advent-of-code"
8+
)
9+
10+
func fs1(input io.Reader) int {
11+
scanner := bufio.NewScanner(input)
12+
scanner.Scan()
13+
header := scanner.Text()
14+
ip := lib.StringToInt(header[4:])
15+
var instructions []apply
16+
for scanner.Scan() {
17+
line := scanner.Text()
18+
del := lib.NewDelimiter(line, " ")
19+
a := del.GetInt(1)
20+
b := del.GetInt(2)
21+
c := del.GetInt(3)
22+
var instruction apply
23+
switch del.GetString(0) {
24+
case "addr":
25+
instruction = addr(a, b, c)
26+
case "addi":
27+
instruction = addi(a, b, c)
28+
case "mulr":
29+
instruction = mulr(a, b, c)
30+
case "muli":
31+
instruction = muli(a, b, c)
32+
case "banr":
33+
instruction = banr(a, b, c)
34+
case "bani":
35+
instruction = bani(a, b, c)
36+
case "borr":
37+
instruction = borr(a, b, c)
38+
case "bori":
39+
instruction = bori(a, b, c)
40+
case "setr":
41+
instruction = setr(a, b, c)
42+
case "seti":
43+
instruction = seti(a, b, c)
44+
case "gtir":
45+
instruction = gtir(a, b, c)
46+
case "gtri":
47+
instruction = gtri(a, b, c)
48+
case "gtrr":
49+
instruction = gtrr(a, b, c)
50+
case "eqir":
51+
instruction = eqir(a, b, c)
52+
case "eqri":
53+
instruction = eqri(a, b, c)
54+
case "eqrr":
55+
instruction = eqrr(a, b, c)
56+
default:
57+
panic(line)
58+
}
59+
instructions = append(instructions, instruction)
60+
}
61+
62+
reg := make([]int, 6)
63+
reg[0] = 0
64+
for {
65+
if reg[ip] == 28 {
66+
return reg[4]
67+
}
68+
instructions[reg[ip]](reg)
69+
if reg[ip]+1 >= len(instructions) {
70+
break
71+
}
72+
reg[ip]++
73+
}
74+
75+
return 0
76+
}
77+
78+
type apply func(reg []int)
79+
80+
func addr(a, b, c int) apply {
81+
return func(reg []int) {
82+
reg[c] = reg[a] + reg[b]
83+
}
84+
}
85+
86+
func addi(a, b, c int) apply {
87+
return func(reg []int) {
88+
reg[c] = reg[a] + b
89+
}
90+
}
91+
92+
func mulr(a, b, c int) apply {
93+
return func(reg []int) {
94+
reg[c] = reg[a] * reg[b]
95+
}
96+
}
97+
98+
func muli(a, b, c int) apply {
99+
return func(reg []int) {
100+
reg[c] = reg[a] * b
101+
}
102+
}
103+
104+
func banr(a, b, c int) apply {
105+
return func(reg []int) {
106+
reg[c] = reg[a] & reg[b]
107+
}
108+
}
109+
110+
func bani(a, b, c int) apply {
111+
return func(reg []int) {
112+
reg[c] = reg[a] & b
113+
}
114+
}
115+
116+
func borr(a, b, c int) apply {
117+
return func(reg []int) {
118+
reg[c] = reg[a] | reg[b]
119+
}
120+
}
121+
122+
func bori(a, b, c int) apply {
123+
return func(reg []int) {
124+
reg[c] = reg[a] | b
125+
}
126+
}
127+
128+
func setr(a, _, c int) apply {
129+
return func(reg []int) {
130+
reg[c] = reg[a]
131+
}
132+
}
133+
134+
func seti(a, _, c int) apply {
135+
return func(reg []int) {
136+
reg[c] = a
137+
}
138+
}
139+
140+
func gtir(a, b, c int) apply {
141+
return func(reg []int) {
142+
if a > reg[b] {
143+
reg[c] = 1
144+
} else {
145+
reg[c] = 0
146+
}
147+
}
148+
}
149+
150+
func gtri(a, b, c int) apply {
151+
return func(reg []int) {
152+
if reg[a] > b {
153+
reg[c] = 1
154+
} else {
155+
reg[c] = 0
156+
}
157+
}
158+
}
159+
160+
func gtrr(a, b, c int) apply {
161+
return func(reg []int) {
162+
if reg[a] > reg[b] {
163+
reg[c] = 1
164+
} else {
165+
reg[c] = 0
166+
}
167+
}
168+
}
169+
170+
func eqir(a, b, c int) apply {
171+
return func(reg []int) {
172+
if a == reg[b] {
173+
reg[c] = 1
174+
} else {
175+
reg[c] = 0
176+
}
177+
}
178+
}
179+
180+
func eqri(a, b, c int) apply {
181+
return func(reg []int) {
182+
if reg[a] == b {
183+
reg[c] = 1
184+
} else {
185+
reg[c] = 0
186+
}
187+
}
188+
}
189+
190+
func eqrr(a, b, c int) apply {
191+
return func(reg []int) {
192+
if reg[a] == reg[b] {
193+
reg[c] = 1
194+
} else {
195+
reg[c] = 0
196+
}
197+
}
198+
}
199+
200+
func fs2(input io.Reader) int {
201+
scanner := bufio.NewScanner(input)
202+
scanner.Scan()
203+
header := scanner.Text()
204+
ip := lib.StringToInt(header[4:])
205+
var instructions []apply
206+
for scanner.Scan() {
207+
line := scanner.Text()
208+
del := lib.NewDelimiter(line, " ")
209+
a := del.GetInt(1)
210+
b := del.GetInt(2)
211+
c := del.GetInt(3)
212+
var instruction apply
213+
switch del.GetString(0) {
214+
case "addr":
215+
instruction = addr(a, b, c)
216+
case "addi":
217+
instruction = addi(a, b, c)
218+
case "mulr":
219+
instruction = mulr(a, b, c)
220+
case "muli":
221+
instruction = muli(a, b, c)
222+
case "banr":
223+
instruction = banr(a, b, c)
224+
case "bani":
225+
instruction = bani(a, b, c)
226+
case "borr":
227+
instruction = borr(a, b, c)
228+
case "bori":
229+
instruction = bori(a, b, c)
230+
case "setr":
231+
instruction = setr(a, b, c)
232+
case "seti":
233+
instruction = seti(a, b, c)
234+
case "gtir":
235+
instruction = gtir(a, b, c)
236+
case "gtri":
237+
instruction = gtri(a, b, c)
238+
case "gtrr":
239+
instruction = gtrr(a, b, c)
240+
case "eqir":
241+
instruction = eqir(a, b, c)
242+
case "eqri":
243+
instruction = eqri(a, b, c)
244+
case "eqrr":
245+
instruction = eqrr(a, b, c)
246+
default:
247+
panic(line)
248+
}
249+
instructions = append(instructions, instruction)
250+
}
251+
252+
reg := make([]int, 6)
253+
reg[0] = 0
254+
set := make(map[int]struct{})
255+
latest := 0
256+
for {
257+
if reg[ip] == 28 {
258+
if _, exists := set[reg[4]]; exists {
259+
return latest
260+
}
261+
set[reg[4]] = struct{}{}
262+
latest = reg[4]
263+
}
264+
instructions[reg[ip]](reg)
265+
if reg[ip]+1 >= len(instructions) {
266+
break
267+
}
268+
reg[ip]++
269+
}
270+
271+
return 0
272+
}

2018/day21-go/main_test.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package main
2+
3+
import (
4+
"os"
5+
"testing"
6+
7+
"github.com/stretchr/testify/assert"
8+
"github.com/stretchr/testify/require"
9+
)
10+
11+
func TestFs1Input(t *testing.T) {
12+
f, err := os.Open("input.txt")
13+
require.NoError(t, err)
14+
assert.Equal(t, 12213578, fs1(f))
15+
}
16+
17+
func TestFs2Input(t *testing.T) {
18+
f, err := os.Open("input.txt")
19+
require.NoError(t, err)
20+
assert.Equal(t, 12213578, fs2(f))
21+
}

2018/day21-go/test.txt

Whitespace-only changes.

2018/day22-go/go.mod

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
module day22-go
2+
3+
go 1.18
4+
5+
require (
6+
github.com/davecgh/go-spew v1.1.1 // indirect
7+
github.com/pmezard/go-difflib v1.0.0 // indirect
8+
github.com/stretchr/objx v0.5.0 // indirect
9+
github.com/stretchr/testify v1.8.1 // indirect
10+
github.com/teivah/advent-of-code v0.0.6 // indirect
11+
gopkg.in/yaml.v3 v3.0.1 // indirect
12+
)

2018/day22-go/go.sum

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
2+
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
3+
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
4+
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
5+
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
6+
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
7+
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
8+
github.com/stretchr/objx v0.5.0 h1:1zr/of2m5FGMsad5YfcqgdqdWrIhu+EBEJRhR1U7z/c=
9+
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
10+
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
11+
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
12+
github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=
13+
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
14+
github.com/teivah/advent-of-code v0.0.6 h1:eVO1MZZSqQmsydjdRzJXSudIDBiRYNvWuZlBAQf5Qlc=
15+
github.com/teivah/advent-of-code v0.0.6/go.mod h1:65W3/TlV/tOdtAKvZHawTrSlBP4giocTo0jYJOp0F2k=
16+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
17+
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
18+
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
19+
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

2018/day22-go/input.txt

Whitespace-only changes.

0 commit comments

Comments
 (0)