adventofcode/2019/day02/main.go

131 lines
2.8 KiB
Go
Raw Normal View History

2019-12-02 13:01:36 +00:00
package main
import (
"fmt"
"os"
"strings"
2019-12-05 15:01:52 +00:00
intcode "git.bullercodeworks.com/brian/adventofcode/2019/intcode-processor"
helpers "git.bullercodeworks.com/brian/adventofcode/helpers"
2019-12-02 13:01:36 +00:00
)
func main() {
2019-12-05 15:01:52 +00:00
inp := helpers.StdinToString()
2019-12-02 13:01:36 +00:00
var prog []int
for _, v := range strings.Split(inp, ",") {
2019-12-05 15:01:52 +00:00
prog = append(prog, helpers.Atoi(v))
2019-12-02 13:01:36 +00:00
}
part1(prog)
part2(prog)
2019-12-02 13:01:36 +00:00
}
func part1(prog []int) {
p := intcode.NewProgram(prog)
p.Run()
if p.State() == intcode.RET_ERR {
fmt.Println(p.Error())
2019-12-02 13:01:36 +00:00
}
fmt.Println("Part 1:", p.GetProgramValueAt(0))
2019-12-02 13:01:36 +00:00
}
2019-12-03 15:25:11 +00:00
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
p := intcode.NewProgram(progcpy)
p.Run()
return p.GetProgramValueAt(0)
2019-12-03 15:25:11 +00:00
}
target := 19690720
zeroVal := runProg(0, 0, prog)
nDiff := runProg(1, 0, prog) - zeroVal
vDiff := runProg(0, 1, prog) - zeroVal
fmt.Println("Part 2:", ((target - zeroVal) / nDiff), ((target%nDiff)-(zeroVal%nDiff))/vDiff)
2019-12-03 15:25:11 +00:00
}
2019-12-03 15:03:08 +00:00
func slowestPart2(prog []int) {
2019-12-02 13:01:36 +00:00
for n := 0; n <= 99; n++ {
for v := 0; v <= 99; v++ {
progcpy := make([]int, len(prog))
copy(progcpy, prog)
progcpy[1], progcpy[2] = n, v
for i := 0; i < len(progcpy); i += 4 {
2019-12-03 14:48:05 +00:00
switch progcpy[i] {
2019-12-02 13:01:36 +00:00
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:
if progcpy[0] == 19690720 {
fmt.Println("Found value:", n, v)
fmt.Println("Answer:", (100*n + v))
os.Exit(0)
2019-12-03 14:48:05 +00:00
} else {
fmt.Println(n, v, ":", progcpy[0])
2019-12-02 13:01:36 +00:00
}
i = len(progcpy)
}
}
}
}
}
2019-12-03 15:03:08 +00:00
func slowPart2(prog []int) {
2019-12-03 14:48:05 +00:00
target := 19690720
useN := 0
for n := 0; n <= 99; n++ {
progcpy := make([]int, len(prog))
copy(progcpy, prog)
progcpy[1] = n
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)
}
}
if progcpy[0] > target {
useN = n - 1
break
}
}
for v := 0; v <= 99; v++ {
progcpy := make([]int, len(prog))
copy(progcpy, prog)
progcpy[1] = useN
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:
if progcpy[0] == target {
fmt.Println("Found value:", useN, v)
fmt.Println("Answer:", (100*useN + v))
os.Exit(0)
}
i = len(progcpy)
}
}
}
}
2019-12-02 13:01:36 +00:00
func printState(prog []int) {
fmt.Println(prog)
}
func add(i, j int, prog []int) int {
return prog[i] + prog[j]
}
func mult(i, j int, prog []int) int {
return prog[i] * prog[j]
}