Skip to content
This repository was archived by the owner on Dec 28, 2024. It is now read-only.

Commit 75eabce

Browse files
committed
Add too slow solution for day 17 part 2
Naive solution that works for the example, but is too slow for regular inputs.
1 parent 6031974 commit 75eabce

File tree

3 files changed

+78
-22
lines changed

3 files changed

+78
-22
lines changed

solutions/day17/machine.go

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,17 @@ import (
66
)
77

88
type machine struct {
9-
a int64
10-
b int64
11-
c int64
12-
seq []int64
13-
out []int64
14-
pos int
15-
count int
9+
a int64
10+
b int64
11+
c int64
12+
seq []int64
13+
out []int64
14+
pos int
1615
}
1716

18-
func (m *machine) run(maxItt int) {
19-
for m.pos < len(m.seq) && (maxItt <= 0 || m.count < maxItt) {
17+
func (m *machine) run(maxOut int) {
18+
for m.pos < len(m.seq) && (maxOut < 1 || len(m.out) < maxOut) {
2019
m.next()
21-
m.count++
2220
}
2321
}
2422

solutions/day17/main.go

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import (
66
)
77

88
func main() {
9-
common.Setup(17, part1, nil)
9+
common.Setup(17, part1, part2)
1010
}
1111

1212
func part1(
@@ -19,3 +19,47 @@ func part1(
1919
m.run(-1)
2020
return m.strOut()
2121
}
22+
23+
func part2(
24+
input string,
25+
) string {
26+
m, err := parseMachine(input)
27+
if err != nil {
28+
return fmt.Sprintf("Failed to parse machine: %v", err)
29+
}
30+
31+
lenSeq := len(m.seq)
32+
initA := int64(0)
33+
initB := m.b
34+
initC := m.c
35+
for initA = 0; true; initA++ {
36+
m.run(lenSeq)
37+
if compareSeqOut(m, lenSeq) {
38+
break
39+
}
40+
m.a = initA
41+
m.b = initB
42+
m.c = initC
43+
m.out = m.out[:0]
44+
m.pos = 0
45+
}
46+
47+
return fmt.Sprintf("Registry A should be %d", initA-1)
48+
}
49+
50+
func compareSeqOut(
51+
m machine,
52+
lenSeq int,
53+
) bool {
54+
if len(m.out) != lenSeq {
55+
return false
56+
}
57+
58+
for i, seq := range m.seq {
59+
if m.out[i] != seq {
60+
return false
61+
}
62+
}
63+
64+
return true
65+
}

solutions/day17/main_test.go

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,6 @@ import (
44
"testing"
55
)
66

7-
const ex1Out = "4,6,3,5,6,3,5,2,1,0"
8-
const ex1 = `Register A: 729
9-
Register B: 0
10-
Register C: 0
11-
12-
Program: 0,1,5,4,3,0`
13-
147
func TestParseMachine(t *testing.T) {
158
testInput := `Register A: -1337
169
Register B: 0
@@ -49,9 +42,30 @@ Program: 0,-1,5,-4,3,0`
4942
}
5043
}
5144

52-
func TestPart1_Example1(t *testing.T) {
53-
out := part1(ex1)
54-
if out != ex1Out {
55-
t.Errorf("expected '%s' but got '%s'", ex1Out, out)
45+
func TestPart1_Example(t *testing.T) {
46+
const ex = `Register A: 729
47+
Register B: 0
48+
Register C: 0
49+
50+
Program: 0,1,5,4,3,0`
51+
const exOut = "4,6,3,5,6,3,5,2,1,0"
52+
53+
out := part1(ex)
54+
if out != exOut {
55+
t.Errorf("expected '%s' but got '%s'", exOut, out)
56+
}
57+
}
58+
59+
func TestPart2_Example(t *testing.T) {
60+
ex := `Register A: 2024
61+
Register B: 0
62+
Register C: 0
63+
64+
Program: 0,3,5,4,3,0`
65+
exOut := "Registry A should be 117440"
66+
67+
out := part2(ex)
68+
if out != exOut {
69+
t.Errorf("expected '%s' but got '%s'", exOut, out)
5670
}
5771
}

0 commit comments

Comments
 (0)