Beispiel #1
0
        if not any(field.contains(number) for field in fields):
            return number
    return None


def part1(fields: List[Field], tickets: List[List[int]]) -> int:
    return  sum(get_invalid_number(fields, ticket) or 0 for ticket in tickets)


def part2(fields: List[Field], my_ticket: List[int], other_tickets: List[List[int]]) -> int:
    valid_tickets = [t for t in other_tickets if get_invalid_number(fields, t) is None]
    candidates: Dict[str, List[int]] = defaultdict(list)
    for field in fields:
        for index in range(len(fields)):
            if all(field.contains(t[index]) for t in valid_tickets):
                candidates[field.name].append(index)
    col_mapping: Dict[str, int] = dict()
    for candidate in sorted(candidates.items(), key=lambda x: len(x[1])):
        for index in candidate[1]:
            if index not in col_mapping.values():
                col_mapping[candidate[0]] = index
    departure_cols = (index for name, index in col_mapping.items() if name.startswith('departure'))
    return math.prod(my_ticket[index] for index in departure_cols)


if __name__ == '__main__':
    raw_data = get_raw_input()
    raw_fields, raw_my_ticket, raw_other_tickets = parse_raw_data(raw_data)
    print(part1(raw_fields, raw_other_tickets))  # 29019
    print(part2(raw_fields, raw_my_ticket, raw_other_tickets))  # 517827547723
Beispiel #2
0

def find_all_velocities(target: Target) -> list[tuple[int, int]]:
    hits = []
    MAXX = max(abs(target.xmin), abs(target.xmax))
    MAXY = max(abs(target.ymin), abs(target.ymax))
    for x in range(-MAXX, MAXX + 1):
        for y in range(-MAXY, MAXY + 1):
            if shoot(x, y, target):
                hits.append((x, y))
    return hits


def part1(hits: list[tuple[int, int]]) -> int:
    maxyspeed = max(v[1] for v in hits)
    maxheight = maxyspeed * (maxyspeed + 1) / 2
    return int(maxheight)


def part2(hits: list[tuple[int, int]]) -> int:
    return len(hits)


if __name__ == '__main__':
    raw_input = get_raw_input()
    target = Target(raw_input)
    # print(target.xmin, target.xmax, target.ymin, target.ymax)
    hits = find_all_velocities(target)
    print(part1(hits))  # 23005
    print(part2(hits))  # 2040
Beispiel #3
0
from utils import get_raw_input


def find_two_sum(entries: Set[int], total: int) -> Optional[Tuple[int, int]]:
    for entry in entries:
        other = total - entry
        if other in entries:
            return (entry, other)
    return None


def part1(entries: Set[int]) -> int:
    pair = find_two_sum(entries, 2020)
    return pair[0] * pair[1] if pair else 0


def part2(entries: Set[int]) -> int:
    for entry1 in entries:
        pair = find_two_sum(entries - {entry1}, 2020 - entry1)
        if pair:
            return entry1 * pair[0] * pair[1]
    return 0


if __name__ == '__main__':
    raw_input = [int(x) for x in get_raw_input().split()]
    numbers: Set[int] = set(raw_input)
    print(part1(numbers))  # 41979
    print(part2(numbers))  # 193416912
Beispiel #4
0
eyr:1972 cid:100
hcl:#18171d ecl:amb hgt:170 pid:186cm iyr:2018 byr:1926

iyr:2019
hcl:#602927 eyr:1967 hgt:170cm
ecl:grn pid:012533040 byr:1946

hcl:dab227 iyr:2012
ecl:brn hgt:182cm pid:021572410 eyr:2020 byr:1992 cid:277

hgt:59cm ecl:zzz
eyr:2038 hcl:74454a iyr:2023
pid:3556412378 byr:2007

pid:087499704 hgt:74in ecl:grn iyr:2012 eyr:2030 byr:1980
hcl:#623a2f

eyr:2029 ecl:blu cid:129 byr:1989
iyr:2014 pid:896056539 hcl:#a97842 hgt:165cm

hcl:#888785
hgt:164cm byr:2001 iyr:2015 cid:88
pid:545766238 ecl:hzl
eyr:2022

iyr:2010 hgt:158cm hcl:#b6652a ecl:blu byr:1944 eyr:2021 pid:093154719"""

inp = utils.get_raw_input(4)
first(inp)
second(inp)
Beispiel #5
0
 def handle_keyboard_input(self):
     return ut.get_raw_input()