--- /dev/null
+"""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)
--- /dev/null
+"""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)
--- /dev/null
+"""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 = []