Super-Optimized 2019 Day 2

This commit is contained in:
Brian Buller 2019-12-03 09:25:11 -06:00
parent 4b016eac64
commit a28a6fd163
3 changed files with 27 additions and 11 deletions

1
2019/day02/input2 Normal file
View File

@ -0,0 +1 @@
1,0,0,3,1,1,2,3,1,3,4,3,1,5,0,3,2,10,1,19,1,19,5,23,1,23,9,27,2,27,6,31,1,31,6,35,2,35,9,39,1,6,39,43,2,10,43,47,1,47,9,51,1,51,6,55,1,55,6,59,2,59,10,63,1,6,63,67,2,6,67,71,1,71,5,75,2,13,75,79,1,10,79,83,1,5,83,87,2,87,10,91,1,5,91,95,2,95,6,99,1,99,6,103,2,103,6,107,2,107,9,111,1,111,5,115,1,115,6,119,2,6,119,123,1,5,123,127,1,127,13,131,1,2,131,135,1,135,10,0,99,2,14,0,0

1
2019/day02/input3 Normal file
View File

@ -0,0 +1 @@
1,0,0,3,1,1,2,3,1,3,4,3,1,5,0,3,2,1,10,19,1,6,19,23,1,13,23,27,1,6,27,31,1,31,10,35,1,35,6,39,1,39,13,43,2,10,43,47,1,47,6,51,2,6,51,55,1,5,55,59,2,13,59,63,2,63,9,67,1,5,67,71,2,13,71,75,1,75,5,79,1,10,79,83,2,6,83,87,2,13,87,91,1,9,91,95,1,9,95,99,2,99,9,103,1,5,103,107,2,9,107,111,1,5,111,115,1,115,2,119,1,9,119,0,99,2,0,14,0

View File

@ -18,7 +18,6 @@ func main() {
for _, v := range strings.FieldsFunc(inp, f) { for _, v := range strings.FieldsFunc(inp, f) {
prog = append(prog, Atoi(v)) prog = append(prog, Atoi(v))
} }
//part1(prog)
part2(prog) part2(prog)
} }
@ -36,6 +35,31 @@ func part1(prog []int) {
} }
} }
func part2(prog []int) {
runProg := func(n, v int, prog []int) int {
progcpy := make([]int, len(prog))
copy(progcpy, prog)
progcpy[1] = n
progcpy[2] = v
for i := 0; i < len(progcpy); i += 4 {
switch progcpy[i] {
case 1:
progcpy[progcpy[i+3]] = add(progcpy[i+1], progcpy[i+2], progcpy)
case 2:
progcpy[progcpy[i+3]] = mult(progcpy[i+1], progcpy[i+2], progcpy)
case 99:
i = len(progcpy)
}
}
return progcpy[0]
}
target := 19690720
zeroVal := runProg(0, 0, prog)
nDiff := runProg(1, 0, prog) - zeroVal
vDiff := runProg(0, 1, prog) - zeroVal
fmt.Println(((target - zeroVal) / nDiff), ((target%nDiff)-(zeroVal%nDiff))/vDiff)
}
func slowestPart2(prog []int) { func slowestPart2(prog []int) {
for n := 0; n <= 99; n++ { for n := 0; n <= 99; n++ {
for v := 0; v <= 99; v++ { for v := 0; v <= 99; v++ {
@ -108,16 +132,6 @@ func slowPart2(prog []int) {
} }
} }
func part2(prog []int) {
target := 19690720
zeroVal := 610685
n := (target - zeroVal) / 360000
target = target % 1000
zeroVal = zeroVal % 1000
v := target - zeroVal
fmt.Println("Answer:", (100*n + v))
}
func printState(prog []int) { func printState(prog []int) {
fmt.Println(prog) fmt.Println(prog)
} }