From 8f24a935ebb859a7046d46b35cf99cb88a2cb99d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thorben=20G=C3=BCnther?= Date: Sun, 22 Dec 2019 20:06:49 +0100 Subject: [PATCH] Update day 2. --- 2/input.txt | 1 + 2/main.go | 34 +++++++++++++++++++++++----------- 2/opcode_test.go | 11 +++++++++++ 3 files changed, 35 insertions(+), 11 deletions(-) create mode 100644 2/input.txt create mode 100644 2/opcode_test.go diff --git a/2/input.txt b/2/input.txt new file mode 100644 index 0000000..640e1a3 --- /dev/null +++ b/2/input.txt @@ -0,0 +1 @@ +1,0,0,3,1,1,2,3,1,3,4,3,1,5,0,3,2,6,1,19,1,5,19,23,1,13,23,27,1,6,27,31,2,31,13,35,1,9,35,39,2,39,13,43,1,43,10,47,1,47,13,51,2,13,51,55,1,55,9,59,1,59,5,63,1,6,63,67,1,13,67,71,2,71,10,75,1,6,75,79,1,79,10,83,1,5,83,87,2,10,87,91,1,6,91,95,1,9,95,99,1,99,9,103,2,103,10,107,1,5,107,111,1,9,111,115,2,13,115,119,1,119,10,123,1,123,10,127,2,127,10,131,1,5,131,135,1,10,135,139,1,139,2,143,1,6,143,0,99,2,14,0,0 diff --git a/2/main.go b/2/main.go index 83b9c2b..4a70552 100644 --- a/2/main.go +++ b/2/main.go @@ -1,35 +1,47 @@ package main import ( + "bufio" "fmt" + "os" "git.xenrox.net/xenrox/aoc2019/tools" ) -func main() { - var lines []int - lines, err := tools.ScanFileInt("input.txt") - if err != nil { - fmt.Println("Error") - } +func calcOpcode(lines []int) int { pos := 0 var code int - //lines[1] = 12 - //lines[2] = 2 for true { code = lines[pos] switch code { case 99: - fmt.Println(lines[0]) - return + return lines[0] case 1: lines[lines[pos+3]] = lines[lines[pos+1]] + lines[lines[pos+2]] case 2: lines[lines[pos+3]] = lines[lines[pos+1]] * lines[lines[pos+2]] default: fmt.Println("Error") - return + return 0 } pos += 4 } + return 0 +} + +func main() { + var lines []int + file, err := os.Open("input.txt") + if err != nil { + fmt.Println("Error") + } + defer file.Close() + scanner := bufio.NewScanner(file) + lines, err := tools.ScanFileInt("input.txt") + if err != nil { + fmt.Println("Error") + } + lines[1] = 12 + lines[2] = 2 + fmt.Println(calcOpcode(lines)) } diff --git a/2/opcode_test.go b/2/opcode_test.go new file mode 100644 index 0000000..7f065db --- /dev/null +++ b/2/opcode_test.go @@ -0,0 +1,11 @@ +package main + +import "testing" + +func TestFuel1(t *testing.T) { + t.Parallel() + slice := []int{1, 9, 10, 3, 2, 3, 11, 0, 99, 30, 40, 50} + if calcOpcode(slice) != 3500 { + t.Error("Error") + } +} -- 2.44.0