From 1680aec3ad9b02e4a5dbd266165c0111d39c6f54 Mon Sep 17 00:00:00 2001 From: Alphyron Date: Mon, 2 Dec 2019 21:02:53 +0100 Subject: [PATCH] :sparkles: Finish task for part one for day 2 --- day2/input.txt | 1 + day2/main.go | 66 +++++++++++++++++++++++++++++++++++++ day2/main_test.go | 83 +++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 150 insertions(+) create mode 100644 day2/input.txt create mode 100644 day2/main.go create mode 100644 day2/main_test.go diff --git a/day2/input.txt b/day2/input.txt new file mode 100644 index 0000000..04b9352 --- /dev/null +++ b/day2/input.txt @@ -0,0 +1 @@ +1,0,0,3,1,1,2,3,1,3,4,3,1,5,0,3,2,1,13,19,1,9,19,23,2,13,23,27,2,27,13,31,2,31,10,35,1,6,35,39,1,5,39,43,1,10,43,47,1,5,47,51,1,13,51,55,2,55,9,59,1,6,59,63,1,13,63,67,1,6,67,71,1,71,10,75,2,13,75,79,1,5,79,83,2,83,6,87,1,6,87,91,1,91,13,95,1,95,13,99,2,99,13,103,1,103,5,107,2,107,10,111,1,5,111,115,1,2,115,119,1,119,6,0,99,2,0,14,0 diff --git a/day2/main.go b/day2/main.go new file mode 100644 index 0000000..740b137 --- /dev/null +++ b/day2/main.go @@ -0,0 +1,66 @@ +package main + +import ( + "bufio" + "errors" + "log" + "os" + "strconv" + "strings" +) + +func main() { + file, err := os.Open("input.txt") + if err != nil { + log.Fatal(err) + } + defer file.Close() + + scanner := bufio.NewScanner(file) + if !scanner.Scan() { + log.Fatalln("Problem while read line.") + return + } + + stringarr := strings.Split(scanner.Text(), ",") + var t2 = []int{} + + for _, i := range stringarr { + j, err := strconv.Atoi(i) + if err != nil { + panic(err) + } + t2 = append(t2, j) + } + // ========= Finish now, we can solve the task ====== + t2[1] = 12 + t2[2] = 2 + ParseArray(t2) + + log.Println("Part1: " + strconv.Itoa(t2[0])) +} + +func ParseArray(list []int) error { + for i := 0; i < len(list); i += 4 { + cmd := list[i] + + if cmd == 99 { + return nil + } + + num1 := list[list[i+1]] + num2 := list[list[i+2]] + sum := 0 + + if cmd == 1 { + sum = num1 + num2 + } else if cmd == 2 { + sum = num1 * num2 + } else { + return errors.New("Wrong Command (" + strconv.Itoa(cmd) + ") at position " + strconv.Itoa(i)) + } + + list[list[i+3]] = sum + } + return nil +} diff --git a/day2/main_test.go b/day2/main_test.go new file mode 100644 index 0000000..7813c81 --- /dev/null +++ b/day2/main_test.go @@ -0,0 +1,83 @@ +package main + +import ( + "strconv" + "strings" + "testing" +) + +func equalsArray(arr1 []int, arr2 []int) bool { + if len(arr1) != len(arr2) { + return false + } + + for i := 0; i < len(arr1); i++ { + if arr1[i] != arr2[i] { + return false + } + } + return true +} + +func join(arr []int) string { + valuesText := []string{} + + for i := range arr { + number := arr[i] + text := strconv.Itoa(number) + valuesText = append(valuesText, text) + } + return strings.Join(valuesText, ",") +} + +func TestCalcFuelRecusive1(t *testing.T) { + arr := []int{1, 0, 0, 0, 99} + err := ParseArray(arr) + + if err != nil { + t.Errorf("Error where no error should be: %v", err) + } + sol := []int{2, 0, 0, 0, 99} + if !equalsArray(arr, sol) { + t.Errorf("Array was incorrect, got: {%s}, want: {%s}.", join(arr), join(sol)) + } +} + +func TestCalcFuelRecusive2(t *testing.T) { + arr := []int{2, 3, 0, 3, 99} + err := ParseArray(arr) + + if err != nil { + t.Errorf("Error where no error should be: %v", err) + } + sol := []int{2, 3, 0, 6, 99} + if !equalsArray(arr, sol) { + t.Errorf("Array was incorrect, got: {%s}, want: {%s}.", join(arr), join(sol)) + } +} + +func TestCalcFuelRecusive3(t *testing.T) { + arr := []int{2, 4, 4, 5, 99, 0} + err := ParseArray(arr) + + if err != nil { + t.Errorf("Error where no error should be: %v", err) + } + sol := []int{2, 4, 4, 5, 99, 9801} + if !equalsArray(arr, sol) { + t.Errorf("Array was incorrect, got: {%s}, want: {%s}.", join(arr), join(sol)) + } +} + +func TestCalcFuelRecusive4(t *testing.T) { + arr := []int{1, 1, 1, 4, 99, 5, 6, 0, 99} + err := ParseArray(arr) + + if err != nil { + t.Errorf("Error where no error should be: %v", err) + } + sol := []int{30, 1, 1, 4, 2, 5, 6, 0, 99} + if !equalsArray(arr, sol) { + t.Errorf("Array was incorrect, got: {%s}, want: {%s}.", join(arr), join(sol)) + } +}