def part_2(data: List[str]): chunked = chunk_aoc_data(data) valid_ranges, ticket, nearby_ticket_vals = unpack_apply( chunked, get_valid_ranges, lambda x: [int(_) for _ in x[1].split(',')], get_nearby_ticket_values, ) collapsed_ranges = collapse_valid_ranges( chain.from_iterable(valid_ranges.values())) flattened_tickets = chain.from_iterable(nearby_ticket_vals) invalid_numbers = set( get_invalid_numbers(collapsed_ranges, flattened_tickets)) nearby_ticket_vals = nearby_ticket_vals + [ticket] valid_tickets = sorted(filter( lambda x: not len([_ for _ in x if _ in invalid_numbers]), nearby_ticket_vals), reverse=True) prop_map = get_prop_map(valid_ranges, valid_tickets) departure_vals = [] for k, v in prop_map.items(): if k.startswith('departure'): departure_vals.append(v) return prod([ticket[_] for _ in departure_vals])
def get_valid_ranges(x: str) -> dict: ranges = { k: v for k, v in [unpack_apply(_.split(': '), None, clean_ranges) for _ in x] } return ranges
def part_1(data: List[str]): chunked = chunk_aoc_data(data) valid_ranges, _, nearby_ticket_vals = unpack_apply( chunked, get_valid_ranges, None, get_nearby_ticket_values) collapsed_ranges = collapse_valid_ranges( chain.from_iterable(valid_ranges.values())) flattened_tickets = chain.from_iterable(nearby_ticket_vals) return sum(get_invalid_numbers(collapsed_ranges, flattened_tickets))
def part_1(data: List[str]): mask = get_mask_val(data[0]) d = {} for _ in data[1:]: if _.startswith('mask'): mask = get_mask_val(_) continue k, v = unpack_apply(_.split(' = '), get_mem_loc, str_to_bit_str) d[k] = apply_mask(mask, v) return sum(map(bit_str_to_int, d.values()))
def part_2(data: List[str]): mask = get_mask_val(data[0]) d = {} for line in data[1:]: if line.startswith('mask'): mask = get_mask_val(line) continue k, v = unpack_apply(line.split(' = '), get_mem_loc) new_keys = apply_mask_2(mask, str_to_bit_str(k)) for num in new_keys: d[num] = v return sum(map(int, d.values()))
def test_unpack_apply(): x = ('bob', '33', 'fbi') res = unpack_apply(x, capitalize, int, upper) assert res == ('Bob', 33, 'FBI')
def test_unpack_apply_with_none(): x = ['bob', '33', 'fbi'] res = unpack_apply(x, capitalize, None, upper) assert res == ('Bob', '33', 'FBI')
def test_unpack_apply_generator(): x = (_ for _ in ['bob', '33', 'fbi']) res = unpack_apply(x, capitalize, int, upper) assert res == ('Bob', 33, 'FBI')
def test_unpack_apply_from_string(): x = 'bob 33 fbi'.split(' ') res = unpack_apply(x, capitalize, int, upper) assert res == ('Bob', 33, 'FBI')
def test_unpack_apply_less_elements(): x = ('bob', '33') res = unpack_apply(x, capitalize, int, upper) assert res == ('Bob', 33)
def test_unpack_apply_more_elements(): x = ('bob', '33', 'fbi', 'banana') res = unpack_apply(x, capitalize, int, upper) assert res == ('Bob', 33, 'FBI', 'banana')