diff options
author | Brian Chu <brianmchu42@gmail.com> | 2021-12-04 12:49:24 -0800 |
---|---|---|
committer | Brian Chu <brianmchu42@gmail.com> | 2021-12-04 12:49:24 -0800 |
commit | f248d715d4a3c19f84475fcb6d076a6874fcf7e0 (patch) | |
tree | 6d4c3a75be551e16f41bc7fde6002d6b8e407975 | |
download | AdventOfCode2021-f248d715d4a3c19f84475fcb6d076a6874fcf7e0.tar.gz |
commit for days 1 through 4
-rw-r--r-- | .gitignore | 1 | ||||
-rw-r--r-- | day1.py | 39 | ||||
-rw-r--r-- | day2.py | 29 | ||||
-rw-r--r-- | day3.py | 52 | ||||
-rw-r--r-- | day4.py | 55 |
5 files changed, 176 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..2211df6 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +*.txt diff --git a/day1.py b/day1.py new file mode 100644 index 0000000..4cfb292 --- /dev/null +++ b/day1.py @@ -0,0 +1,39 @@ +#!/usr/bin/env python + +curr = None +count = 0 + +with open("day1.txt") as input: + for line in input: + line = int(line) + if not curr: + curr = line + continue + if line > curr: + count += 1 + curr = line + +print(count) + + +curr = None +count = 0 +with open("day1.txt") as input: + first = int(next(input)) + second = int(next(input)) + third = int(next(input)) + while True: + if not curr: + curr = first + second + third + continue + if first + second + third > curr: + count += 1 + curr = first + second + third + try: + first = second + second = third + third = int(next(input)) + except StopIteration: + break + +print(count) diff --git a/day2.py b/day2.py new file mode 100644 index 0000000..f82eb3a --- /dev/null +++ b/day2.py @@ -0,0 +1,29 @@ +#!/usr/bin/env python + +depth = 0 +dist = 0 +with open("day2.txt") as data: + for line in data: + match line.split(): + case ["forward", x]: + dist += int(x) + case ["up", x]: + depth -= int(x) + case ["down", x]: + depth += int(x) + print(depth * dist) + +depth = 0 +dist = 0 +aim = 0 +with open("day2.txt") as data: + for line in data: + match line.split(): + case ["forward", x]: + dist += int(x) + depth += aim * int(x) + case ["up", x]: + aim -= int(x) + case ["down", x]: + aim += int(x) + print(depth * dist) diff --git a/day3.py b/day3.py new file mode 100644 index 0000000..f969550 --- /dev/null +++ b/day3.py @@ -0,0 +1,52 @@ +#!/usr/bin/env python +import numpy as np + +with open("day3.txt") as data: + counts = np.array([0] * 12) + for i, line in enumerate(data): + line = np.array([num for num in map(int, list(line.strip()))]) + counts += line + gamma = (counts > i/2).astype(int).tolist() + epsilon = (counts < i/2).astype(int).tolist() + + gamma = int("".join(str(x) for x in gamma), 2) + epsilon = int("".join(str(x) for x in epsilon), 2) + print(gamma * epsilon) + + +# part 2 + +with open("day3.txt") as data: + # load data into numpy array + inputs = np.zeros((1000, 12)).astype(int) + for i, line in enumerate(data): + line = np.array([num for num in map(int, list(line.strip()))]) + inputs[i] = line + + oxygen = inputs.copy() + for i in range(12): + counts = np.bincount(oxygen[:, i]) + if counts[0] == counts[1]: + bit = 1 + else: + bit = np.argmax(counts) + oxygen = oxygen[oxygen[:, i] == bit] + + oxygen = int("".join(str(x) for x in oxygen[0].tolist()), 2) + + co2 = inputs.copy() + for i in range(12): + counts = np.bincount(co2[:, i]) + print(counts) + if len(counts) < 2 or counts[0] == counts[1]: + bit = 0 + else: + bit = np.argmin(counts) + bit = np.argmin(np.bincount(co2[:, i])) + co2 = co2[co2[:, i] == bit] + if len(counts) < 2: + break + + co2 = int("".join(str(x) for x in co2[0].tolist()), 2) + + print(oxygen * co2) diff --git a/day4.py b/day4.py new file mode 100644 index 0000000..6cf430c --- /dev/null +++ b/day4.py @@ -0,0 +1,55 @@ +#!/usr/bin/env python + +import numpy as np + +def bingo(board): + for i in range(5): + if np.all(board[:, i] == 1) or np.all(board[i, :] == 1): + return True + return False + +def any_bingo(boards): + for index, board in enumerate(boards): + if bingo(board): + yield index + yield -1 + +with open("day4.txt") as data: + order = next(data).strip().split(',') + next(data) + boards = [] + current_board = [] + for line in data: + if line == '\n': + boards.append(current_board) + current_board = [] + else: + current_board.append(line.strip().split()) + boards = np.array(boards).astype(int) + print(boards.shape) + marked = np.zeros_like(boards) + # part 1 + for num in map(int, order): + indices = np.where(boards == num) + marked[indices] = 1 + if (board_num := next(any_bingo(marked))) != -1: + ind = np.where(marked[board_num] == 0) + total = np.sum(boards[board_num][ind]) + print(total * num) + break + # part 2 + marked[:] = 0 + won = None + for num in map(int, order): + indices = np.where(boards == num) + marked[indices] = 1 + nextwon = {index for index in any_bingo(marked) if index != -1} + if len(nextwon) == 99: + diff = (nextwon - won).pop() + print(diff) + ind = np.where(marked[diff] == 0) + total = np.sum(boards[diff][ind]) + print(total*num) + break + else: + won = nextwon |