-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday17_2.go
51 lines (41 loc) · 1.18 KB
/
day17_2.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package day17
import (
"math"
"github.com/blfuentes/AdventOfCode_2024_Go/utilities"
)
type DoubleIdx struct {
PIdx int64
Mindex int
}
func findNewRegister(program Program) int64 {
reversed := utilities.ReverseCopy(program.Ops)
checkStack := utilities.Stack[DoubleIdx]{}
currentSolution := math.MaxInt64
checkStack.Push(DoubleIdx{0, 0})
for !checkStack.IsEmpty() {
tocheck, _ := checkStack.Pop()
for bitIdx := 0; bitIdx <= 8; bitIdx++ {
cloneProgram := Program{int64(bitIdx) + tocheck.PIdx, program.RegB, program.RegC, program.Ops}
output := runProgram(cloneProgram)
if output[0] == reversed[tocheck.Mindex] {
if tocheck.Mindex+1 >= len(program.Ops) {
if bitIdx+int(tocheck.PIdx) < currentSolution {
currentSolution = bitIdx + int(tocheck.PIdx)
}
} else {
checkStack.Push(DoubleIdx{8 * (tocheck.PIdx + int64(bitIdx)), tocheck.Mindex + 1})
}
}
}
}
return int64(currentSolution)
}
func Executepart2() int64 {
var result int64 = 0
var fileName string = "./day17/day17.txt"
if fileContent, err := utilities.ReadFileAsText(fileName); err == nil {
program := parseContent(fileContent)
result = findNewRegister(program)
}
return result
}