Skip to content

Commit 98c06aa

Browse files
committed
day 17 part 1
1 parent 7615936 commit 98c06aa

File tree

3 files changed

+128
-0
lines changed

3 files changed

+128
-0
lines changed

17/input.txt

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Register A: 62769524
2+
Register B: 0
3+
Register C: 0
4+
5+
Program: 2,4,1,7,7,5,0,3,4,0,1,7,5,5,3,0

17/main.go

+118
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"math"
6+
"os"
7+
"strconv"
8+
"strings"
9+
)
10+
11+
type Reg struct {
12+
A int
13+
B int
14+
C int
15+
// instruction pointer
16+
IP int
17+
}
18+
19+
type Op func(op int, reg *Reg)
20+
21+
var instructions = map[int]Op{
22+
0: adv,
23+
1: bxl,
24+
2: bst,
25+
3: jnz,
26+
4: bxc,
27+
5: out,
28+
6: bdv,
29+
7: cdv,
30+
}
31+
32+
func comboOp(op int, reg Reg) int {
33+
switch {
34+
case op <= 3:
35+
return op
36+
case op == 4:
37+
return reg.A
38+
case op == 5:
39+
return reg.B
40+
case op == 6:
41+
return reg.C
42+
}
43+
44+
return 0
45+
}
46+
47+
func adv(op int, reg *Reg) {
48+
reg.A = reg.A / (int(math.Pow(2, float64(comboOp(op, *reg)))))
49+
reg.IP += 2
50+
}
51+
52+
func bxl(op int, reg *Reg) {
53+
reg.B = reg.B ^ op
54+
reg.IP += 2
55+
}
56+
57+
func bst(op int, reg *Reg) {
58+
reg.B = comboOp(op, *reg) % 8
59+
reg.IP += 2
60+
}
61+
62+
func jnz(op int, reg *Reg) {
63+
if reg.A == 0 {
64+
reg.IP += 2
65+
return
66+
}
67+
reg.IP = op
68+
}
69+
70+
func bxc(op int, reg *Reg) {
71+
reg.B = reg.B ^ reg.C
72+
reg.IP += 2
73+
}
74+
75+
func out(op int, reg *Reg) {
76+
fmt.Print(comboOp(op, *reg) % 8)
77+
fmt.Print(",")
78+
reg.IP += 2
79+
}
80+
81+
func bdv(op int, reg *Reg) {
82+
reg.B = reg.A / (int(math.Pow(2, float64(comboOp(op, *reg)))))
83+
reg.IP += 2
84+
}
85+
86+
func cdv(op int, reg *Reg) {
87+
reg.C = reg.A / (int(math.Pow(2, float64(comboOp(op, *reg)))))
88+
reg.IP += 2
89+
}
90+
91+
func part1() {
92+
//raw, _ := os.ReadFile("test.txt")
93+
raw, _ := os.ReadFile("input.txt")
94+
data := strings.Split(string(raw), "\n\n")
95+
96+
regs := strings.Split(data[0], "\n")
97+
a, _ := strconv.Atoi(strings.TrimSpace(strings.Split(regs[0], ":")[1]))
98+
b, _ := strconv.Atoi(strings.TrimSpace(strings.Split(regs[1], ":")[1]))
99+
c, _ := strconv.Atoi(strings.TrimSpace(strings.Split(regs[2], ":")[1]))
100+
reg := Reg{A: a, B: b, C: c}
101+
102+
prog := strings.Split(strings.Split(data[1], "Program: ")[1], ",")
103+
program := make([]int, len(prog))
104+
for i, part := range prog {
105+
program[i], _ = strconv.Atoi(strings.TrimSpace(part))
106+
}
107+
108+
for reg.IP < len(program) {
109+
opCode := program[reg.IP]
110+
operand := program[reg.IP+1]
111+
instruction := instructions[opCode]
112+
instruction(operand, &reg)
113+
}
114+
}
115+
116+
func main() {
117+
part1()
118+
}

17/test.txt

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Register A: 729
2+
Register B: 0
3+
Register C: 0
4+
5+
Program: 0,1,5,4,3,0

0 commit comments

Comments
 (0)