From 58f5cc26f85758573ef685ceed001ca8539bdcae Mon Sep 17 00:00:00 2001 From: ptrcnull Date: Sat, 4 Dec 2021 07:36:07 +0100 Subject: [PATCH] 04: part1+2 --- 4/input | 601 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 4/part1.go | 128 +++++++++++ 4/part2.go | 60 ++++++ 4/testinput | 19 ++ 4 files changed, 808 insertions(+) create mode 100644 4/input create mode 100644 4/part1.go create mode 100644 4/part2.go create mode 100644 4/testinput diff --git a/4/input b/4/input new file mode 100644 index 0000000..d189927 --- /dev/null +++ b/4/input @@ -0,0 +1,601 @@ +57,9,8,30,40,62,24,70,54,73,12,3,71,95,58,88,23,81,53,80,22,45,98,37,18,72,14,20,66,0,19,31,82,34,55,29,27,96,48,28,87,83,36,26,63,21,5,46,33,86,32,56,6,38,52,16,41,74,99,77,13,35,65,4,78,91,90,43,1,2,64,60,94,85,61,84,42,76,68,10,49,89,11,17,79,69,39,50,25,51,47,93,44,92,59,75,7,97,67,15 + +45 57 55 43 31 +32 52 79 65 80 +21 98 16 64 6 +19 78 48 59 51 +37 2 69 56 99 + +87 20 29 96 75 +83 34 84 72 98 +70 89 90 73 38 +86 2 47 62 11 +24 60 64 65 31 + +11 20 22 49 30 +59 87 10 31 68 +64 24 82 26 6 +92 38 48 4 54 +93 81 28 80 99 + +29 4 62 28 85 +71 2 77 3 98 +74 57 25 33 92 +64 95 61 73 99 +76 36 81 87 1 + +79 59 96 61 95 +81 77 56 68 36 +69 39 0 55 14 +16 3 4 34 63 +84 80 99 37 9 + +86 33 77 30 59 +19 54 48 28 89 +26 38 82 68 69 +87 76 85 22 50 +74 72 58 81 49 + + 3 8 39 15 69 +14 72 90 81 58 +54 13 59 53 97 +84 20 43 57 89 +22 92 28 51 45 + +86 91 63 52 27 +50 75 94 89 31 +79 44 92 29 97 +34 60 42 37 80 +73 28 7 96 10 + +85 60 89 34 6 +41 81 39 37 57 +23 70 79 46 15 +74 54 59 88 9 +58 97 5 51 1 + +54 82 22 26 18 +46 12 21 36 79 +83 71 14 29 45 +42 24 73 58 68 +63 32 9 86 98 + +59 83 13 34 44 +80 55 81 67 3 +74 58 32 43 6 +61 73 21 23 66 + 2 9 52 29 86 + +29 24 37 21 2 +81 0 22 59 41 +44 40 72 31 71 + 9 99 50 65 97 +55 69 88 58 96 + + 3 69 94 88 12 +40 81 77 38 6 + 8 35 91 18 85 + 2 14 73 62 44 +46 9 37 1 20 + +86 58 85 43 65 +92 44 69 2 14 +83 3 93 16 49 +42 59 29 75 32 +45 4 48 21 68 + +87 65 80 18 46 +66 49 78 60 31 +20 74 29 96 86 +12 35 47 93 16 +38 91 54 73 28 + +26 68 98 32 67 +46 61 64 35 38 +92 77 70 76 88 +86 0 58 13 51 +96 1 62 53 8 + + 2 40 32 62 33 +84 96 99 76 95 + 9 1 12 7 90 +67 11 14 97 24 +42 54 57 45 83 + +39 99 37 0 95 +18 2 73 31 17 +32 66 21 62 9 + 4 78 22 53 45 +41 33 71 6 50 + +14 12 2 42 7 +52 71 90 28 75 + 0 40 79 39 93 +84 16 82 31 94 +74 36 59 72 15 + + 7 92 42 41 22 +28 31 91 68 12 +45 84 83 34 56 +70 43 37 54 60 +61 40 98 77 17 + +12 81 17 27 66 +49 95 82 97 85 +16 58 13 11 56 +88 31 36 96 23 + 0 51 55 22 62 + + 8 36 9 63 71 +79 97 60 16 91 +93 68 54 28 32 +42 57 20 43 47 +99 26 67 76 33 + + 1 55 58 48 92 +66 71 89 46 96 +15 37 94 14 47 +22 61 91 80 51 +33 44 63 10 88 + + 5 63 34 56 0 +97 22 48 11 85 +29 10 61 30 26 +55 1 32 27 77 +80 81 70 62 33 + +77 72 75 41 66 + 7 54 58 21 70 +95 30 14 71 99 +20 79 22 91 94 +45 10 86 18 63 + +55 22 21 79 86 +35 95 99 60 1 +25 68 82 93 14 +74 28 41 73 78 +15 61 70 56 3 + +80 35 25 22 12 +37 24 97 59 44 +54 84 1 33 11 + 9 28 74 30 95 +67 81 19 71 40 + +10 78 74 83 8 +90 86 41 82 31 +17 51 54 12 29 +32 62 87 2 0 +98 33 27 22 64 + +86 80 85 28 26 +44 25 5 78 87 +50 70 57 75 32 +11 20 52 97 88 +68 43 0 7 38 + +88 16 10 34 75 +76 84 41 1 61 +49 94 14 26 36 +85 77 22 98 70 +12 38 3 74 92 + +34 91 21 73 99 +28 82 69 18 85 +97 25 65 61 55 +96 33 63 2 77 +12 41 72 39 23 + + 0 45 95 55 34 +31 77 54 66 79 +90 11 49 68 93 +61 15 56 4 53 +57 69 97 7 6 + +94 11 44 83 87 +27 47 93 50 38 +29 55 10 49 32 +76 73 91 37 34 +51 62 4 85 46 + +66 64 5 33 99 +95 34 65 69 27 +49 17 46 53 76 +75 9 92 94 7 +59 60 2 40 70 + +28 80 27 88 79 +26 49 81 64 69 +90 51 42 83 70 +46 10 53 5 96 +29 99 84 22 8 + +86 49 31 53 28 +85 94 4 98 30 +51 7 48 88 1 +76 92 64 29 73 +81 6 21 36 74 + +14 19 15 97 81 +92 37 98 77 33 +20 24 4 51 79 +99 66 43 75 73 +46 87 58 93 5 + +69 76 46 21 57 +49 90 40 34 99 +70 89 4 0 23 + 5 86 44 62 53 +36 13 61 51 15 + +88 37 14 50 26 +76 83 24 46 5 +43 42 72 17 59 + 6 11 36 25 19 +70 53 52 98 30 + +87 93 25 46 74 +62 16 9 30 85 +60 21 29 17 5 +35 49 84 53 42 +13 90 99 70 48 + +19 91 10 89 52 +71 1 42 75 83 +81 32 96 53 5 +26 60 3 95 51 +44 12 33 76 64 + +77 17 29 55 43 +62 52 92 53 21 +74 71 46 38 7 +23 79 65 61 89 +50 90 83 26 19 + +58 85 18 17 29 +76 78 91 87 31 +49 82 95 89 6 +53 79 9 97 25 +48 68 98 13 21 + +40 90 77 45 48 +18 54 15 56 57 +82 11 36 92 35 +50 68 86 0 97 +24 78 49 75 62 + +63 91 7 16 8 +90 60 93 40 45 +49 28 41 35 21 +79 54 5 0 13 +68 20 37 55 59 + +38 26 33 78 76 +42 63 73 98 24 +77 27 67 8 30 +90 13 20 59 5 +32 22 1 46 79 + +15 39 72 27 73 +14 29 34 30 8 +91 43 66 75 21 + 7 16 78 48 41 +93 83 77 94 57 + +22 41 70 14 73 +64 4 13 60 98 +59 71 12 53 93 +68 11 54 95 37 +58 35 43 48 87 + +81 7 49 42 24 +86 76 36 34 16 +55 73 27 28 88 +66 83 58 80 48 +62 9 18 96 77 + +64 15 37 61 17 +80 69 67 98 89 +22 12 32 74 47 +97 23 49 30 91 +38 68 53 40 82 + +17 1 56 75 46 +20 2 98 71 96 +34 35 63 73 59 + 7 89 95 51 16 +69 81 37 91 61 + + 3 17 45 36 59 + 7 24 70 86 72 +77 15 34 69 37 +84 60 76 33 5 +26 21 48 61 12 + +19 56 90 95 3 +68 50 37 65 27 +39 35 72 61 22 +49 80 24 23 58 + 7 12 89 94 9 + +45 32 90 66 73 +22 7 41 21 20 +49 63 93 59 15 + 2 82 96 30 27 +40 85 6 97 42 + +49 12 67 7 0 +24 79 48 6 85 +38 29 13 11 17 + 1 60 70 34 87 +46 75 64 76 14 + +27 96 15 23 54 +56 39 67 34 76 +43 62 14 7 57 +86 24 35 94 55 +38 51 84 29 16 + +60 33 9 97 20 +92 26 30 42 7 +36 56 65 99 94 +43 86 41 50 15 +80 98 44 96 88 + +86 15 65 31 22 +92 3 40 46 68 +39 64 69 47 74 +87 19 50 34 91 +66 27 2 43 32 + +30 73 45 93 56 +65 82 0 28 60 +77 31 70 46 27 + 7 15 58 76 35 +43 92 91 18 86 + +31 32 76 63 61 +18 40 38 87 3 +33 82 65 93 89 +98 67 78 70 74 + 6 37 48 71 0 + +10 58 67 66 61 +60 13 45 23 96 +48 73 4 63 56 +87 75 94 31 98 +70 97 40 19 86 + + 0 24 58 22 84 +48 36 70 40 33 +94 93 4 77 56 +44 18 45 89 16 +75 35 79 64 6 + + 2 47 41 21 56 +33 44 51 38 13 + 0 29 88 12 66 +64 78 46 67 50 +49 94 80 42 54 + +71 8 90 94 5 +19 43 17 96 16 +73 81 53 61 93 +11 15 78 56 30 +66 87 3 65 52 + +16 92 5 78 42 +56 54 39 87 61 +96 28 29 59 73 + 1 36 8 35 13 +47 32 37 81 38 + +34 89 41 61 28 +73 74 51 63 11 + 6 88 32 13 92 +69 57 33 27 79 +12 35 43 84 44 + +37 84 77 75 19 +22 17 99 85 95 +10 48 36 56 32 +82 29 13 89 2 +16 74 53 43 3 + +87 9 18 33 77 + 7 26 68 46 61 + 5 36 8 96 16 +88 3 92 94 74 +60 15 22 49 43 + +96 94 89 48 55 +84 5 8 83 51 +12 11 40 97 53 +75 62 71 18 63 +16 19 58 82 44 + +31 39 17 45 16 +54 92 95 37 65 +55 30 34 3 59 +41 66 48 56 91 +18 88 61 15 28 + +12 26 96 2 56 +65 9 31 51 17 +78 54 94 80 76 +87 16 30 20 59 +45 64 10 29 71 + +24 26 47 90 97 +82 86 20 17 30 +93 11 41 3 68 +42 52 88 22 57 +83 49 69 0 73 + +55 90 51 38 92 +96 61 50 34 63 +78 72 8 73 85 +25 76 45 89 32 +58 54 1 9 16 + +32 89 12 43 58 +59 6 54 91 17 + 2 37 99 78 45 +57 63 29 90 21 +66 83 34 0 61 + +58 55 63 0 6 +15 90 57 39 56 + 8 76 20 89 30 +61 79 83 70 42 +78 81 43 64 41 + +93 14 57 55 53 +84 0 24 22 54 + 5 90 87 26 13 + 4 46 64 18 17 + 9 58 67 68 92 + +39 76 85 24 9 +36 27 93 64 33 +40 73 31 74 41 + 0 10 57 5 91 + 4 16 59 54 96 + +34 82 54 14 87 +59 21 1 30 60 +27 45 71 58 97 + 4 72 70 85 39 +38 74 96 12 91 + +48 78 3 42 24 +26 85 56 4 80 +35 8 29 93 55 +91 73 7 75 54 + 1 61 88 74 99 + +68 40 41 63 17 +73 61 45 57 66 +14 15 78 0 6 +33 46 47 95 82 +92 48 10 1 70 + +79 19 88 55 81 +40 35 15 63 21 +85 26 57 97 39 +71 24 60 89 22 + 5 27 49 28 38 + + 3 90 23 80 78 +74 89 53 63 14 +48 56 72 71 29 +15 36 45 83 39 +50 44 28 67 97 + +91 22 63 55 26 +69 4 11 42 75 +92 65 48 28 72 +51 79 15 80 68 +98 59 24 64 9 + +48 87 47 81 6 +35 60 59 69 20 +62 99 41 21 63 +51 46 19 12 84 +80 57 28 64 32 + +86 53 52 33 25 +39 90 40 95 88 + 6 61 78 46 91 + 2 74 76 70 89 +18 96 56 12 16 + +65 17 39 45 85 +31 87 63 47 22 +38 1 3 80 20 +25 62 13 12 72 +95 36 11 86 67 + +75 92 82 14 8 +16 20 72 77 23 + 0 61 9 50 18 +96 19 21 63 70 +76 80 53 64 41 + +60 20 69 68 35 +64 9 29 14 15 +49 75 53 88 98 +95 28 7 42 25 + 5 74 80 1 4 + +41 6 58 42 85 +75 65 50 0 7 +82 80 12 5 61 +19 48 21 87 47 +71 14 24 8 23 + +95 81 9 27 75 +93 33 63 89 32 +46 8 59 51 28 +37 6 67 57 52 +68 4 0 44 14 + + 5 88 61 35 85 +68 92 48 74 6 +13 53 55 94 25 +12 15 52 86 96 +23 76 16 45 82 + +54 35 90 57 30 +58 25 97 89 41 +62 75 5 0 94 +86 93 77 37 16 +68 48 33 76 20 + +61 87 30 76 49 +36 39 74 63 23 +92 82 21 45 79 +33 59 57 83 10 + 6 51 93 85 81 + +13 50 17 52 73 +59 38 46 87 96 +35 63 21 3 8 + 6 97 90 23 71 +95 27 66 77 15 + +87 69 71 2 38 + 0 64 14 92 33 +12 46 15 89 97 +48 41 43 52 44 +16 21 74 31 60 + + 6 71 87 35 74 +40 16 19 73 69 + 1 67 42 78 23 +49 59 65 45 53 +48 82 30 72 39 + +39 31 13 2 38 +60 65 18 7 1 +74 23 78 51 4 +50 61 83 94 25 +34 3 80 6 87 + +87 15 42 55 64 +93 30 83 80 46 +24 81 26 31 8 +84 14 67 82 23 +75 22 94 74 40 + +40 21 75 2 78 +25 15 49 61 55 +98 70 92 93 63 +53 1 0 33 32 +12 59 18 44 73 + +78 11 12 58 61 +26 8 51 28 69 +64 35 89 95 1 +20 79 62 13 83 +53 7 84 18 34 diff --git a/4/part1.go b/4/part1.go new file mode 100644 index 0000000..bd0bf02 --- /dev/null +++ b/4/part1.go @@ -0,0 +1,128 @@ +package main + +import ( + "bufio" + "fmt" + "os" + "strconv" + "strings" +) + +const boardSize = 5 + +type Board [][]int + +func (b Board) Mark(num int) { + for x := 0; x < boardSize; x++ { + for y := 0; y < boardSize; y++ { + if b[x][y] == num { + b[x][y] = -1 + } + } + } +} + +func (b Board) CheckWin() bool { + for x := 0; x < boardSize; x++ { + match := true + + for y := 0; y < boardSize; y++ { + if b[x][y] != -1 { + match = false + } + } + + if match { + return true + } + } + + for y := 0; y < boardSize; y++ { + match := true + + for x := 0; x < boardSize; x++ { + if b[x][y] != -1 { + match = false + } + } + + if match { + return true + } + } + + return false +} + +func (b Board) Sum() int { + sum := 0 + + for x := 0; x < boardSize; x++ { + for y := 0; y < boardSize; y++ { + if b[x][y] != -1 { + sum += b[x][y] + } + } + } + + return sum +} + +func NewBoard(lines []string) Board { + var board Board + + for _, line := range lines { + var numbers []int + for i := 0; i < boardSize; i++ { + val, _ := strconv.Atoi(strings.TrimSpace(line[(i*3):(i*3)+2])) + numbers = append(numbers, val) + } + board = append(board, numbers) + } + + return board +} + +func main() { + var numbers []int + var boards []Board + var boardLines []string + + s := bufio.NewScanner(os.Stdin) + for s.Scan() { + // first line should be the numbers + if numbers == nil { + for _, n := range strings.Split(s.Text(), ",") { + val, _ := strconv.Atoi(n) + numbers = append(numbers, val) + } + s.Scan() + continue + } + + // if the line is empty, create new board + // else, collect more lines + if len(s.Bytes()) == 0 { + boards = append(boards, NewBoard(boardLines)) + boardLines = []string{} + } else { + boardLines = append(boardLines, s.Text()) + } + } + // if we have any lines left, convert them into board + if len(boardLines) > 0 { + boards = append(boards, NewBoard(boardLines)) + } + + for _, num := range numbers { + for _, board := range boards { + board.Mark(num) + if board.CheckWin() { + fmt.Println(board.Sum() * num) + os.Exit(0) + } + } + } + + fmt.Println(":(") +} diff --git a/4/part2.go b/4/part2.go new file mode 100644 index 0000000..3bc40c3 --- /dev/null +++ b/4/part2.go @@ -0,0 +1,60 @@ +package main + +import ( + "bufio" + "fmt" + "os" + "strconv" + "strings" +) + +func main() { + var numbers []int + var boards []Board + var boardLines []string + + s := bufio.NewScanner(os.Stdin) + for s.Scan() { + // first line should be the numbers + if numbers == nil { + for _, n := range strings.Split(s.Text(), ",") { + val, _ := strconv.Atoi(n) + numbers = append(numbers, val) + } + s.Scan() + continue + } + + if len(s.Bytes()) == 0 { + boards = append(boards, NewBoard(boardLines)) + boardLines = []string{} + } else { + boardLines = append(boardLines, s.Text()) + } + } + // if we have any lines left, convert them into board + if len(boardLines) > 0 { + boards = append(boards, NewBoard(boardLines)) + } + + for _, num := range numbers { + if len(boards) == 1 { + board := boards[0] + board.Mark(num) + fmt.Println(board.Sum() * num) + os.Exit(0) + } + + // crude filter for not winning boards + var newBoards []Board + for _, board := range boards { + board.Mark(num) + if !board.CheckWin() { + newBoards = append(newBoards, board) + } + } + boards = newBoards + } + + fmt.Println(":(") +} diff --git a/4/testinput b/4/testinput new file mode 100644 index 0000000..49d17bc --- /dev/null +++ b/4/testinput @@ -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