From cb623d32f858a9635d9ffe061df570b9b11cce07 Mon Sep 17 00:00:00 2001 From: Jan-Lukas Else Date: Sat, 4 Dec 2021 18:06:20 +0100 Subject: [PATCH] Day 04 --- 04/input.txt | 601 ++++++++++++++++++++++++++++++++++++++++++++++++ 04/main.go | 174 ++++++++++++++ 04/main_test.go | 17 ++ 04/test.txt | 19 ++ 4 files changed, 811 insertions(+) create mode 100644 04/input.txt create mode 100644 04/main.go create mode 100644 04/main_test.go create mode 100644 04/test.txt diff --git a/04/input.txt b/04/input.txt new file mode 100644 index 0000000..1c4218f --- /dev/null +++ b/04/input.txt @@ -0,0 +1,601 @@ +17,11,37,7,89,48,99,28,56,55,57,27,83,59,53,72,6,87,33,82,13,23,35,40,71,47,78,2,39,4,51,1,67,31,79,69,15,73,80,22,92,95,91,43,26,97,36,34,12,96,86,52,66,94,61,76,64,77,85,98,42,68,84,63,60,30,65,19,54,58,24,20,25,75,93,16,18,44,14,88,45,10,9,3,70,74,81,90,46,38,21,49,29,50,0,5,8,32,62,41 + +57 80 91 40 12 +62 36 72 0 20 +55 60 25 92 96 +14 2 17 18 86 + 1 4 90 66 38 + + 1 25 81 16 24 +33 40 86 28 96 + 4 97 90 32 13 +50 38 35 14 56 +73 42 9 36 67 + +46 25 28 59 88 +33 48 15 0 95 +16 30 24 2 71 +11 81 58 70 65 +37 1 26 94 75 + +53 56 5 72 99 +46 27 23 49 4 + 3 93 21 65 37 +35 7 50 32 24 +81 78 52 54 88 + +76 14 58 84 17 + 5 89 97 57 15 +19 77 35 56 93 +31 71 92 2 39 +10 27 91 67 47 + +27 95 74 40 50 +49 66 37 80 75 +54 70 1 16 41 +11 77 61 6 89 +71 45 73 23 29 + +87 72 85 89 18 +91 56 54 14 88 +84 46 35 9 69 +36 66 53 34 17 +71 57 4 16 7 + +35 85 76 2 59 +47 51 36 29 22 +25 62 86 78 95 +61 13 97 99 65 +92 52 39 9 73 + +51 39 78 88 62 +79 91 58 50 15 +25 82 89 20 1 +12 73 7 95 32 +54 67 98 77 4 + +49 38 14 68 13 +33 42 44 58 65 +15 47 29 84 82 +34 94 18 99 11 +89 43 23 0 78 + +90 66 44 50 75 +71 57 87 40 98 +73 49 13 38 21 +35 6 11 91 84 +26 14 52 82 56 + +81 38 54 73 42 +16 12 94 36 60 +45 98 10 70 46 + 4 58 96 53 64 +48 84 30 0 2 + +37 16 48 41 97 +85 43 78 35 42 +39 18 30 44 82 +93 74 14 19 56 +87 10 80 77 17 + +27 38 36 91 45 +71 33 72 9 89 +26 39 25 74 88 + 1 81 35 86 59 +10 63 14 61 53 + +96 44 76 21 91 +82 31 90 4 48 +25 85 20 18 75 +64 71 98 47 49 +11 70 3 8 99 + +71 73 69 20 39 +63 25 28 32 1 + 8 29 41 36 74 +24 59 95 16 76 +70 4 68 60 46 + +35 73 66 20 44 +91 87 24 72 90 +64 81 84 83 30 +93 14 80 49 56 +53 94 10 92 34 + + 6 79 37 43 78 +81 64 27 26 86 +11 80 21 52 93 +30 72 23 51 40 +12 97 71 38 41 + +30 74 71 69 78 +85 56 23 58 24 +32 87 60 29 47 +67 19 57 35 14 +84 55 52 54 10 + +81 23 12 54 20 +27 67 33 17 15 +16 11 40 73 32 +72 30 35 7 71 +94 68 3 61 84 + + 9 12 73 84 90 +99 36 93 62 47 +28 54 59 17 74 +87 32 15 52 85 +31 11 82 1 43 + + 7 92 57 6 71 +28 90 78 66 48 +25 35 72 58 65 +64 68 49 83 2 +36 0 52 88 3 + +31 21 25 76 55 +32 53 92 38 6 +36 43 64 10 95 +96 79 90 8 54 +40 18 30 72 71 + +61 1 52 70 60 +47 69 57 74 35 + 3 34 85 66 14 +92 33 7 31 20 +29 54 23 38 2 + +39 49 87 43 11 +74 16 42 69 29 +17 60 0 38 66 +93 31 50 28 40 +75 76 12 79 88 + +93 44 15 97 1 +47 14 75 0 28 +27 49 45 59 91 + 7 42 72 86 74 +57 3 16 36 79 + +11 78 4 0 21 +71 9 18 88 80 +57 16 1 7 75 +25 50 70 3 22 +23 68 89 19 65 + +99 17 60 70 66 +88 24 30 8 62 +49 59 4 29 7 +14 81 65 26 36 +46 15 80 95 6 + +70 79 91 85 26 +89 63 86 73 82 +41 29 6 27 66 +59 39 37 17 51 +98 11 7 5 15 + +47 13 36 27 83 +55 64 8 87 76 + 4 2 75 71 28 +79 12 85 94 54 +45 17 14 30 62 + +21 20 46 80 25 +71 33 36 54 81 +76 34 63 40 35 +16 24 8 89 82 + 1 87 64 84 14 + +43 3 63 50 25 +65 14 4 92 60 +26 45 49 51 96 +74 11 40 46 7 +90 67 77 56 68 + + 4 24 92 95 44 +68 78 42 6 52 +17 23 89 56 38 +35 30 55 22 53 +73 64 61 34 19 + +77 50 94 27 31 +16 39 12 14 95 +24 48 26 71 67 +85 3 43 66 82 +92 56 8 44 0 + +37 46 31 67 9 +30 89 90 0 75 +99 45 73 93 52 +24 64 70 56 98 +53 48 10 51 14 + +84 28 33 23 27 +53 13 87 1 24 +86 16 10 90 62 +88 12 99 43 22 +40 97 78 77 9 + +97 43 61 53 90 +89 19 7 60 66 + 5 58 18 16 10 +14 13 47 39 20 +67 72 51 85 25 + +54 2 70 69 91 +14 23 49 59 7 +64 88 33 65 56 +31 12 36 73 46 +82 87 66 71 95 + +78 37 84 17 34 +64 6 68 74 46 +28 39 24 20 55 +12 61 97 60 98 +56 22 7 8 26 + +97 46 67 51 92 +79 82 63 69 80 +83 84 26 10 65 +90 35 49 95 14 +89 36 17 39 45 + +56 85 31 43 97 +48 84 83 12 28 +39 42 14 64 82 +98 17 72 29 91 +32 6 75 15 37 + +35 75 70 16 55 +60 47 96 51 21 +65 52 54 6 3 +24 12 72 64 78 + 8 17 34 83 71 + +62 34 27 90 22 +53 79 40 19 78 +52 77 23 60 54 +68 47 59 85 31 + 1 26 45 6 29 + +64 74 89 6 97 +99 94 36 72 32 +59 13 42 69 16 +66 37 41 55 83 +35 75 54 95 57 + +61 52 9 18 70 +41 53 90 94 87 +80 20 0 11 42 + 8 46 63 29 5 +34 15 7 91 10 + + 4 90 30 28 64 +38 35 42 79 21 +37 65 88 83 56 +78 11 12 22 50 +74 49 81 82 71 + +99 89 13 71 39 +96 78 67 82 59 +65 75 55 69 17 +16 15 86 27 85 +58 46 11 32 9 + +76 42 24 97 47 +72 55 28 32 18 + 8 65 50 84 71 +52 56 36 87 59 +20 33 89 46 41 + +59 84 46 15 11 +52 60 31 97 16 +67 3 53 10 61 +36 80 6 43 2 +95 91 14 76 77 + +89 27 59 4 99 +71 56 74 19 5 +11 84 61 90 16 +29 93 97 24 52 +64 88 18 34 50 + +63 18 38 79 34 +17 67 96 87 70 +60 2 54 29 81 +24 72 12 39 8 +59 36 44 97 69 + +47 87 74 21 73 +64 67 89 92 6 +95 26 77 65 45 +22 69 31 13 61 +29 43 68 81 28 + +12 30 18 91 27 +37 4 75 67 72 +99 33 19 90 25 +86 24 22 52 80 +38 62 94 11 44 + +53 47 36 57 9 +14 92 27 29 67 +21 87 76 5 60 +88 10 54 95 48 +32 18 64 8 37 + +83 53 88 51 46 +36 85 31 0 95 +58 52 57 2 75 +78 74 3 27 20 +86 99 7 29 96 + +48 32 52 95 21 +80 20 75 68 29 +38 56 94 16 50 +99 91 26 6 82 + 8 93 25 43 61 + +92 4 79 73 82 +68 20 44 15 70 +37 69 96 58 53 +25 76 86 54 47 +38 3 1 33 46 + +62 84 30 15 35 +65 90 46 0 38 +87 93 70 26 44 +41 1 11 66 14 +45 22 55 96 57 + +31 52 45 82 53 +61 15 40 83 68 +87 29 8 36 85 +26 47 12 37 32 +67 4 58 17 65 + +31 14 4 22 93 +47 50 41 29 6 +69 63 3 38 7 +82 23 75 48 90 +73 66 83 87 5 + +23 71 21 24 29 +52 94 84 14 58 +70 49 27 96 64 +48 66 38 65 89 +67 36 19 26 9 + + 2 22 40 34 12 +13 78 75 68 36 +19 50 79 9 5 +95 10 54 71 37 +20 63 7 97 31 + + 8 31 88 80 63 +33 25 77 37 89 +22 49 66 20 43 +50 21 30 57 99 + 9 47 48 26 44 + +22 28 39 26 57 +49 43 37 76 95 +83 30 41 50 27 +46 51 55 96 47 +87 62 24 19 60 + +48 68 34 73 84 +21 28 90 38 4 +13 19 27 45 36 +32 44 69 37 75 + 8 60 92 85 58 + +53 77 25 45 8 +34 41 80 79 81 +32 65 98 86 91 +74 38 50 58 44 +56 75 52 33 97 + +85 12 64 3 58 +90 36 27 71 78 +44 84 54 87 31 +16 50 14 17 93 +72 55 97 4 38 + +99 41 38 68 51 +73 25 5 59 9 +48 83 36 58 55 +31 53 35 64 98 +66 80 30 13 92 + +20 7 90 3 59 +83 49 42 26 22 +53 56 23 13 19 +63 5 74 97 79 + 1 92 32 73 4 + +52 79 29 27 14 + 8 39 0 71 82 +18 85 88 81 10 +15 2 63 9 23 +26 84 12 46 40 + +56 63 16 1 40 +44 18 5 50 80 +19 88 25 79 36 +17 89 78 76 46 +99 29 35 2 10 + +62 96 27 68 31 +34 30 7 8 21 +70 22 80 40 42 +61 18 98 23 5 +38 69 24 78 65 + +75 12 1 56 73 +25 90 9 10 53 +18 30 59 76 77 +39 40 28 68 46 +82 83 42 15 95 + +47 83 65 24 35 +94 30 12 17 63 +66 75 56 40 41 +67 53 26 27 64 +55 22 31 74 11 + +14 21 4 79 38 +52 70 50 44 29 +93 67 65 54 73 +83 34 97 30 60 +27 76 33 6 39 + +16 62 3 75 84 +54 10 1 2 44 +21 42 0 99 64 +91 45 67 34 82 +83 78 17 53 6 + +83 0 44 31 72 +73 19 18 85 45 +59 51 11 75 64 +90 56 97 46 53 +39 32 17 60 10 + +50 57 93 43 9 +63 20 15 5 17 +35 48 2 52 60 +34 33 90 85 16 +13 53 47 59 62 + +25 75 7 58 98 +28 66 20 21 84 +16 19 85 89 0 + 4 63 70 48 61 +13 8 23 15 37 + +19 15 8 88 97 +81 70 4 59 72 +34 24 39 47 58 +11 10 50 18 99 +40 16 78 45 74 + +82 98 67 94 22 +43 81 96 38 77 +15 6 0 40 62 +52 11 83 54 69 +12 7 70 5 31 + +30 57 73 38 85 +35 84 25 19 65 +17 6 33 54 13 +10 24 31 50 88 + 2 8 1 59 43 + +34 86 21 13 33 +37 20 39 57 66 +10 72 98 40 3 +83 23 87 11 70 +71 24 38 2 58 + +47 99 60 43 0 +66 84 40 29 79 +52 58 25 91 55 +37 69 22 82 4 +65 62 2 31 28 + +42 10 1 23 3 +27 53 46 89 31 +55 9 36 49 85 +87 73 92 5 61 +98 72 86 65 94 + + 6 58 78 75 3 +72 84 79 17 71 +66 0 90 81 28 +94 7 63 76 40 +85 82 49 74 65 + +46 27 38 87 55 +93 21 40 61 82 +43 75 54 32 77 + 6 23 84 10 4 +92 89 51 0 86 + +71 6 28 94 26 +12 84 39 65 22 +73 23 78 2 59 +40 66 83 91 92 +67 25 64 42 41 + +43 92 81 83 99 +59 18 23 78 94 +89 40 47 56 4 +24 73 9 29 60 + 5 26 51 68 6 + +59 7 83 32 72 +46 82 58 78 44 +70 45 64 84 38 +34 8 79 1 81 +41 98 48 33 18 + +68 30 32 16 88 +33 76 41 99 60 +11 38 14 49 65 +89 58 24 26 34 +70 92 29 64 94 + +78 87 15 29 5 +75 54 63 20 33 +84 88 25 80 11 +93 73 92 39 22 + 2 10 4 82 77 + +93 33 71 39 3 +27 42 76 12 13 +20 64 52 37 72 +94 21 23 99 91 +95 97 10 63 92 + +30 65 97 73 85 +90 60 34 1 37 +79 47 40 31 9 +62 51 15 19 76 +96 77 64 72 71 + +69 88 51 14 64 +77 12 96 46 58 +89 2 49 80 37 +32 62 44 23 84 +78 97 87 41 85 + +17 40 61 68 81 +52 24 77 30 80 +75 88 27 78 87 +94 16 36 67 19 +95 43 97 46 93 + +32 60 9 75 98 +74 82 94 20 81 +47 7 10 76 48 +88 11 86 69 67 +72 36 83 62 34 + +26 8 64 47 76 +27 15 82 65 80 +54 79 84 87 37 +97 18 3 93 20 +88 7 40 99 35 + +40 94 6 53 58 +45 60 81 64 30 +66 55 74 37 7 +63 95 23 72 17 +80 18 69 89 28 + +48 83 84 85 3 +98 80 50 96 91 +18 69 44 62 15 +20 88 12 45 28 + 8 29 0 37 27 \ No newline at end of file diff --git a/04/main.go b/04/main.go new file mode 100644 index 0000000..186c93a --- /dev/null +++ b/04/main.go @@ -0,0 +1,174 @@ +package main + +import ( + "log" + "os" + "strconv" + "strings" + + "github.com/thoas/go-funk" +) + +func main() { + inputBytes, _ := os.ReadFile("input.txt") + inputs := strings.Split(string(inputBytes), "\n") + + log.Println("Puzzle 1:", puzzle1(inputs)) + log.Println("Puzzle 2:", puzzle2(inputs)) +} + +func puzzle1(input []string) int { + called, bingos := parseBingos(input) + // Iterate through called numbers + lastCalledNumber := -1 + firstBingo := -1 +callnumbers: + for _, calledNumber := range called { + lastCalledNumber = calledNumber + // Replace called number in every bingo with -1 + for _, bingo := range bingos { + for i := 0; i < 5; i++ { + for j := 0; j < 5; j++ { + if bingo[i][j] == calledNumber { + bingo[i][j] = -1 + } + } + } + } + // Check if any bingo has row or column with sum of just 0 + for b, bingo := range bingos { + // Check rows + for i := 0; i < 5; i++ { + if funk.SumInt(bingo[i]) == -5 { + firstBingo = b + break callnumbers + } + } + // Check columns + for i := 0; i < 5; i++ { + if bingo[0][i]+bingo[1][i]+bingo[2][i]+bingo[3][i]+bingo[4][i] == -5 { + firstBingo = b + break callnumbers + } + } + } + } + // Get sum of remaining numbers in first bingo + sum := 0 + for _, row := range bingos[firstBingo] { + for _, number := range row { + if number != -1 { + sum += number + } + } + } + // Return product of sum and last called number + return sum * lastCalledNumber +} + +func puzzle2(input []string) int { + called, bingos := parseBingos(input) + // Iterate through called numbers + lastCalledNumber := -1 + solvedBingos := []int{} + lastSolvedBingo := -1 +callnumbers: + for _, calledNumber := range called { + lastCalledNumber = calledNumber + // Replace called number in every bingo with -1 + for _, bingo := range bingos { + for i := 0; i < 5; i++ { + for j := 0; j < 5; j++ { + if bingo[i][j] == calledNumber { + bingo[i][j] = -1 + } + } + } + } + // Check if any bingo has row or column with sum of just 0 + bingoiter: + for b, bingo := range bingos { + // Skip solved bingos + if funk.ContainsInt(solvedBingos, b) { + continue bingoiter + } + // Check rows + for i := 0; i < 5; i++ { + if funk.SumInt(bingo[i]) == -5 { + // Add bingo to solved bingos + solvedBingos = append(solvedBingos, b) + lastSolvedBingo = b + continue bingoiter + } + } + // Check columns + for i := 0; i < 5; i++ { + if bingo[0][i]+bingo[1][i]+bingo[2][i]+bingo[3][i]+bingo[4][i] == -5 { + // Add bingo to solved bingos + solvedBingos = append(solvedBingos, b) + lastSolvedBingo = b + continue bingoiter + } + } + } + // Check if all bingos but are solved + if len(bingos) == len(solvedBingos) { + break callnumbers + } + } + // Get sum of remaining numbers in last solved bingo + sum := 0 + for _, row := range bingos[lastSolvedBingo] { + for _, number := range row { + if number != -1 { + sum += number + } + } + } + // Return product of sum and last called number + return sum * lastCalledNumber +} + +func parseBingos(input []string) (called []int, bingos [][][]int) { + // Parse called numbers + called = []int{} + for _, number := range strings.Split(input[0], ",") { + no, _ := strconv.Atoi(number) + called = append(called, no) + } + // Parse bingos + bingos = [][][]int{} + bingoCurrentRow := 0 + currentBingo := [][]int{} + for _, line := range input[2:] { + // Trim spaces + line = strings.TrimSpace(line) + // Replace double spaces with single spaces + line = strings.ReplaceAll(line, " ", " ") + // Check if line is empty + if line == "" { + continue + } + // Parse row + row := []int{} + for i, number := range strings.Split(line, " ") { + if i > 4 { + break + } + no, _ := strconv.Atoi(number) + row = append(row, no) + } + // Add row to bingo + currentBingo = append(currentBingo, row) + // Check if bingo is complete + if bingoCurrentRow == 4 { + bingos = append(bingos, currentBingo) + bingoCurrentRow = 0 + currentBingo = [][]int{} + } else { + bingoCurrentRow++ + } + } + // Return called numbers and bingos + return +} diff --git a/04/main_test.go b/04/main_test.go new file mode 100644 index 0000000..dbe5579 --- /dev/null +++ b/04/main_test.go @@ -0,0 +1,17 @@ +package main + +import ( + "os" + "strings" + "testing" + + "github.com/stretchr/testify/assert" +) + +func Test(t *testing.T) { + inputBytes, _ := os.ReadFile("test.txt") + inputs := strings.Split(string(inputBytes), "\n") + + assert.Equal(t, 4512, puzzle1(inputs)) + assert.Equal(t, 1924, puzzle2(inputs)) +} diff --git a/04/test.txt b/04/test.txt new file mode 100644 index 0000000..49d17bc --- /dev/null +++ b/04/test.txt @@ -0,0 +1,19 @@ +7,4,9,5,11,17,23,2,0,14,21,24,10,16,13,6,15,25,12,22,18,20,8,19,3,26,1 + +22 13 17 11 0 + 8 2 23 4 24 +21 9 14 16 7 + 6 10 3 18 5 + 1 12 20 15 19 + + 3 15 0 2 22 + 9 18 13 17 5 +19 8 7 25 23 +20 11 10 24 4 +14 21 16 12 6 + +14 21 17 24 4 +10 16 15 9 19 +18 8 23 26 20 +22 11 13 6 5 + 2 0 12 3 7 \ No newline at end of file