diff --git a/2019/day02/1202input b/2019/day02/1202input new file mode 100644 index 0000000..f729e3c --- /dev/null +++ b/2019/day02/1202input @@ -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 diff --git a/2019/day02/input b/2019/day02/input new file mode 100644 index 0000000..f2c7299 --- /dev/null +++ b/2019/day02/input @@ -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 diff --git a/2019/day02/main.go b/2019/day02/main.go new file mode 100644 index 0000000..f266410 --- /dev/null +++ b/2019/day02/main.go @@ -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 +} diff --git a/2019/day02/testinput1 b/2019/day02/testinput1 new file mode 100644 index 0000000..2912131 --- /dev/null +++ b/2019/day02/testinput1 @@ -0,0 +1 @@ +1,9,10,3,2,3,11,0,99,30,40,50 diff --git a/2019/day02/testinput2 b/2019/day02/testinput2 new file mode 100644 index 0000000..a2389ec --- /dev/null +++ b/2019/day02/testinput2 @@ -0,0 +1 @@ +1,0,0,0,99 diff --git a/2019/day02/testinput3 b/2019/day02/testinput3 new file mode 100644 index 0000000..e795b14 --- /dev/null +++ b/2019/day02/testinput3 @@ -0,0 +1 @@ +2,3,0,3,99 diff --git a/2019/day02/testinput4 b/2019/day02/testinput4 new file mode 100644 index 0000000..89e8255 --- /dev/null +++ b/2019/day02/testinput4 @@ -0,0 +1 @@ +2,4,4,5,99,0 diff --git a/2019/day02/testinput5 b/2019/day02/testinput5 new file mode 100644 index 0000000..f4b112c --- /dev/null +++ b/2019/day02/testinput5 @@ -0,0 +1 @@ +1,1,1,4,99,5,6,0,99