Skip to content

Commit c6b96a5

Browse files
committed
2024.17 Part 1
1 parent b71e675 commit c6b96a5

File tree

3 files changed

+131
-0
lines changed

3 files changed

+131
-0
lines changed

2024/17-computer/example.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Register A: 2024
2+
Register B: 0
3+
Register C: 0
4+
5+
Program: 0,3,5,4,3,0

2024/17-computer/input.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Register A: 52042868
2+
Register B: 0
3+
Register C: 0
4+
5+
Program: 2,4,1,7,7,5,0,3,4,4,1,7,5,5,3,0

2024/17-computer/main.go

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
package main
2+
3+
import (
4+
"advent-of-code/util"
5+
"bufio"
6+
"fmt"
7+
"strconv"
8+
"strings"
9+
)
10+
11+
var A, B, C, ogB, ogC int
12+
var pc int
13+
var instructions []int
14+
var output []int
15+
16+
func main() {
17+
file := util.FileFromArgs()
18+
scan := bufio.NewScanner(file)
19+
20+
// Read input
21+
scan.Scan()
22+
A, _ = strconv.Atoi(scan.Text()[12:])
23+
scan.Scan()
24+
ogB, _ = strconv.Atoi(scan.Text()[12:])
25+
scan.Scan()
26+
ogC, _ = strconv.Atoi(scan.Text()[12:])
27+
scan.Scan()
28+
scan.Scan()
29+
instructions = util.StringToInts(scan.Text()[9:], ",")
30+
31+
fmt.Println(A, ogB, ogC, instructions)
32+
B, C = ogB, ogC
33+
34+
// Part 1: Run the program
35+
for Tick() {
36+
}
37+
fmt.Println(strings.Join(util.IntsToStrings(output), ","))
38+
39+
// Part 2: Find the correct value for A
40+
part2:
41+
for i := 0; ; i++ {
42+
A, B, C, pc = i, ogB, ogC, 0
43+
output = []int{}
44+
45+
for Tick() {
46+
if len(output) > len(instructions) {
47+
continue part2
48+
}
49+
}
50+
51+
if len(output) == len(instructions) {
52+
for j := range output {
53+
if output[j] != instructions[j] {
54+
continue part2
55+
}
56+
}
57+
58+
fmt.Println(i)
59+
break
60+
}
61+
62+
if i%1e7 == 0 {
63+
fmt.Println(i / 1e7)
64+
}
65+
}
66+
}
67+
68+
// Tick executes the next instruction. The function will return true as long as the program is active
69+
func Tick() bool {
70+
if pc > len(instructions)-1 {
71+
return false
72+
}
73+
74+
operand := instructions[pc+1]
75+
if instructions[pc] != 1 && instructions[pc] != 3 && instructions[pc] != 4 {
76+
switch operand {
77+
case 4:
78+
operand = A
79+
case 5:
80+
operand = B
81+
case 6:
82+
operand = C
83+
default:
84+
}
85+
}
86+
87+
operations[instructions[pc]](operand)
88+
89+
pc += 2
90+
91+
return true
92+
}
93+
94+
var operations = [8]func(int){
95+
0: func(operand int) { // adv
96+
A = A / util.IntPow(2, operand)
97+
},
98+
1: func(operand int) { // bxl
99+
B = B ^ operand
100+
},
101+
2: func(operand int) { // bst
102+
B = operand % 8
103+
},
104+
3: func(operand int) { // jnz
105+
if A != 0 {
106+
pc = operand - 2
107+
}
108+
},
109+
4: func(operand int) { // bxc
110+
B = B ^ C
111+
},
112+
5: func(operand int) { // out
113+
output = append(output, operand%8)
114+
},
115+
6: func(operand int) { // bdv
116+
B = A / util.IntPow(2, operand)
117+
},
118+
7: func(operand int) { // cdv
119+
C = A / util.IntPow(2, operand)
120+
},
121+
}

0 commit comments

Comments
 (0)