2019 Day 02 Complete
This commit is contained in:
parent
abe0af5bb7
commit
6a26daaea7
1
2019/day02/1202input
Normal file
1
2019/day02/1202input
Normal file
@ -0,0 +1 @@
|
||||
1,12,2,3,1,1,2,3,1,3,4,3,1,5,0,3,2,13,1,19,1,10,19,23,1,23,9,27,1,5,27,31,2,31,13,35,1,35,5,39,1,39,5,43,2,13,43,47,2,47,10,51,1,51,6,55,2,55,9,59,1,59,5,63,1,63,13,67,2,67,6,71,1,71,5,75,1,75,5,79,1,79,9,83,1,10,83,87,1,87,10,91,1,91,9,95,1,10,95,99,1,10,99,103,2,103,10,107,1,107,9,111,2,6,111,115,1,5,115,119,2,119,13,123,1,6,123,127,2,9,127,131,1,131,5,135,1,135,13,139,1,139,10,143,1,2,143,147,1,147,10,0,99,2,0,14,0
|
1
2019/day02/input
Normal file
1
2019/day02/input
Normal file
@ -0,0 +1 @@
|
||||
1,0,0,3,1,1,2,3,1,3,4,3,1,5,0,3,2,13,1,19,1,10,19,23,1,23,9,27,1,5,27,31,2,31,13,35,1,35,5,39,1,39,5,43,2,13,43,47,2,47,10,51,1,51,6,55,2,55,9,59,1,59,5,63,1,63,13,67,2,67,6,71,1,71,5,75,1,75,5,79,1,79,9,83,1,10,83,87,1,87,10,91,1,91,9,95,1,10,95,99,1,10,99,103,2,103,10,107,1,107,9,111,2,6,111,115,1,5,115,119,2,119,13,123,1,6,123,127,2,9,127,131,1,131,5,135,1,135,13,139,1,139,10,143,1,2,143,147,1,147,10,0,99,2,0,14,0
|
92
2019/day02/main.go
Normal file
92
2019/day02/main.go
Normal file
@ -0,0 +1,92 @@
|
||||
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))
|
||||
}
|
||||
//part1(prog)
|
||||
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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func part2(prog []int) {
|
||||
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 {
|
||||
switch prog[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] == 19690720 {
|
||||
fmt.Println("Found value:", n, v)
|
||||
fmt.Println("Answer:", (100*n + v))
|
||||
os.Exit(0)
|
||||
}
|
||||
i = len(progcpy)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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
|
||||
}
|
1
2019/day02/testinput1
Normal file
1
2019/day02/testinput1
Normal file
@ -0,0 +1 @@
|
||||
1,9,10,3,2,3,11,0,99,30,40,50
|
1
2019/day02/testinput2
Normal file
1
2019/day02/testinput2
Normal file
@ -0,0 +1 @@
|
||||
1,0,0,0,99
|
1
2019/day02/testinput3
Normal file
1
2019/day02/testinput3
Normal file
@ -0,0 +1 @@
|
||||
2,3,0,3,99
|
1
2019/day02/testinput4
Normal file
1
2019/day02/testinput4
Normal file
@ -0,0 +1 @@
|
||||
2,4,4,5,99,0
|
1
2019/day02/testinput5
Normal file
1
2019/day02/testinput5
Normal file
@ -0,0 +1 @@
|
||||
1,1,1,4,99,5,6,0,99
|
Loading…
Reference in New Issue
Block a user