Skip to content

Commit 9f46278

Browse files
author
Teiva Harsanyi
committed
First
1 parent f6b5b01 commit 9f46278

File tree

7 files changed

+233
-2
lines changed

7 files changed

+233
-2
lines changed

2019/day7-go/main.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -213,8 +213,6 @@ func (s *State) exit(ctx Context) {
213213
s.over = true
214214
}
215215

216-
// Provide each amplifier its phase setting at its first input instruction?
217-
218216
func fs2(reader io.Reader) int {
219217
s := lib.ReaderToString(reader)
220218
codes := lib.StringsToInts(strings.Split(s, ","))

2019/day9-go/go.mod

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
module day9-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.9 // indirect
11+
gopkg.in/yaml.v3 v3.0.1 // indirect
12+
)

2019/day9-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.9 h1:SkdQHg5Vaej1m5EYzas6LlPmSrrw2zKN1v1J6eU9PWM=
15+
github.com/teivah/advent-of-code v0.0.9/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=

2019/day9-go/input.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
1102,34463338,34463338,63,1007,63,34463338,63,1005,63,53,1101,3,0,1000,109,988,209,12,9,1000,209,6,209,3,203,0,1008,1000,1,63,1005,63,65,1008,1000,2,63,1005,63,904,1008,1000,0,63,1005,63,58,4,25,104,0,99,4,0,104,0,99,4,17,104,0,99,0,0,1101,34,0,1013,1101,20,0,1012,1101,536,0,1023,1101,0,23,1006,1102,1,543,1022,1102,1,27,1003,1102,25,1,1014,1102,1,29,1009,1101,0,686,1025,1101,0,30,1004,1102,1,28,1017,1102,1,35,1016,1101,765,0,1028,1102,1,33,1002,1102,1,26,1000,1102,1,822,1027,1102,1,21,1001,1102,1,1,1021,1101,31,0,1007,1101,0,39,1010,1102,36,1,1019,1101,0,32,1015,1101,0,38,1018,1101,0,24,1005,1101,22,0,1011,1101,756,0,1029,1102,1,0,1020,1102,829,1,1026,1102,1,37,1008,1101,0,695,1024,109,19,1205,2,195,4,187,1105,1,199,1001,64,1,64,1002,64,2,64,109,7,1205,-6,215,1001,64,1,64,1105,1,217,4,205,1002,64,2,64,109,-16,21108,40,42,5,1005,1015,233,1106,0,239,4,223,1001,64,1,64,1002,64,2,64,109,-13,2102,1,5,63,1008,63,33,63,1005,63,261,4,245,1105,1,265,1001,64,1,64,1002,64,2,64,109,29,21101,41,0,-9,1008,1017,41,63,1005,63,291,4,271,1001,64,1,64,1105,1,291,1002,64,2,64,109,-22,2107,27,-4,63,1005,63,307,1105,1,313,4,297,1001,64,1,64,1002,64,2,64,109,7,1207,-4,30,63,1005,63,333,1001,64,1,64,1106,0,335,4,319,1002,64,2,64,109,1,21108,42,42,6,1005,1018,353,4,341,1105,1,357,1001,64,1,64,1002,64,2,64,109,14,21101,43,0,-7,1008,1019,41,63,1005,63,377,1106,0,383,4,363,1001,64,1,64,1002,64,2,64,109,-8,21102,44,1,-1,1008,1017,47,63,1005,63,407,1001,64,1,64,1105,1,409,4,389,1002,64,2,64,109,-15,2101,0,2,63,1008,63,25,63,1005,63,433,1001,64,1,64,1105,1,435,4,415,1002,64,2,64,109,7,1201,-8,0,63,1008,63,30,63,1005,63,455,1105,1,461,4,441,1001,64,1,64,1002,64,2,64,109,-12,2108,37,10,63,1005,63,483,4,467,1001,64,1,64,1106,0,483,1002,64,2,64,109,13,21107,45,44,0,1005,1011,499,1105,1,505,4,489,1001,64,1,64,1002,64,2,64,109,-2,2107,20,-8,63,1005,63,523,4,511,1106,0,527,1001,64,1,64,1002,64,2,64,109,20,2105,1,-6,1001,64,1,64,1105,1,545,4,533,1002,64,2,64,109,-28,2102,1,1,63,1008,63,30,63,1005,63,565,1105,1,571,4,551,1001,64,1,64,1002,64,2,64,109,20,1206,0,583,1105,1,589,4,577,1001,64,1,64,1002,64,2,64,109,-7,1206,6,603,4,595,1106,0,607,1001,64,1,64,1002,64,2,64,109,-14,2101,0,2,63,1008,63,33,63,1005,63,629,4,613,1105,1,633,1001,64,1,64,1002,64,2,64,109,-4,1208,8,30,63,1005,63,655,4,639,1001,64,1,64,1105,1,655,1002,64,2,64,109,23,21107,46,47,0,1005,1019,673,4,661,1105,1,677,1001,64,1,64,1002,64,2,64,109,-2,2105,1,7,4,683,1001,64,1,64,1106,0,695,1002,64,2,64,109,3,21102,47,1,-7,1008,1013,47,63,1005,63,717,4,701,1105,1,721,1001,64,1,64,1002,64,2,64,109,-11,1202,-7,1,63,1008,63,32,63,1005,63,745,1001,64,1,64,1105,1,747,4,727,1002,64,2,64,109,10,2106,0,9,4,753,1001,64,1,64,1105,1,765,1002,64,2,64,109,-24,1207,8,28,63,1005,63,783,4,771,1106,0,787,1001,64,1,64,1002,64,2,64,109,5,1201,0,0,63,1008,63,26,63,1005,63,813,4,793,1001,64,1,64,1105,1,813,1002,64,2,64,109,28,2106,0,-1,1001,64,1,64,1105,1,831,4,819,1002,64,2,64,109,-22,1202,-1,1,63,1008,63,24,63,1005,63,857,4,837,1001,64,1,64,1106,0,857,1002,64,2,64,109,-9,2108,30,6,63,1005,63,873,1106,0,879,4,863,1001,64,1,64,1002,64,2,64,109,-2,1208,10,26,63,1005,63,899,1001,64,1,64,1106,0,901,4,885,4,64,99,21102,1,27,1,21101,0,915,0,1105,1,922,21201,1,25948,1,204,1,99,109,3,1207,-2,3,63,1005,63,964,21201,-2,-1,1,21101,942,0,0,1106,0,922,22101,0,1,-1,21201,-2,-3,1,21102,957,1,0,1105,1,922,22201,1,-1,-2,1106,0,968,21201,-2,0,-2,109,-3,2106,0,0

