2021 Day 24 Part 2
This commit is contained in:
parent
d09d968f44
commit
a38c025ead
1 changed files with 38 additions and 0 deletions
|
|
@ -22,6 +22,7 @@ func main() {
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Println(part1(steps))
|
fmt.Println(part1(steps))
|
||||||
|
fmt.Println(part2(steps))
|
||||||
}
|
}
|
||||||
|
|
||||||
func decompile(prog Program) (steps Steps) {
|
func decompile(prog Program) (steps Steps) {
|
||||||
|
|
@ -59,6 +60,32 @@ func part1(steps Steps) int {
|
||||||
return -1
|
return -1
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func part2(steps Steps) int {
|
||||||
|
var inp [14]int
|
||||||
|
var ups []Equation
|
||||||
|
|
||||||
|
for i, step := range steps {
|
||||||
|
switch step.zDiv {
|
||||||
|
case 1:
|
||||||
|
ups = append([]Equation{{i, step.yOff}}, ups...)
|
||||||
|
case 26:
|
||||||
|
up := ups[0]
|
||||||
|
ups = ups[1:]
|
||||||
|
dn := Equation{i, -step.xOff}
|
||||||
|
u, d := minSolution(dn.d - up.d)
|
||||||
|
inp[up.i] = u
|
||||||
|
inp[dn.i] = d
|
||||||
|
default:
|
||||||
|
panic(step.zDiv)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if steps.Run(inp[:], false) == 0 {
|
||||||
|
return reassemble(inp)
|
||||||
|
}
|
||||||
|
return -1
|
||||||
|
}
|
||||||
|
|
||||||
func reassemble(ds [14]int) (n int) {
|
func reassemble(ds [14]int) (n int) {
|
||||||
for _, d := range ds {
|
for _, d := range ds {
|
||||||
n *= 10
|
n *= 10
|
||||||
|
|
@ -78,6 +105,17 @@ func maxSolution(d int) (i, j int) {
|
||||||
return 0, 0
|
return 0, 0
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func minSolution(d int) (i, j int) {
|
||||||
|
for i := 1; i <= 9; i++ {
|
||||||
|
for j := 1; j <= 9; j++ {
|
||||||
|
if i-j == d {
|
||||||
|
return i, j
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return 0, 0
|
||||||
|
}
|
||||||
|
|
||||||
type Step struct{ zDiv, xOff, yOff int }
|
type Step struct{ zDiv, xOff, yOff int }
|
||||||
|
|
||||||
func NewStep(block Block) Step {
|
func NewStep(block Block) Step {
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue