diff options
| author | Gustav Sörnäs <gustav@sornas.net> | 2020-12-05 06:23:51 +0100 |
|---|---|---|
| committer | Gustav Sörnäs <gustav@sornas.net> | 2020-12-05 06:23:51 +0100 |
| commit | 481ea0df1c89833af35156c1c30c29fb8d882f76 (patch) | |
| tree | e6377bdbfc015be2b7b7f33405a91ab3c5cbc45e /20/py/d05.py | |
| parent | 6a77a12c1389b034c5a5b0ca401918b7cd667146 (diff) | |
| download | aoc-481ea0df1c89833af35156c1c30c29fb8d882f76.tar.gz | |
day 05
Diffstat (limited to '20/py/d05.py')
| -rw-r--r-- | 20/py/d05.py | 51 |
1 files changed, 51 insertions, 0 deletions
diff --git a/20/py/d05.py b/20/py/d05.py new file mode 100644 index 0000000..d72d3ee --- /dev/null +++ b/20/py/d05.py @@ -0,0 +1,51 @@ +#!/usr/bin/env python3 +import sys + + +def bin(lo, hi, x): + mid = (hi+lo)//2 + if x in "BR": + # upper + return (mid + 1, hi) + else: + return (lo, mid) + + +def pt1(_in): + highest = 0 + for board in _in: + r_lo, r_hi = 0, 127 + for r in board[:7]: + r_lo, r_hi = bin(r_lo, r_hi, r) + c_lo, c_hi = 0, 7 + for c in board[7:]: + c_lo, c_hi = bin(c_lo, c_hi, c) + i = r_lo*8+c_lo + if i > highest: + highest = i + return highest + + +def pt2(_in): + seats = [] + for board in _in: + r_lo, r_hi = 0, 127 + for r in board[:7]: + r_lo, r_hi = bin(r_lo, r_hi, r) + c_lo, c_hi = 0, 7 + for c in board[7:]: + c_lo, c_hi = bin(c_lo, c_hi, c) + seats.append(r_lo*8+c_lo) + seats = sorted(seats) + for i in range(len(seats) - 1): + if seats[i+1] == seats[i] + 2: + return seats[i] + 1 + + +if __name__ == "__main__": + if len(sys.argv) > 1: + input = open(sys.argv[1], "r").readlines() + else: + input = open("../input/05", "r").readlines() + print(pt1(input)) + print(pt2(input)) |
