From c5fcc31553f7477b0121f64098f9c97b34f56cfd Mon Sep 17 00:00:00 2001 From: Alex Karle Date: Sat, 4 Dec 2021 00:48:05 -0500 Subject: [PATCH] day4: Refactor common routines to separate file I should probably look into having a single sol.py called with -a or -b flags... hmmmm --- .gitignore | 1 + 4/a.py | 37 ++++--------------------------------- 4/b.py | 39 +++++---------------------------------- 4/common.py | 35 +++++++++++++++++++++++++++++++++++ 4 files changed, 45 insertions(+), 67 deletions(-) create mode 100644 4/common.py diff --git a/.gitignore b/.gitignore index d0b311e..1f4582e 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ # compiled solutions a b +__pycache__ diff --git a/4/a.py b/4/a.py index a3bc8f6..14bd774 100755 --- 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 index bd8975b..9505599 100755 --- 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 new file mode 100644 index 0000000..4dbdbe4 --- /dev/null +++ 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 -- libgit2 1.8.1