2019/day9-go/main.go

Lines changed: 179 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,179 @@
1+
package main
2+
3+
import (
4+
"io"
5+
"strings"
6+
7+
lib "github.com/teivah/advent-of-code"
8+
)
9+
10+
func fs1(reader io.Reader) int {
11+
s := lib.ReaderToString(reader)
12+
codes := lib.StringsToInts(strings.Split(s, ","))
13+
14+
const input = 1
15+
output, _ := run(codes, input)
16+
return output
17+
}
18+
19+
func run(codes []int, input int) (int, bool) {
20+
state := &State{
21+
program: codes,
22+
memory: make([]int, 1000),
23+
offset: 0,
24+
over: false,
25+
input: input,
26+
}
27+
state.instructions = map[int]Apply{
28+
1: state.plus,
29+
2: state.mult,
30+
3: state.in,
31+
4: state.out,
32+
5: state.jnz,
33+
6: state.jiz,
34+
7: state.lt,
35+
8: state.eq,
36+
9: state.rlt,
37+
99: state.exit,
38+
}
39+
40+
for !state.over {
41+
if state.offset >= len(codes) {
42+
break
43+
}
44+
state.execute()
45+
}
46+
47+
return state.output, state.over
48+
}
49+
50+
func (s *State) execute() {
51+
opcode := toOpcode(s.program[s.offset])
52+
s.instructions[opcode.opcode](opcode.ctx)
53+
}
54+
55+
type Opcode struct {
56+
opcode int
57+
ctx Context
58+
}
59+
60+
func toOpcode(i int) Opcode {
61+
var v []int
62+
for i != 0 {
63+
v = append(v, i%10)
64+
i /= 10
65+
}
66+
67+
res := make([]int, 0, len(v))
68+
for idx := 0; idx < 5-len(v); idx++ {
69+
res = append(res, 0)
70+
}
71+
72+
for idx := 0; idx < len(v); idx++ {
73+
res = append(res, v[len(v)-idx-1])
74+
}
75+
76+
return Opcode{
77+
opcode: res[3]*10 + res[4],
78+
ctx: Context{
79+
modes: []int{res[2], res[1], res[0]},
80+
},
81+
}
82+
}
83+
84+
type State struct {
85+
program []int
86+
memory []int
87+
offset int
88+
over bool
89+
output int
90+
instructions map[int]Apply
91+
input int
92+
relativeMode int
93+
}
94+
95+
type Context struct {
96+
modes []int
97+
}
98+
99+
func (s *State) getOutputIndex(param int) int {
100+
return s.program[s.offset+param+1]
101+
}
102+
103+
func (s *State) getInput(ctx Context, param int) int {
104+
switch ctx.modes[param] {
105+
case 0:
106+
return s.program[s.program[s.offset+1+param]]
107+
case 1:
108+
return s.program[s.offset+1+param]
109+
case 2:
110+
return s.program[s.program[s.offset+1+param]+s.relativeMode]
111+
default:
112+
panic(ctx.modes[param])
113+
}
114+
}
115+
116+
type Apply func(ctx Context)
117+
118+
func (s *State) plus(ctx Context) {
119+
s.program[s.getOutputIndex(2)] = s.getInput(ctx, 0) + s.getInput(ctx, 1)
120+
s.offset += 4
121+
}
122+
123+
func (s *State) mult(ctx Context) {
124+
s.program[s.getOutputIndex(2)] = s.getInput(ctx, 0) * s.getInput(ctx, 1)
125+
s.offset += 4
126+
}
127+
128+
func (s *State) in(ctx Context) {
129+
s.program[s.getOutputIndex(0)] = s.input
130+
s.offset += 2
131+
}
132+
133+
func (s *State) out(ctx Context) {
134+
s.output = s.getInput(ctx, 0)
135+
s.offset += 2
136+
}
137+
138+
func (s *State) jnz(ctx Context) {
139+
if s.getInput(ctx, 0) != 0 {
140+
s.offset = s.getInput(ctx, 1)
141+
} else {
142+
s.offset += 3
143+
}
144+
}
145+
146+
func (s *State) jiz(ctx Context) {
147+
if s.getInput(ctx, 0) == 0 {
148+
s.offset = s.getInput(ctx, 1)
149+
} else {
150+
s.offset += 3
151+
}
152+
}
153+
154+
func (s *State) lt(ctx Context) {
155+
if s.getInput(ctx, 0) < s.getInput(ctx, 1) {
156+
s.program[s.getOutputIndex(2)] = 1
157+
} else {
158+
s.program[s.getOutputIndex(2)] = 0
159+
}
160+
s.offset += 4
161+
}
162+
163+
func (s *State) eq(ctx Context) {
164+
if s.getInput(ctx, 0) == s.getInput(ctx, 1) {
165+
s.program[s.getOutputIndex(2)] = 1
166+
} else {
167+
s.program[s.getOutputIndex(2)] = 0
168+
}
169+
s.offset += 4
170+
}
171+
172+
func (s *State) rlt(ctx Context) {
173+
s.relativeMode = s.getInput(ctx, 0)
174+
s.offset += 2
175+
}
176+
177+
func (s *State) exit(ctx Context) {
178+
s.over = true
179+
}

2019/day9-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 TestFs1Test(t *testing.T) {
12+
f, err := os.Open("test.txt")
13+
require.NoError(t, err)
14+
assert.Equal(t, 42, fs1(f))
15+
}
16+
17+
func TestFs1Input(t *testing.T) {
18+
f, err := os.Open("input.txt")
19+
require.NoError(t, err)
20+
assert.Equal(t, 42, fs1(f))
21+
}

2019/day9-go/test.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
109,1,204,-1,1001,100,1,100,1008,100,16,101,1006,101,0,99

0 commit comments

Comments
 (0)