From 122d1c3710447dcf5ea84a0f77497cb066f04544 Mon Sep 17 00:00:00 2001 From: Maximilian Friedersdorff Date: Sun, 5 Dec 2021 22:26:11 +0000 Subject: [PATCH] Finish up to day 4 --- 3_2.py | 73 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 4_1.py | 67 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 4_2.py | 74 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 214 insertions(+) create mode 100644 3_2.py create mode 100644 4_1.py create mode 100644 4_2.py diff --git a/3_2.py b/3_2.py new file mode 100644 index 0000000..71a9459 --- /dev/null +++ b/3_2.py @@ -0,0 +1,73 @@ +"""Life support rating""" + +def most_common(digits): + ones = 0 + for d in digits: + if d == '1': + ones += 1 + + if ones >= len(digits)/2: + return 1 + else: + return 0 + + +def least_common(digits): + ones = 0 + for d in digits: + if d == '1': + ones += 1 + + if ones >= len(digits)/2: + return 0 + else: + return 1 + + +with open("3_1_input.txt") as f: + all_lines = [l.strip() for l in f] + n_digits = len(all_lines[0]) + + +lines = all_lines +to_keep = [] +for i in range(n_digits): + print(len(list(zip(*lines)))) + digits = list(zip(*lines))[i] + mc = str(most_common(digits)) + + for line in lines: + if line[i] == mc: + to_keep.append(line) + + lines = to_keep + to_keep = [] + + if len(lines) == 1: + oxygen = lines[0] + print("Oxygen", lines[0]) + break + +lines = all_lines +to_keep = [] +for i in range(n_digits): + digits = list(zip(*lines))[i] + lc = str(least_common(digits)) + + for line in lines: + if line[i] == lc: + to_keep.append(line) + + lines = to_keep + to_keep = [] + + if len(lines) == 1: + co2 = lines[0] + print("CO2", lines[0]) + break + + +oxygen_b10 = int(oxygen, base=2) +co2_b10 = int(co2, base=2) + +print(oxygen_b10 * co2_b10) diff --git a/4_1.py b/4_1.py new file mode 100644 index 0000000..9f5a69b --- /dev/null +++ b/4_1.py @@ -0,0 +1,67 @@ +"""Play bingo""" +import sys + +class BingoBoard(): + def __init__(self, lines): + self.board = [] + self.called = [ + [False] * 5, + [False] * 5, + [False] * 5, + [False] * 5, + [False] * 5, + ] + for line in lines: + self.board.append([int(n) for n in line.strip().split()]) + + def number_called(self, number): + for i, row in enumerate(self.board): + for j, n in enumerate(row): + if n == number: + self.called[i][j] = True + + return self.won() + + return False + + def won(self): + for i, row in enumerate(self.called): + if row == [True] * 5: + return True + + cols_called = list(zip(*self.called)) + cols_board = list(zip(*self.board)) + for j, col in enumerate(cols_called): + if col == tuple([True] * 5): + return True + + return False + + def get_score(self): + score = 0 + for i in range(5): + for j in range(5): + if not self.called[i][j]: + score += self.board[i][j] + print(self.board[i][j]) + + return score + + +bingo_boards = [] +with open("4_input.txt") as f: + bingo_calls = [int(n) for n in f.readline().split(",")] + + lines = f.readlines() + for i in range(0, len(lines), 6): + bingo_boards.append(BingoBoard(lines[i+1:i+6])) + +for call in bingo_calls: + for board in bingo_boards: + won = board.number_called(call) + if won: + score = board.get_score() + print(score) + print(call) + print(score * call) + sys.exit(0) diff --git a/4_2.py b/4_2.py new file mode 100644 index 0000000..2c0a646 --- /dev/null +++ b/4_2.py @@ -0,0 +1,74 @@ +"""Play bingo""" +import sys + +class BingoBoard(): + def __init__(self, lines): + self.board = [] + self.called = [ + [False] * 5, + [False] * 5, + [False] * 5, + [False] * 5, + [False] * 5, + ] + for line in lines: + self.board.append([int(n) for n in line.strip().split()]) + + def number_called(self, number): + for i, row in enumerate(self.board): + for j, n in enumerate(row): + if n == number: + self.called[i][j] = True + + return self.won() + + return False + + def won(self): + for i, row in enumerate(self.called): + if row == [True] * 5: + return True + + cols_called = list(zip(*self.called)) + cols_board = list(zip(*self.board)) + for j, col in enumerate(cols_called): + if col == tuple([True] * 5): + return True + + return False + + def get_score(self): + score = 0 + for i in range(5): + for j in range(5): + if not self.called[i][j]: + score += self.board[i][j] + print(self.board[i][j]) + + return score + + +bingo_boards = [] +with open("4_input.txt") as f: + bingo_calls = [int(n) for n in f.readline().split(",")] + + lines = f.readlines() + for i in range(0, len(lines), 6): + bingo_boards.append(BingoBoard(lines[i+1:i+6])) + +for call in bingo_calls: + to_keep = [] + for board in bingo_boards: + won = board.number_called(call) + if won: + if len(bingo_boards) == 1: + score = board.get_score() + print(score) + print(call) + print(score * call) + sys.exit(0) + else: + to_keep.append(board) + + bingo_boards = to_keep + to_keep = [] -- 2.46.2