adventofcode/2019/day02/main.go

164 lines
3.3 KiB
Go
Raw Normal View History

2019-12-02 13:01:36 +00:00
package main
import (
"bufio"
"fmt"
"log"
"os"
"strconv"
"strings"
)
func main() {
inp := StdinToString()
f := func(c rune) bool {
return c == ','
}
var prog []int
for _, v := range strings.FieldsFunc(inp, f) {
prog = append(prog, Atoi(v))
}
part2(prog)
}
func part1(prog []int) {
for i := 0; i < len(prog); i += 4 {
switch prog[i] {
case 1:
prog[prog[i+3]] = add(prog[i+1], prog[i+2], prog)
case 2:
prog[prog[i+3]] = mult(prog[i+1], prog[i+2], prog)
case 99:
printState(prog)
os.Exit(0)
}
}
}
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
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)
}
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]
}
func StdinToString() string {
var input string
scanner := bufio.NewScanner(os.Stdin)
for scanner.Scan() {
input += scanner.Text()
}
return input
}
func Atoi(i string) int {
var ret int
var err error
if ret, err = strconv.Atoi(i); err != nil {
log.Fatal("Invalid Atoi")
}
return ret
}