From e43af3b8bca6fbf63eaa6f8c7a27607fcfda7fe3 Mon Sep 17 00:00:00 2001 From: Karl Spickermann Date: Sun, 11 Dec 2022 04:28:52 +0100 Subject: [PATCH] Day10 --- day10/day10.go | 56 ++++++++++++++++++ day10/input | 144 ++++++++++++++++++++++++++++++++++++++++++++++ day10/testinput | 146 +++++++++++++++++++++++++++++++++++++++++++++++ day10/testinput2 | 3 + day9/day9.go | 2 +- helper/helper.go | 12 +++- 6 files changed, 361 insertions(+), 2 deletions(-) create mode 100644 day10/day10.go create mode 100644 day10/input create mode 100644 day10/testinput create mode 100644 day10/testinput2 diff --git a/day10/day10.go b/day10/day10.go new file mode 100644 index 0000000..ebb5373 --- /dev/null +++ b/day10/day10.go @@ -0,0 +1,56 @@ +package main + +import ( + "AOC2022/helper" + "fmt" + "os" + "strconv" + "strings" +) + +type Chip struct { + activeValue int + currentcycle int + signalStrenght int + CRT [240]rune +} + +func main() { + args := os.Args[1:] + lines := helper.ReadTextFile(args[0]) + chip := Chip{1, 1, 0, [240]rune{}} + for _, line := range lines { + command := strings.Split(line, " ") + switch command[0] { + case "addx": + chip.increaseCurrentCyle() + chip.increaseCurrentCyle() + chip.activeValue += helper.RemoveError(strconv.Atoi(command[1])) + case "noop": + chip.increaseCurrentCyle() + } + } + fmt.Println(chip.signalStrenght) + chip.pringCRT() +} + +func (chip *Chip) increaseCurrentCyle() { + fmt.Printf("%v : %v \n", chip.currentcycle, chip.activeValue) + spritePos := chip.activeValue + if helper.Contains([]int{spritePos - 1, spritePos, spritePos + 1}, (chip.currentcycle-1)%40) { + chip.CRT[chip.currentcycle-1] = '#' + } else { + chip.CRT[chip.currentcycle-1] = '.' + } + if chip.currentcycle == 20 || (chip.currentcycle-20)%40 == 0 { + chip.signalStrenght += chip.activeValue * chip.currentcycle + } + chip.currentcycle++ +} +func (chip *Chip) pringCRT() { + j := 0 + for i := 0; i <= 240; i += 40 { + fmt.Println(string(chip.CRT[j:i])) + j = i + } +} diff --git a/day10/input b/day10/input new file mode 100644 index 0000000..f9589dd --- /dev/null +++ b/day10/input @@ -0,0 +1,144 @@ +noop +addx 33 +addx -30 +noop +noop +addx 7 +addx 1 +noop +noop +addx 3 +addx 3 +addx 3 +addx -4 +addx 5 +addx 2 +noop +addx 7 +noop +addx 1 +addx 4 +noop +addx 1 +addx -38 +noop +addx 16 +addx -13 +addx 2 +addx 7 +noop +addx -2 +addx -10 +addx 17 +addx -5 +addx 10 +noop +addx -15 +addx 16 +addx 2 +noop +noop +addx 7 +addx 3 +addx -2 +addx 2 +addx 5 +addx -38 +addx 7 +addx -6 +addx 2 +noop +addx 7 +noop +addx 1 +addx 4 +noop +noop +noop +noop +noop +addx 3 +noop +addx 3 +addx 2 +noop +addx 7 +noop +addx -20 +addx 21 +addx 3 +addx 1 +addx -35 +addx 1 +addx 4 +noop +addx 31 +noop +addx -26 +addx 5 +noop +noop +addx -2 +addx 25 +addx -18 +addx -13 +addx 14 +addx 2 +noop +noop +noop +addx 6 +addx 1 +addx 5 +addx 3 +addx -2 +addx -38 +addx 24 +addx -17 +addx 5 +noop +noop +addx -2 +addx 31 +addx -24 +addx 7 +addx -10 +addx 6 +noop +addx 3 +addx 2 +noop +noop +addx 7 +addx -2 +addx -26 +addx 31 +addx 5 +addx -40 +addx 5 +addx 33 +addx -31 +noop +addx 1 +addx 4 +addx 1 +addx 4 +addx 20 +noop +noop +addx -14 +addx -1 +addx 5 +noop +noop +addx 1 +addx 2 +noop +noop +addx 7 +noop +noop +noop +noop +noop +noop \ No newline at end of file diff --git a/day10/testinput b/day10/testinput new file mode 100644 index 0000000..94cd0a8 --- /dev/null +++ b/day10/testinput @@ -0,0 +1,146 @@ +addx 15 +addx -11 +addx 6 +addx -3 +addx 5 +addx -1 +addx -8 +addx 13 +addx 4 +noop +addx -1 +addx 5 +addx -1 +addx 5 +addx -1 +addx 5 +addx -1 +addx 5 +addx -1 +addx -35 +addx 1 +addx 24 +addx -19 +addx 1 +addx 16 +addx -11 +noop +noop +addx 21 +addx -15 +noop +noop +addx -3 +addx 9 +addx 1 +addx -3 +addx 8 +addx 1 +addx 5 +noop +noop +noop +noop +noop +addx -36 +noop +addx 1 +addx 7 +noop +noop +noop +addx 2 +addx 6 +noop +noop +noop +noop +noop +addx 1 +noop +noop +addx 7 +addx 1 +noop +addx -13 +addx 13 +addx 7 +noop +addx 1 +addx -33 +noop +noop +noop +addx 2 +noop +noop +noop +addx 8 +noop +addx -1 +addx 2 +addx 1 +noop +addx 17 +addx -9 +addx 1 +addx 1 +addx -3 +addx 11 +noop +noop +addx 1 +noop +addx 1 +noop +noop +addx -13 +addx -19 +addx 1 +addx 3 +addx 26 +addx -30 +addx 12 +addx -1 +addx 3 +addx 1 +noop +noop +noop +addx -9 +addx 18 +addx 1 +addx 2 +noop +noop +addx 9 +noop +noop +noop +addx -1 +addx 2 +addx -37 +addx 1 +addx 3 +noop +addx 15 +addx -21 +addx 22 +addx -6 +addx 1 +noop +addx 2 +addx 1 +noop +addx -10 +noop +noop +addx 20 +addx 1 +addx 2 +addx 2 +addx -6 +addx -11 +noop +noop +noop \ No newline at end of file diff --git a/day10/testinput2 b/day10/testinput2 new file mode 100644 index 0000000..fd7eba8 --- /dev/null +++ b/day10/testinput2 @@ -0,0 +1,3 @@ +noop +addx 3 +addx -5 \ No newline at end of file diff --git a/day9/day9.go b/day9/day9.go index 12bc4b1..4ce433e 100644 --- a/day9/day9.go +++ b/day9/day9.go @@ -114,7 +114,7 @@ func (simulation *Simulation) print(size int) { tempArr += "#" } else if simulation.headPos == pos { tempArr += "H" - } else if helper.Contains(simulation.tailPos, pos) { + } else if helper.Contains2Int(simulation.tailPos, pos) { tempArr += "T" } else { tempArr += "." diff --git a/helper/helper.go b/helper/helper.go index bbe58ba..7cfeaeb 100644 --- a/helper/helper.go +++ b/helper/helper.go @@ -1,6 +1,7 @@ package helper import ( + "constraints" "fmt" "os" "strings" @@ -78,7 +79,16 @@ func Sum[T int | int64](slice []T) (s T) { return } -func Contains[T [2]int](elems []T, v T) bool { +func Contains2Int[T [2]int](elems []T, v T) bool { + for _, s := range elems { + if v == s { + return true + } + } + return false +} + +func Contains[T constraints.Ordered](elems []T, v T) bool { for _, s := range elems { if v == s { return true