day 3: another method
This commit is contained in:
parent
120660d6a7
commit
9c619282f4
@ -32,5 +32,5 @@ def part2(inp):
|
||||
print(f"Cumulative product of tress : {tree_product}")
|
||||
|
||||
if __name__ == "__main__":
|
||||
#part1('./input.txt')
|
||||
part1('input.txt')
|
||||
part2('input.txt')
|
||||
|
51
day5/day5.py
51
day5/day5.py
@ -1,5 +1,6 @@
|
||||
#! /usr/bin/env python3
|
||||
from itertools import product
|
||||
from bisect import bisect
|
||||
|
||||
|
||||
def main(filename):
|
||||
@ -23,34 +24,42 @@ def part1(results):
|
||||
def part2(results):
|
||||
seat_ids = sorted(x[2] for x in results.values())
|
||||
missing_seat_ids = set(range(max(seat_ids))) - set(seat_ids)
|
||||
print(missing_seat_ids)
|
||||
print("Your seat id : ", max(missing_seat_ids))
|
||||
|
||||
|
||||
def parse_boarding_pass(boarding_pass):
|
||||
row = _parse(boarding_pass[:7], "F", "B", 128)
|
||||
col = _parse(boarding_pass[7:], "L", "R", 7)
|
||||
def parse_boarding_pass(boarding_pass, strategy="binary"):
|
||||
"Poor man's dispatcher"
|
||||
try:
|
||||
to_call = globals()[f"parse_boarding_pass_{strategy}"]
|
||||
return to_call(boarding_pass)
|
||||
except KeyError:
|
||||
raise KeyError(f"Bad strategy name {strategy}")
|
||||
|
||||
|
||||
def parse_boarding_pass_binary(boarding_pass):
|
||||
"Parse boarding pass using a binary conversion"
|
||||
boarding_pass = boarding_pass.translate(str.maketrans("FLBR", "0011"))
|
||||
row = boarding_pass[:7]
|
||||
col = boarding_pass[7:]
|
||||
return int(row, base=2), int(col, base=2)
|
||||
|
||||
|
||||
def parse_boarding_pass_bisect(boarding_pass):
|
||||
"Pass boarding pass using bisection algorithm"
|
||||
row = bisect(boarding_pass[:7], lower_option="F", upper_option="B", max=127)
|
||||
col = bisect(boarding_pass[7:], lower_option="L", upper_option="R", max=7)
|
||||
return row, col
|
||||
|
||||
|
||||
def _parse(inp, lower_option, upper_option, number):
|
||||
rows = slice(0, number - 1)
|
||||
def bisect(inp, lower_option, upper_option, max):
|
||||
min_v, max_v = 0, max
|
||||
for l in inp:
|
||||
length = max_v - min_v
|
||||
if l == lower_option:
|
||||
rows = lower_half(rows)
|
||||
else:
|
||||
rows = upper_half(rows)
|
||||
return rows.start
|
||||
|
||||
|
||||
def lower_half(sl: slice):
|
||||
length = sl.stop - sl.start
|
||||
return slice(sl.start, sl.start + length // 2)
|
||||
|
||||
|
||||
def upper_half(sl: slice):
|
||||
length = sl.stop - sl.start
|
||||
return slice(1 + sl.start + length // 2, sl.stop)
|
||||
|
||||
max_v = min_v + length // 2
|
||||
elif l == upper_option:
|
||||
min_v = 1 + min_v + length // 2
|
||||
return min_v
|
||||
|
||||
def get_seat_id(row, col):
|
||||
return 8 * row + col
|
||||
|
@ -1,5 +1,5 @@
|
||||
#! /usr/bin/env python3
|
||||
from day6 import parse_boarding_pass, get_seat_id
|
||||
from day5 import *
|
||||
|
||||
def tests():
|
||||
inputs = {
|
||||
@ -9,8 +9,13 @@ def tests():
|
||||
"BBFFBBFRLL": (102, 4, 820)
|
||||
}
|
||||
|
||||
test("bisect", inputs)
|
||||
test("binary", inputs)
|
||||
|
||||
|
||||
def test(strategy, inputs):
|
||||
for boarding_pass, expected in inputs.items():
|
||||
row, col = parse_boarding_pass(boarding_pass)
|
||||
row, col = parse_boarding_pass(boarding_pass, strategy=strategy)
|
||||
seat_id = get_seat_id(row, col)
|
||||
assert row == expected[0]
|
||||
assert col == expected[1]
|
||||
|
Loading…
Reference in New Issue
Block a user