aoc

Advent of Code Solutions
git clone git://git.alexkarle.com.com/aoc
Log | Files | Refs | README | LICENSE

commit c5fcc31553f7477b0121f64098f9c97b34f56cfd (patch)
parent e7c4ab6fee080efd9f9329a7c0809b316f9074f4
Author: Alex Karle <alex@alexkarle.com>
Date:   Sat,  4 Dec 2021 00:48:05 -0500

day4: Refactor common routines to separate file

I should probably look into having a single sol.py called with -a or -b
flags... hmmmm

Diffstat:
M.gitignore | 1+
M4/a.py | 37++++---------------------------------
M4/b.py | 39+++++----------------------------------
A4/common.py | 35+++++++++++++++++++++++++++++++++++
4 files changed, 45 insertions(+), 67 deletions(-)

diff --git a/.gitignore b/.gitignore @@ -1,3 +1,4 @@ # compiled solutions a b +__pycache__ diff --git a/4/a.py b/4/a.py @@ -1,42 +1,13 @@ #!/usr/bin/env python3 import sys +import common from collections import defaultdict -def parse(): - first = True - N = None - boards = [] - board = [] - for l in sys.stdin: - if first: - N = [int(x) for x in l.strip().split(',')] - first = False - else: - l = l.strip() - if l: - row = [int(x) for x in l.split()] - board.append(row) - elif board: - boards.append(board) - board = [] - - return N, boards - - def main(boards, N): # we need an efficient way to find boards with numbers - # roll each n into `(b, r, c)` - index = {} - i = 0 - for b in boards: - for r in range(len(b)): - for c in range(len(b[r])): - x = b[r][c] - if x not in index: - index[x] = [] - index[x].append((i, r, c)) - i+=1 + # get a mapping of num_called -> (board_idx, row, col) + index = common.index(boards) # now go through all numbers and keep track of if r/c counts # by board, if one is 5... @@ -67,5 +38,5 @@ def main(boards, N): if __name__ == '__main__': - N, boards = parse() + N, boards = common.parse() main(boards, N) diff --git a/4/b.py b/4/b.py @@ -1,44 +1,15 @@ #!/usr/bin/env python3 import sys +import common from collections import defaultdict -def parse(): - first = True - N = None - boards = [] - board = [] - for l in sys.stdin: - if first: - N = [int(x) for x in l.strip().split(',')] - first = False - else: - l = l.strip() - if l: - row = [int(x) for x in l.split()] - board.append(row) - elif board: - boards.append(board) - board = [] - - return N, boards - - def main(boards, N): # we need an efficient way to find boards with numbers - # roll each n into `(b, r, c)` - index = {} - i = 0 - for b in boards: - for r in range(len(b)): - for c in range(len(b[r])): - x = b[r][c] - if x not in index: - index[x] = [] - index[x].append((i, r, c)) - i+=1 + # get a mapping of num_called -> (board_idx, row, col) + index = common.index(boards) # now go through all numbers and keep track of if r/c counts - # by board, if one is 5... + # by board, if one is 5, we have a winner! winners = {} seen = {} counts = {} @@ -72,5 +43,5 @@ def main(boards, N): if __name__ == '__main__': - N, boards = parse() + N, boards = common.parse() main(boards, N) diff --git a/4/common.py b/4/common.py @@ -0,0 +1,35 @@ +import sys + +def parse(): + first = True + N = None + boards = [] + board = [] + for l in sys.stdin: + if first: + N = [int(x) for x in l.strip().split(',')] + first = False + else: + l = l.strip() + if l: + row = [int(x) for x in l.split()] + board.append(row) + elif board: + boards.append(board) + board = [] + + return N, boards + +def index(boards): + # Generates a lookup table of: + # num_called => (board, row, column) + index = {} + i = 0 + for b in boards: + for r in range(len(b)): + for c in range(len(b[r])): + x = b[r][c] + if x not in index: + index[x] = [] + index[x].append((i, r, c)) + i+=1