From 773150f2817ca8b44dc7553250e6768d5a1f6b9e Mon Sep 17 00:00:00 2001 From: Bruno BELANYI Date: Wed, 16 Dec 2020 10:10:50 +0100 Subject: [PATCH] 2020: d16: ex1: add solution --- 2020/d16/ex1/ex1.py | 59 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100755 2020/d16/ex1/ex1.py diff --git a/2020/d16/ex1/ex1.py b/2020/d16/ex1/ex1.py new file mode 100755 index 0000000..91f2665 --- /dev/null +++ b/2020/d16/ex1/ex1.py @@ -0,0 +1,59 @@ +#!/usr/bin/env python +import sys +from typing import Dict, List, Set, Tuple + +Ranges = Dict[str, Set[int]] +Ticket = List[int] + + +def parse(raw: List[str]) -> Tuple[Ranges, List[Ticket]]: + def extract_field_info(line: str) -> Tuple[str, Set[int]]: + field, rest = line.split(":") + ranges: Set[int] = set() + for r in rest.strip().split(" or "): + lhs, rhs = r.split("-") + ranges |= set(range(int(lhs), int(rhs) + 1)) + return field, ranges + + def parse_tickets(line: str) -> Ticket: + return [int(i) for i in line.split(",")] + + ranges: Ranges = {} + tickets: List[Ticket] = [] + + should_parse_tickets = False + for line in raw: + if line == "": + should_parse_tickets = True + continue + if "ticket" in line: + continue + if should_parse_tickets: + tickets.append(parse_tickets(line)) + else: + field, field_ranges = extract_field_info(line) + ranges[field] = field_ranges + + return ranges, tickets + + +def solve(raw: List[str]) -> int: + ranges, tickets = parse(raw) + sum = 0 + + for ticket in tickets[1:]: + for val in ticket: + if any(val in r for r in ranges.values()): + continue + sum += val + + return sum + + +def main() -> None: + input = [line.strip() for line in sys.stdin.readlines()] + print(solve(input)) + + +if __name__ == "__main__": + main()