~xenrox/aoc2019

8f24a935ebb859a7046d46b35cf99cb88a2cb99d — Thorben Günther 4 years ago 884ebf4
Update day 2.
3 files changed, 35 insertions(+), 11 deletions(-)

A 2/input.txt
M 2/main.go
A 2/opcode_test.go
A 2/input.txt => 2/input.txt +1 -0
@@ 0,0 1,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

M 2/main.go => 2/main.go +23 -11
@@ 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))
}

A 2/opcode_test.go => 2/opcode_test.go +11 -0
@@ 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")
	}
}