From c8c3c0dfcd1786ee96fd1dad43902c2d0e9f8b8c Mon Sep 17 00:00:00 2001 From: Alek Ratzloff Date: Tue, 7 Dec 2021 11:15:07 -0800 Subject: [PATCH] Add day 4, 5, and 6 Signed-off-by: Alek Ratzloff --- day04/day04.py | 108 +++++++++ day04/example.txt | 19 ++ day04/input.txt | 601 ++++++++++++++++++++++++++++++++++++++++++++++ day05/day05.py | 97 ++++++++ day05/example.txt | 10 + day05/input.txt | 500 ++++++++++++++++++++++++++++++++++++++ day06/day06.py | 47 ++++ day06/example.txt | 1 + day06/input.txt | 1 + 9 files changed, 1384 insertions(+) create mode 100755 day04/day04.py create mode 100644 day04/example.txt create mode 100644 day04/input.txt create mode 100755 day05/day05.py create mode 100644 day05/example.txt create mode 100644 day05/input.txt create mode 100755 day06/day06.py create mode 100644 day06/example.txt create mode 100644 day06/input.txt diff --git a/day04/day04.py b/day04/day04.py new file mode 100755 index 0000000..7e08ceb --- /dev/null +++ b/day04/day04.py @@ -0,0 +1,108 @@ +#!/usr/bin/env python3 +import sys +from typing import Optional, Sequence + + +class Board: + def __init__(self, matrix: Sequence[Sequence[int]]): + self.matrix = matrix + self.marks = [[False] * len(matrix[0]) for _ in range(len(matrix[0]))] + + def mark(self, num: int): + for i, row in enumerate(self.matrix): + for j, col in enumerate(row): + if col == num: + self.marks[i][j] = True + + def row(self, i: int) -> Sequence[int]: + return self.matrix[i] + + def col(self, j: int) -> Sequence[int]: + return [self.matrix[i][j] for i in range(len(self.matrix))] + + def row_marks(self, i: int) -> Sequence[int]: + return self.marks[i] + + def col_marks(self, j: int) -> Sequence[int]: + return [self.marks[i][j] for i in range(len(self.marks))] + + def row_marked(self) -> Optional[int]: + "If an entire row is marked, return that row's index. Else return None." + for i in range(len(self.matrix)): + if all(self.row_marks(i)): + return i + return None + + def col_marked(self) -> Optional[int]: + "If an entire column is marked, return that column's index. Else return None." + for j in range(len(self.matrix[0])): + if all(self.col_marks(j)): + return j + return None + + def all_unmarked(self) -> Sequence[int]: + "Gets a list of all the unmarked numbers on this board" + return [ + self.matrix[i][j] + for i in range(len(self.matrix)) + for j in range(len(self.matrix[i])) + if not self.marks[i][j] + ] + + +def part1(draws: Sequence[int], matrices: Sequence[Sequence[Sequence[int]]]): + boards = [Board(matrix) for matrix in matrices] + result = None + for draw in draws: + # Mark the boards + for board in boards: + board.mark(draw) + # Check to see if there are 5 in a row in any direction + row_num = board.row_marked() + if row_num is not None: + result = sum(board.all_unmarked()) * draw + break + col_num = board.col_marked() + if col_num is not None: + result = sum(board.all_unmarked()) * draw + break + + if result is not None: + break + print(result) + + +def part2(draws: Sequence[int], matrices: Sequence[Sequence[Sequence[int]]]): + boards = [Board(matrix) for matrix in matrices] + result = None + for draw in draws: + # Mark the boards + for board in boards: + board.mark(draw) + # Filter out the winners + if len(boards) == 1: + board = boards[0] + if board.row_marked() is not None or board.col_marked() is not None: + result = sum(board.all_unmarked()) * draw + break + else: + boards = [ + board + for board in boards + if board.row_marked() is None and board.col_marked() is None + ] + print(result) + + +lines = [line.strip() for line in sys.stdin if line.strip()] +draws = [int(draw) for draw in lines.pop(0).split(",")] +# Matrices are 5 lines +matrices = [ + [[int(col) for col in line.split()] for line in lines[i : i + 5]] + for i in range(0, len(lines), 5) +] + +print("Part 1") +part1(draws, matrices) +print("Part 2") +part2(draws, matrices) diff --git a/day04/example.txt b/day04/example.txt new file mode 100644 index 0000000..669a51d --- /dev/null +++ b/day04/example.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 diff --git a/day04/input.txt b/day04/input.txt new file mode 100644 index 0000000..ebec7e9 --- /dev/null +++ b/day04/input.txt @@ -0,0 +1,601 @@ +23,30,70,61,79,49,19,37,64,48,72,34,69,53,15,74,89,38,46,36,28,32,45,2,39,58,11,62,97,40,14,87,96,94,91,92,80,99,6,31,57,98,65,10,33,63,42,17,47,66,26,22,73,27,7,0,55,8,56,29,86,25,4,12,51,60,35,50,5,75,95,44,16,93,21,3,24,52,77,76,43,41,9,84,67,71,83,88,59,68,85,82,1,18,13,78,20,90,81,54 + +50 98 65 14 47 + 0 22 3 83 46 +87 93 81 84 58 +40 35 28 74 48 +45 99 59 37 64 + +85 66 90 32 88 +95 6 4 74 27 + 1 10 70 41 92 +54 36 42 9 39 +60 99 31 67 16 + + 4 44 66 10 58 +33 64 93 42 46 +19 63 6 83 54 +60 51 76 8 30 +71 49 73 7 55 + +17 67 52 61 98 +46 5 4 51 76 +73 59 74 8 33 +48 96 20 26 15 +55 19 86 29 43 + +20 75 12 67 41 +89 36 65 66 92 +40 19 1 0 28 +99 61 85 58 50 +44 72 57 35 86 + +69 87 27 59 33 +47 34 60 93 9 +71 84 46 24 96 +15 91 5 61 19 +57 78 55 31 8 + +19 10 1 81 96 +27 71 2 52 56 +15 22 48 82 34 +64 47 42 49 51 +26 72 61 12 57 + +71 94 40 34 26 +12 80 57 38 55 + 4 56 11 73 49 +75 60 61 9 50 +91 70 23 1 90 + +39 86 30 73 38 + 6 53 58 14 36 +85 12 75 88 5 + 0 29 41 21 15 +47 66 59 54 1 + +99 97 50 17 60 +36 13 29 80 32 +49 85 75 71 15 +10 79 41 61 66 +68 57 55 74 98 + +68 33 87 89 59 +96 35 76 78 55 + 4 63 51 10 65 +58 38 22 54 9 +66 18 37 60 6 + +43 86 50 23 77 +10 42 19 61 2 +40 29 20 84 0 +70 59 96 80 57 +76 12 39 36 6 + +73 43 92 37 99 +36 42 10 77 87 + 3 57 4 20 35 +18 7 46 91 11 +17 98 8 53 61 + +22 37 89 51 9 +71 6 72 87 32 +13 79 86 53 98 +16 2 93 48 38 +63 82 66 61 69 + +73 90 85 54 65 + 9 66 28 5 63 +91 50 70 59 80 +95 68 92 72 67 +69 88 36 43 53 + +36 81 66 78 90 + 2 25 94 82 55 +34 45 1 14 37 +13 4 70 48 75 +67 73 32 18 91 + +33 93 71 48 47 + 8 79 69 53 82 + 5 31 80 45 37 +67 77 41 56 97 +65 46 62 42 81 + +67 70 59 24 88 +84 11 29 52 78 + 4 39 12 90 2 +44 3 10 75 89 +30 93 22 14 8 + +79 60 98 99 49 +23 26 86 91 38 +77 45 95 66 75 +81 42 85 21 3 +40 37 65 20 50 + +12 54 0 86 52 +15 56 29 39 94 +66 79 14 65 26 + 3 4 59 60 40 +47 48 19 13 85 + +32 44 69 90 21 +35 8 1 59 56 +72 71 84 18 11 +96 38 23 37 79 +92 20 33 94 17 + + 1 94 42 21 82 +92 60 9 32 38 +71 3 37 77 18 +89 16 74 76 2 +83 30 28 11 70 + +94 3 1 71 87 + 6 66 19 76 28 +10 86 22 62 2 +67 0 31 46 27 + 8 33 43 92 29 + +35 90 8 30 27 +67 60 82 68 1 + 5 29 93 44 34 +56 65 48 37 51 +57 45 63 94 77 + +67 80 45 57 43 +37 81 25 84 82 +50 8 9 64 7 +29 18 52 16 14 +73 28 11 76 6 + + 5 76 67 18 16 +68 47 15 29 59 +46 32 40 9 84 +30 17 20 22 3 +35 80 38 72 88 + +35 44 14 89 72 +75 67 56 2 3 +58 41 49 12 52 +92 9 22 34 88 +65 39 93 61 47 + +38 67 33 18 60 +34 50 69 31 83 +29 30 9 12 95 +79 2 24 54 87 +46 68 48 58 42 + +61 87 46 26 34 +74 85 9 54 38 +50 29 84 40 4 +49 39 33 99 53 +77 59 0 42 35 + +86 68 23 62 5 +96 92 7 4 1 +50 70 12 83 46 +34 63 91 56 11 +76 90 71 88 95 + +19 18 13 3 62 +42 29 57 79 85 +39 64 14 28 98 +99 36 91 9 63 +69 66 2 17 31 + +51 43 49 98 94 +31 64 53 54 57 + 3 28 10 12 2 +24 99 95 35 17 +76 27 48 0 41 + +80 62 13 38 98 +32 15 16 8 96 +93 43 81 99 40 +20 57 37 24 3 +94 17 70 14 7 + +52 71 49 95 84 +76 38 45 59 89 + 1 7 27 0 98 +92 64 8 50 68 +13 91 26 51 2 + +31 45 25 1 5 +50 68 77 61 53 +74 20 99 38 63 +76 44 15 42 51 +67 87 86 12 24 + +49 0 70 82 9 + 2 24 96 74 60 +68 16 40 32 20 +48 6 98 11 65 +94 10 54 8 95 + +74 41 11 33 76 + 2 10 44 89 23 +56 45 78 60 34 +15 5 26 83 71 +20 72 85 75 54 + +15 59 93 53 8 + 4 10 84 44 36 +17 62 24 27 98 +87 54 73 13 35 + 9 48 52 33 7 + +56 80 70 74 35 +53 69 75 25 27 +47 91 85 62 32 +93 26 89 18 52 +16 73 49 55 77 + +42 40 54 67 73 +11 10 49 35 59 +12 93 37 15 69 +97 41 47 39 2 +75 99 21 29 26 + +23 75 41 10 86 +71 67 66 38 99 +91 92 63 40 28 +69 97 42 77 60 +44 53 12 84 57 + +72 51 31 90 37 +35 89 55 73 87 +46 32 45 0 58 +50 81 13 18 66 +38 4 40 62 22 + +14 48 35 76 83 +13 70 26 4 1 +30 22 91 93 29 +69 41 74 40 63 +80 65 66 72 23 + +23 65 33 56 38 +84 41 34 21 2 + 4 78 27 17 11 +22 53 52 32 80 +24 25 42 91 99 + +54 51 0 23 52 +92 69 10 46 7 +20 35 12 37 73 +19 56 26 79 32 +27 74 34 5 57 + +75 10 24 32 7 +96 54 22 78 5 +23 69 65 43 20 +29 85 44 92 71 +41 87 73 0 48 + +54 92 16 36 37 +42 59 4 9 44 +52 14 12 6 47 +57 38 70 82 0 +53 81 32 35 3 + +17 22 62 80 30 + 8 28 15 42 46 +79 64 32 29 75 + 5 0 9 90 69 +41 71 85 1 6 + +68 89 40 31 39 +32 48 64 38 28 +80 98 88 14 97 + 6 60 52 11 55 +95 34 63 81 4 + +80 33 14 83 68 +78 69 81 59 15 +72 0 74 21 75 +49 6 67 73 64 + 8 25 87 3 45 + +34 97 86 1 79 +49 12 63 10 59 +88 30 84 74 87 +67 47 26 0 57 +71 40 2 76 98 + +15 89 23 65 44 +27 87 54 38 12 +43 29 18 39 94 +48 0 7 57 61 +70 28 60 68 50 + +13 34 49 67 40 +88 74 99 20 26 +63 69 62 24 32 +35 45 96 79 1 +92 7 17 76 30 + +95 21 75 46 74 +39 7 58 23 90 +61 64 37 81 82 +92 36 54 9 53 +17 51 33 10 27 + +67 35 44 22 23 +28 96 1 56 29 + 0 12 5 50 99 +70 42 8 24 25 +39 53 51 89 85 + +50 15 94 84 27 +72 26 51 3 85 +63 45 1 64 44 +17 80 13 88 2 +12 97 91 25 18 + +59 14 9 67 63 + 6 18 26 98 50 +86 74 75 56 34 +48 7 99 20 64 + 8 53 10 15 57 + + 6 35 13 68 24 +90 19 91 71 86 +95 58 10 44 98 + 8 41 60 1 16 +29 59 43 84 48 + +48 56 8 74 4 +66 30 77 35 90 +94 0 75 49 84 + 5 39 11 54 87 +33 58 96 22 2 + + 5 38 57 63 65 +74 58 22 8 81 +45 96 78 3 11 +28 42 30 39 51 +87 33 34 75 14 + +56 34 67 70 17 + 7 80 10 31 85 +68 59 63 74 40 +13 81 99 62 6 +92 84 71 37 39 + +85 99 74 16 10 +12 21 91 2 83 + 4 94 38 51 36 +41 97 45 65 24 +50 82 92 52 35 + +28 65 6 13 23 + 7 57 86 18 67 +26 85 29 22 89 +99 62 94 31 96 +14 17 50 56 9 + +98 10 63 4 8 +46 21 58 89 3 +27 12 11 55 16 +61 38 43 33 54 +53 14 99 31 25 + +25 70 24 40 14 +75 82 58 68 41 +22 71 72 93 1 +47 97 6 81 45 +92 42 2 76 12 + +31 84 30 0 85 +55 70 72 45 57 +78 52 67 60 22 +43 32 8 44 34 +14 64 91 89 18 + +70 19 62 16 56 +84 49 41 3 20 +85 5 76 95 22 +63 55 37 31 72 +42 17 28 65 1 + +85 17 57 62 48 +34 29 69 52 28 +90 64 54 21 38 + 0 50 84 44 60 +93 80 75 89 83 + +39 84 78 12 5 +29 4 35 7 85 +73 25 58 27 45 +22 90 91 47 74 +60 96 15 24 26 + +13 30 82 31 43 +23 71 1 51 36 +40 62 25 54 86 + 8 83 2 47 34 +33 41 27 98 24 + +13 25 53 50 56 +77 4 41 19 22 +68 70 75 9 65 +30 33 60 74 80 +31 83 34 79 11 + +11 90 38 78 73 +17 16 14 37 4 +80 68 21 70 92 +47 26 81 67 25 +10 31 23 41 22 + +90 62 2 50 79 +77 51 8 11 13 +32 29 43 88 33 +39 34 89 45 23 +91 9 6 68 3 + +62 70 89 27 87 +45 65 96 80 29 + 1 54 90 68 16 +72 50 28 95 12 +21 71 81 10 60 + +33 14 60 44 78 + 6 65 87 11 8 +79 21 59 35 19 +26 69 67 42 27 +25 36 80 10 45 + +71 24 80 87 56 + 7 61 43 38 18 +52 46 41 28 48 + 0 74 20 34 63 + 3 84 42 85 9 + +36 64 41 7 49 +91 92 13 94 88 +73 98 79 0 12 +76 66 86 67 9 + 2 85 74 5 34 + + 8 81 7 56 28 +36 13 42 29 75 +12 27 85 45 9 +26 25 62 41 22 +79 11 95 0 24 + +72 76 81 67 16 +96 41 94 58 7 + 0 79 38 27 11 +61 36 56 88 39 +89 63 31 75 8 + +62 51 5 46 28 +77 97 89 86 13 +87 55 73 90 57 +84 44 40 49 34 +25 0 58 6 21 + + 7 56 15 41 94 +42 89 16 18 74 +57 79 96 35 3 +14 45 20 19 80 +87 85 28 69 17 + +27 88 54 62 65 +44 93 69 13 9 +85 63 43 11 47 +83 57 30 20 56 +71 46 49 7 77 + +45 24 75 39 69 +48 74 44 49 64 +65 25 22 46 93 +88 52 27 37 50 +19 35 47 54 67 + +44 32 71 13 57 + 7 38 26 98 65 +46 1 21 8 55 +30 62 92 27 3 +69 50 99 85 11 + +86 6 64 34 97 +47 98 7 38 9 +26 68 75 92 54 +58 42 13 78 37 +85 28 81 16 51 + +82 74 15 4 86 +55 0 70 88 24 +50 79 63 40 21 +47 39 61 49 36 +89 16 13 2 37 + +89 19 9 82 13 +84 34 58 56 10 +27 92 46 4 94 +44 24 52 86 55 +39 23 22 99 5 + +65 92 8 86 77 +98 79 72 28 78 +16 23 3 55 48 +68 95 66 30 43 +50 31 15 11 45 + +32 70 25 59 31 +47 68 77 56 23 +66 78 54 88 50 +55 60 58 89 83 +84 99 86 97 95 + +53 46 1 94 87 + 8 80 38 77 81 +17 51 47 19 69 +86 50 71 5 93 +61 66 36 58 0 + +90 58 17 29 92 +67 1 8 64 99 +63 22 57 19 68 +78 36 93 53 2 +27 48 62 39 14 + + 8 49 22 90 54 +26 4 99 27 34 +78 25 11 85 28 +31 42 36 53 15 +64 75 60 45 35 + +99 84 26 53 90 +61 51 98 39 86 +47 37 52 80 63 +67 49 35 70 11 +32 45 94 73 43 + +91 92 74 94 32 +27 56 50 33 54 +67 46 35 25 10 +93 97 30 90 4 +57 15 69 83 39 + +71 68 74 81 11 +44 98 60 17 73 +43 40 32 38 39 +61 56 97 94 70 +23 2 86 91 54 + +19 98 93 42 88 + 0 16 30 32 71 +89 86 81 76 68 +29 2 14 72 63 + 7 27 67 59 1 + +24 18 28 98 95 +10 62 80 71 36 + 3 89 20 63 46 +47 65 84 22 6 +82 19 81 38 45 + +54 85 67 34 79 +25 58 38 73 61 +72 98 4 19 40 +32 10 29 31 89 +15 33 5 7 63 + +49 48 71 81 88 +70 5 39 41 22 +19 20 7 75 23 +69 46 63 14 54 +80 45 94 6 55 + +88 62 76 78 95 +64 65 36 58 22 + 7 21 98 93 42 +79 99 9 89 10 + 6 5 33 92 72 diff --git a/day05/day05.py b/day05/day05.py new file mode 100755 index 0000000..221a159 --- /dev/null +++ b/day05/day05.py @@ -0,0 +1,97 @@ +#!/usr/bin/env python3 +from collections import defaultdict +import dataclasses +import sys +from typing import DefaultDict, Generator, Sequence, Tuple +import re +from itertools import repeat +import pprint + + +SEG_PAT = re.compile(r"(?P[\d]+),(?P[\d]+) -> (?P[\d]+),(?P[\d]+)") + + +@dataclasses.dataclass +class Seg: + x1: int + y1: int + x2: int + y2: int + + @staticmethod + def from_str(s: str) -> "Seg": + match = SEG_PAT.match(s) + assert match + return Seg( + int(match["x1"]), int(match["y1"]), int(match["x2"]), int(match["y2"]) + ) + + def points(self, diag: bool) -> Generator[Tuple[int, int], None, None]: + if self.x1 == self.x2: + # We're going up and down + if self.y1 > self.y2: + lo = self.y2 + hi = self.y1 + else: + lo = self.y1 + hi = self.y2 + yield from zip(repeat(self.x1), range(lo, hi + 1)) + elif self.y1 == self.y2: + # We're going side to side + if self.x1 > self.x2: + lo = self.x2 + hi = self.x1 + else: + lo = self.x1 + hi = self.x2 + yield from zip(range(lo, hi + 1), repeat(self.y1)) + elif diag: + # Figure out the slope + if self.x1 > self.x2: + # right to left + lox = self.x2 + hix = self.x1 + loy = self.y2 + hiy = self.y1 + else: + lox = self.x1 + hix = self.x2 + loy = self.y1 + hiy = self.y2 + slope = float(hiy - loy) / float(hix - lox) + yf = float(loy) + for x in range(lox, hix + 1): + yield (x, int(yf)) + yf += slope + else: + # No support for diagonals + return None + + +def part1(segments: Sequence[Seg]): + # Create a counting dict, we can probably avoid the matrix stuff + points: DefaultDict[Tuple[int, int], int] = defaultdict(lambda: 0) + for seg in segments: + for point in seg.points(diag=False): + points[point] += 1 + # Figure out how many locations where at least 2 lines overlap + count = len([overlap for overlap in points.values() if overlap > 1]) + print(f"{count} overlaps >1") + + +def part2(segments: Sequence[Seg]): + # Create a counting dict, we can probably avoid the matrix stuff + points: DefaultDict[Tuple[int, int], int] = defaultdict(lambda: 0) + for seg in segments: + for point in seg.points(diag=True): + points[point] += 1 + # Figure out how many locations where at least 2 lines overlap + count = len([overlap for overlap in points.values() if overlap > 1]) + print(f"{count} overlaps >1") + + +segments = [Seg.from_str(line) for line in sys.stdin] +print("Part 1") +part1(segments) +print("Part 2") +part2(segments) diff --git a/day05/example.txt b/day05/example.txt new file mode 100644 index 0000000..b258f68 --- /dev/null +++ b/day05/example.txt @@ -0,0 +1,10 @@ +0,9 -> 5,9 +8,0 -> 0,8 +9,4 -> 3,4 +2,2 -> 2,1 +7,0 -> 7,4 +6,4 -> 2,0 +0,9 -> 2,9 +3,4 -> 1,4 +0,0 -> 8,8 +5,5 -> 8,2 diff --git a/day05/input.txt b/day05/input.txt new file mode 100644 index 0000000..39de4e8 --- /dev/null +++ b/day05/input.txt @@ -0,0 +1,500 @@ +419,207 -> 419,109 +300,888 -> 803,385 +104,959 -> 457,959 +987,951 -> 987,385 +173,602 -> 919,602 +173,70 -> 305,70 +341,19 -> 486,19 +128,579 -> 128,100 +210,867 -> 969,867 +880,493 -> 880,58 +937,831 -> 131,25 +520,921 -> 476,965 +760,147 -> 461,147 +646,108 -> 646,27 +99,906 -> 99,591 +19,956 -> 19,273 +89,201 -> 326,201 +275,948 -> 962,261 +292,489 -> 689,489 +674,109 -> 20,763 +861,861 -> 529,529 +155,200 -> 273,200 +628,803 -> 209,384 +654,401 -> 578,325 +723,625 -> 828,730 +137,406 -> 862,406 +893,45 -> 41,897 +631,10 -> 941,320 +618,435 -> 537,435 +939,29 -> 30,938 +505,796 -> 505,244 +799,779 -> 77,779 +938,576 -> 427,576 +522,635 -> 405,518 +244,89 -> 946,89 +447,791 -> 316,660 +560,731 -> 687,731 +16,878 -> 835,59 +45,707 -> 45,565 +767,166 -> 404,529 +791,260 -> 791,950 +373,949 -> 373,156 +38,774 -> 38,557 +445,537 -> 445,370 +817,756 -> 959,898 +472,551 -> 952,71 +696,381 -> 657,420 +43,829 -> 43,190 +101,635 -> 728,635 +197,532 -> 140,532 +693,368 -> 299,368 +433,140 -> 433,610 +136,58 -> 136,666 +472,294 -> 886,294 +690,883 -> 671,864 +141,598 -> 141,118 +56,651 -> 56,957 +747,82 -> 747,91 +219,455 -> 55,291 +444,131 -> 444,802 +326,459 -> 528,661 +245,965 -> 143,965 +916,316 -> 630,316 +263,55 -> 977,769 +262,451 -> 587,451 +960,178 -> 960,564 +960,88 -> 476,572 +314,259 -> 314,169 +404,742 -> 429,742 +830,921 -> 409,921 +181,396 -> 463,678 +338,293 -> 23,608 +851,667 -> 851,350 +181,859 -> 718,322 +314,240 -> 870,796 +778,984 -> 77,283 +476,178 -> 440,178 +935,357 -> 841,263 +695,683 -> 414,964 +760,241 -> 306,241 +390,355 -> 791,355 +460,710 -> 851,710 +559,448 -> 870,448 +161,526 -> 301,386 +935,495 -> 633,193 +205,536 -> 383,536 +290,626 -> 290,94 +55,972 -> 946,81 +240,531 -> 631,922 +189,806 -> 573,806 +518,827 -> 866,479 +239,829 -> 260,829 +151,51 -> 849,51 +301,736 -> 532,736 +23,889 -> 336,889 +284,124 -> 284,933 +637,610 -> 67,40 +610,828 -> 610,159 +763,590 -> 763,963 +804,576 -> 804,694 +689,872 -> 82,265 +440,377 -> 190,127 +933,330 -> 310,953 +873,99 -> 873,328 +756,808 -> 860,808 +119,64 -> 928,873 +74,144 -> 489,559 +957,938 -> 838,938 +148,320 -> 932,320 +386,171 -> 386,985 +357,171 -> 494,171 +254,67 -> 254,95 +196,910 -> 827,910 +107,114 -> 758,114 +971,40 -> 801,40 +504,602 -> 215,891 +184,310 -> 720,846 +280,300 -> 955,975 +49,637 -> 49,572 +352,512 -> 739,899 +610,123 -> 585,123 +808,881 -> 758,881 +646,980 -> 818,980 +948,482 -> 384,482 +115,144 -> 852,881 +506,836 -> 547,836 +985,369 -> 374,980 +883,975 -> 48,975 +447,664 -> 312,799 +24,597 -> 24,331 +45,19 -> 979,953 +210,689 -> 210,430 +704,806 -> 704,612 +985,982 -> 124,121 +70,174 -> 550,174 +463,12 -> 637,12 +107,97 -> 716,97 +935,265 -> 390,810 +42,223 -> 42,86 +60,245 -> 60,473 +695,735 -> 208,735 +547,265 -> 802,265 +941,667 -> 941,806 +250,286 -> 611,286 +10,64 -> 630,64 +482,889 -> 482,150 +441,820 -> 776,820 +529,474 -> 529,265 +533,465 -> 217,149 +242,473 -> 242,830 +633,160 -> 476,317 +942,24 -> 942,784 +80,313 -> 92,325 +295,109 -> 295,712 +31,964 -> 857,138 +285,255 -> 955,925 +650,610 -> 650,366 +722,586 -> 625,586 +580,384 -> 580,531 +78,407 -> 896,407 +296,310 -> 730,744 +717,966 -> 924,966 +524,551 -> 524,671 +44,127 -> 784,867 +214,849 -> 238,849 +749,320 -> 749,241 +886,146 -> 336,696 +889,933 -> 455,499 +644,232 -> 79,797 +400,979 -> 626,979 +433,681 -> 433,523 +447,57 -> 676,57 +185,416 -> 659,890 +849,645 -> 257,53 +633,721 -> 633,901 +766,355 -> 766,56 +669,393 -> 669,523 +833,336 -> 833,58 +52,114 -> 52,413 +699,957 -> 109,957 +14,953 -> 945,22 +641,15 -> 929,303 +25,874 -> 866,33 +856,73 -> 28,901 +94,892 -> 592,892 +256,357 -> 256,700 +960,579 -> 31,579 +940,859 -> 940,987 +507,673 -> 820,986 +164,361 -> 133,361 +210,424 -> 876,424 +28,186 -> 28,376 +452,149 -> 531,149 +142,160 -> 142,435 +180,801 -> 180,439 +681,267 -> 42,267 +724,414 -> 786,476 +762,492 -> 762,427 +902,808 -> 227,133 +70,923 -> 821,172 +468,12 -> 457,12 +208,129 -> 986,907 +78,786 -> 78,352 +573,869 -> 820,869 +780,680 -> 520,940 +276,66 -> 276,244 +423,629 -> 592,629 +888,507 -> 888,139 +869,878 -> 869,951 +274,614 -> 625,965 +926,289 -> 982,233 +102,687 -> 102,214 +52,264 -> 52,12 +904,43 -> 657,43 +184,685 -> 184,628 +506,912 -> 601,817 +356,524 -> 87,524 +202,260 -> 202,276 +970,63 -> 83,950 +402,332 -> 950,880 +195,666 -> 843,666 +13,82 -> 892,961 +614,28 -> 614,871 +892,162 -> 892,101 +363,665 -> 59,665 +768,208 -> 410,208 +483,300 -> 295,300 +590,108 -> 881,108 +837,967 -> 837,326 +368,731 -> 368,913 +900,921 -> 873,921 +896,931 -> 848,979 +562,939 -> 857,939 +85,351 -> 598,351 +917,30 -> 624,30 +605,314 -> 605,303 +382,655 -> 340,697 +949,115 -> 653,115 +667,311 -> 370,608 +971,983 -> 39,51 +178,687 -> 178,69 +906,197 -> 296,807 +947,886 -> 383,322 +551,667 -> 551,238 +86,65 -> 916,895 +589,887 -> 865,611 +332,53 -> 84,53 +361,148 -> 55,148 +883,205 -> 661,205 +415,552 -> 52,552 +46,42 -> 46,952 +955,13 -> 39,929 +677,482 -> 208,482 +414,268 -> 927,268 +101,509 -> 101,149 +946,971 -> 139,164 +223,597 -> 223,517 +805,896 -> 796,896 +565,875 -> 878,875 +472,431 -> 472,732 +643,15 -> 643,202 +618,725 -> 618,284 +376,532 -> 376,120 +807,981 -> 415,981 +716,401 -> 61,401 +893,955 -> 743,805 +264,935 -> 264,677 +586,908 -> 638,908 +780,277 -> 780,418 +234,410 -> 428,410 +899,214 -> 899,703 +948,51 -> 948,509 +238,664 -> 879,23 +20,877 -> 638,877 +146,738 -> 109,738 +670,893 -> 524,893 +317,423 -> 27,713 +91,600 -> 477,986 +902,63 -> 902,797 +647,839 -> 647,667 +227,358 -> 236,349 +985,541 -> 660,866 +86,562 -> 86,949 +368,851 -> 863,356 +327,905 -> 57,635 +561,275 -> 781,495 +196,65 -> 626,65 +110,688 -> 720,78 +720,472 -> 115,472 +817,135 -> 817,876 +752,387 -> 752,104 +78,127 -> 635,684 +812,170 -> 155,170 +606,718 -> 804,916 +843,494 -> 979,494 +919,346 -> 454,346 +866,828 -> 818,828 +114,115 -> 114,250 +895,308 -> 370,308 +665,893 -> 690,893 +939,275 -> 741,275 +290,321 -> 290,910 +747,327 -> 107,967 +734,715 -> 391,372 +368,497 -> 506,359 +773,945 -> 391,563 +772,537 -> 733,537 +271,679 -> 488,679 +665,745 -> 665,984 +143,177 -> 685,719 +671,860 -> 147,860 +674,365 -> 857,182 +343,74 -> 985,716 +284,46 -> 180,46 +595,800 -> 20,225 +57,278 -> 792,278 +649,285 -> 165,769 +600,24 -> 600,116 +862,939 -> 862,871 +153,917 -> 682,388 +117,884 -> 257,884 +726,763 -> 531,763 +810,985 -> 899,985 +718,942 -> 718,466 +674,19 -> 674,203 +117,677 -> 117,918 +928,261 -> 928,945 +719,390 -> 719,321 +822,601 -> 484,263 +725,793 -> 725,111 +201,745 -> 588,745 +404,889 -> 908,385 +981,39 -> 610,410 +148,426 -> 711,989 +128,260 -> 319,451 +325,306 -> 325,585 +557,415 -> 557,745 +915,101 -> 648,101 +104,636 -> 104,520 +93,964 -> 641,416 +201,709 -> 201,90 +921,571 -> 798,571 +313,624 -> 313,510 +343,649 -> 28,649 +688,246 -> 24,910 +696,610 -> 353,610 +126,310 -> 126,394 +457,98 -> 457,981 +277,707 -> 277,531 +943,721 -> 37,721 +959,295 -> 702,295 +23,547 -> 891,547 +209,114 -> 931,836 +737,174 -> 737,195 +208,890 -> 115,797 +170,401 -> 726,401 +11,218 -> 11,297 +989,10 -> 10,989 +866,86 -> 487,86 +867,31 -> 867,334 +846,414 -> 861,414 +478,315 -> 478,697 +572,843 -> 731,843 +657,12 -> 161,508 +903,194 -> 142,955 +612,321 -> 147,786 +813,920 -> 259,920 +834,389 -> 651,206 +824,153 -> 824,557 +399,871 -> 115,871 +270,785 -> 270,120 +469,640 -> 753,640 +620,132 -> 620,175 +620,234 -> 666,234 +594,409 -> 948,55 +670,323 -> 670,89 +262,65 -> 262,379 +879,617 -> 284,22 +493,423 -> 761,423 +17,931 -> 906,42 +512,494 -> 473,494 +122,230 -> 122,87 +15,207 -> 533,207 +216,183 -> 50,183 +360,107 -> 280,107 +403,841 -> 941,841 +913,442 -> 500,29 +864,947 -> 864,85 +500,516 -> 634,382 +283,20 -> 669,20 +916,770 -> 176,30 +966,73 -> 252,787 +847,841 -> 171,165 +163,219 -> 766,219 +482,515 -> 275,308 +528,949 -> 240,949 +725,574 -> 847,696 +109,131 -> 109,538 +655,837 -> 476,837 +803,631 -> 803,51 +977,83 -> 149,911 +207,231 -> 171,231 +617,29 -> 617,294 +838,708 -> 446,708 +711,597 -> 612,498 +975,942 -> 279,246 +315,128 -> 315,293 +146,962 -> 873,235 +448,180 -> 54,180 +177,680 -> 866,680 +891,265 -> 741,265 +656,949 -> 414,949 +909,456 -> 196,456 +574,286 -> 58,286 +861,691 -> 861,383 +779,351 -> 779,827 +459,989 -> 459,350 +936,480 -> 936,699 +645,309 -> 348,606 +861,62 -> 621,302 +568,324 -> 568,358 +889,221 -> 889,335 +538,759 -> 538,266 +780,736 -> 780,827 +866,518 -> 983,401 +67,871 -> 840,98 +432,664 -> 664,664 +146,24 -> 755,24 +964,585 -> 964,770 +372,144 -> 809,144 +688,827 -> 867,827 +137,916 -> 137,942 +846,131 -> 846,46 +764,21 -> 457,328 +140,66 -> 799,725 +703,224 -> 83,844 +557,67 -> 557,681 +355,544 -> 764,135 +625,893 -> 126,394 +842,214 -> 842,322 +582,778 -> 582,762 +341,861 -> 341,859 +143,767 -> 52,858 +114,109 -> 114,200 +394,210 -> 396,212 +861,353 -> 861,652 +873,553 -> 62,553 +44,962 -> 984,22 +734,56 -> 734,828 +798,516 -> 950,516 +367,755 -> 367,618 +868,637 -> 868,780 +192,952 -> 192,734 +603,109 -> 705,211 +12,17 -> 984,989 +910,147 -> 910,620 +515,749 -> 515,517 +775,136 -> 761,150 +662,636 -> 662,21 +894,490 -> 310,490 +956,732 -> 297,73 +514,99 -> 140,99 +308,419 -> 691,419 +485,86 -> 485,187 +737,783 -> 979,783 +90,76 -> 869,855 +959,112 -> 84,112 +879,494 -> 879,257 +933,425 -> 933,619 +64,391 -> 64,21 +106,305 -> 253,452 +324,152 -> 853,152 +666,225 -> 39,852 +370,904 -> 257,791 +592,845 -> 592,15 +936,971 -> 267,302 +147,210 -> 62,210 +308,323 -> 495,323 +212,918 -> 110,918 +229,392 -> 685,848 +896,132 -> 326,702 +483,143 -> 605,265 +251,317 -> 130,317 +758,93 -> 445,93 +156,286 -> 458,286 +401,904 -> 383,904 +244,256 -> 851,256 +928,411 -> 612,411 +642,920 -> 642,420 +494,707 -> 494,225 +87,112 -> 87,256 +972,907 -> 83,18 +139,104 -> 139,761 +493,725 -> 493,529 +981,145 -> 459,667 +390,240 -> 702,240 +466,982 -> 807,982 +320,143 -> 692,515 +477,649 -> 477,206 +456,254 -> 456,578 diff --git a/day06/day06.py b/day06/day06.py new file mode 100755 index 0000000..95da91f --- /dev/null +++ b/day06/day06.py @@ -0,0 +1,47 @@ +#!/usr/bin/env python3 +import sys +from typing import Sequence +from copy import deepcopy + + +def part1(fish: Sequence[int], count: int): + for i in range(count): + new = [] + for f in fish: + if f == 0: + new += [8] + f = 7 + f -= 1 + new += [f] + fish = new + print(len(fish)) + + +def part2(init: Sequence[int], count: int): + # This is a mapping for the number of fish by index, 0-8 + fish = [0] * 9 + for i in init: + fish[i] += 1 + # Go through each day, rotating everything to the left + for _ in range(count): + zeroes = fish[0] + # rotate + for i in range(1, len(fish)): + fish[i - 1] = fish[i] + # position 8 + fish[len(fish) - 1] = zeroes + # position 6 + fish[len(fish) - 3] += zeroes + print(sum(fish)) + + +fish = [int(state) for state in sys.stdin.readline().strip().split(",") if state] + +print("Part 1 (80 days)") +# part1(fish, 80) +# Part 2 function does the same thing except much faster +part2(fish, 80) +print(len(fish)) + +print("Part 2 (256 days)") +part2(fish, 256) diff --git a/day06/example.txt b/day06/example.txt new file mode 100644 index 0000000..a7af2b1 --- /dev/null +++ b/day06/example.txt @@ -0,0 +1 @@ +3,4,3,1,2 \ No newline at end of file diff --git a/day06/input.txt b/day06/input.txt new file mode 100644 index 0000000..b53f931 --- /dev/null +++ b/day06/input.txt @@ -0,0 +1 @@ +1,3,3,4,5,1,1,1,1,1,1,2,1,4,1,1,1,5,2,2,4,3,1,1,2,5,4,2,2,3,1,2,3,2,1,1,4,4,2,4,4,1,2,4,3,3,3,1,1,3,4,5,2,5,1,2,5,1,1,1,3,2,3,3,1,4,1,1,4,1,4,1,1,1,1,5,4,2,1,2,2,5,5,1,1,1,1,2,1,1,1,1,3,2,3,1,4,3,1,1,3,1,1,1,1,3,3,4,5,1,1,5,4,4,4,4,2,5,1,1,2,5,1,3,4,4,1,4,1,5,5,2,4,5,1,1,3,1,3,1,4,1,3,1,2,2,1,5,1,5,1,3,1,3,1,4,1,4,5,1,4,5,1,1,5,2,2,4,5,1,3,2,4,2,1,1,1,2,1,2,1,3,4,4,2,2,4,2,1,4,1,3,1,3,5,3,1,1,2,2,1,5,2,1,1,1,1,1,5,4,3,5,3,3,1,5,5,4,4,2,1,1,1,2,5,3,3,2,1,1,1,5,5,3,1,4,4,2,4,2,1,1,1,5,1,2,4,1,3,4,4,2,1,4,2,1,3,4,3,3,2,3,1,5,3,1,1,5,1,2,2,4,4,1,2,3,1,2,1,1,2,1,1,1,2,3,5,5,1,2,3,1,3,5,4,2,1,3,3,4 \ No newline at end of file