Skip to content

Commit d36c155

Browse files
committed
Solution for 2024, day 17
1 parent e957c4c commit d36c155

File tree

4 files changed

+145
-0
lines changed

4 files changed

+145
-0
lines changed

2024/17/Makefile

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
input:
2+
http "https://adventofcode.com/2024/day/17/input" "Cookie:session=${AOC_SESSION};" >input
3+
4+
main1:
5+
go build -o main1 main1.go common.go
6+
7+
main2:
8+
go build -o main2 main2.go common.go
9+
10+
.PHONY: run1 run2 clean
11+
12+
run1: main1 input
13+
./main1 <input
14+
15+
run2: main2 input
16+
./main2 <input
17+
18+
clean:
19+
rm -f main1 main2 input

2024/17/common.go

+80
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
package main
2+
3+
import (
4+
"bufio"
5+
"os"
6+
"strconv"
7+
"strings"
8+
)
9+
10+
func parseInput() (int, int, int, []int) {
11+
scanner := bufio.NewScanner(os.Stdin)
12+
13+
scanner.Scan()
14+
a, _ := strconv.Atoi(strings.Fields(scanner.Text())[2])
15+
16+
scanner.Scan()
17+
b, _ := strconv.Atoi(strings.Fields(scanner.Text())[2])
18+
19+
scanner.Scan()
20+
c, _ := strconv.Atoi(strings.Fields(scanner.Text())[2])
21+
22+
scanner.Scan()
23+
24+
scanner.Scan()
25+
var code []int
26+
for _, s := range strings.Split(strings.Fields(scanner.Text())[1], ",") {
27+
n, _ := strconv.Atoi(s)
28+
code = append(code, n)
29+
}
30+
return a, b, c, code
31+
}
32+
33+
func run(a, b, c int, code []int) []int {
34+
output := make([]int, 0)
35+
36+
for ip := 0; ip < len(code); {
37+
var (
38+
op = code[ip]
39+
arg = code[ip+1]
40+
combo = 0
41+
)
42+
43+
switch arg {
44+
case 1, 2, 3:
45+
combo = arg
46+
case 4:
47+
combo = a
48+
case 5:
49+
combo = b
50+
case 6:
51+
combo = c
52+
}
53+
54+
switch op {
55+
case 0: // adv
56+
a >>= combo
57+
case 1: // bxl
58+
b ^= arg
59+
case 2: // bst
60+
b = combo % 8
61+
case 3: // jnz
62+
if a != 0 {
63+
ip = arg
64+
continue
65+
}
66+
case 4: // bxc
67+
b ^= c
68+
case 5: // out
69+
output = append(output, combo%8)
70+
case 6: // bdv
71+
b = a >> combo
72+
case 7: // cdv
73+
c = a >> combo
74+
}
75+
76+
ip += 2
77+
}
78+
79+
return output
80+
}

2024/17/main1.go

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"strconv"
6+
"strings"
7+
)
8+
9+
func main() {
10+
a, b, c, code := parseInput()
11+
12+
output := run(a, b, c, code)
13+
14+
fmt.Println(strings.Join(toStr(output), ","))
15+
}
16+
17+
func toStr(x []int) []string {
18+
s := make([]string, len(x))
19+
for i := 0; i < len(x); i++ {
20+
s[i] = strconv.Itoa(x[i])
21+
}
22+
return s
23+
}

2024/17/main2.go

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"slices"
6+
)
7+
8+
func main() {
9+
_, b, c, code := parseInput()
10+
11+
a := 0
12+
for i := len(code) - 1; i >= 0; i-- {
13+
// make room for 3 bits by shifting to the left
14+
a <<= 3
15+
16+
// check incrementally only the latest bits,
17+
// until we find the right value
18+
for !slices.Equal(run(a, b, c, code), code[i:]) {
19+
a++
20+
}
21+
}
22+
fmt.Println(a)
23+
}

0 commit comments

Comments
 (0)