-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
101 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
__year__ = 2020 | ||
__day__ = 16 | ||
__title__ = "Ticket Translation" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
class: 1-3 or 5-7 | ||
row: 6-11 or 33-44 | ||
seat: 13-40 or 45-50 | ||
|
||
your ticket: | ||
7,1,14 | ||
|
||
nearby tickets: | ||
7,3,47 | ||
40,4,50 | ||
55,2,20 | ||
38,6,12 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
import collections | ||
import functools | ||
|
||
from dataclasses import dataclass | ||
from math import prod | ||
|
||
from aoc.lib.sets import IntervalSet | ||
from aoc.lib.solution import SolutionBase | ||
|
||
import logging | ||
|
||
|
||
__all__ = ["Solution"] | ||
__log__ = logging.getLogger(__name__) | ||
|
||
Ticket = tuple[int, ...] | ||
|
||
|
||
@dataclass | ||
class Input: | ||
rules: dict[str, IntervalSet] | ||
own: Ticket | ||
nearby: list[Ticket] | ||
|
||
|
||
class Solution(SolutionBase): | ||
@staticmethod | ||
def prepare(data): | ||
return Input( | ||
rules={ | ||
field: IntervalSet( | ||
*( | ||
map(int, interval.split("-")) | ||
for interval in value.strip().split(" or ") | ||
) | ||
) | ||
for field, value in (rule.split(":") for rule in data[0]) | ||
}, | ||
own=tuple(map(int, data[1][1].split(","))), | ||
nearby=list(tuple(map(int, ticket.split(","))) for ticket in data[2][1:]), | ||
) | ||
|
||
@staticmethod | ||
def part_01(data): | ||
valid = functools.reduce(lambda a, b: a | b, data.rules.values()) | ||
return sum(sum(x for x in ticket if x not in valid) for ticket in data.nearby) | ||
|
||
@staticmethod | ||
def part_02(data): | ||
valid = functools.reduce(lambda a, b: a | b, data.rules.values()) | ||
tickets = (ticket for ticket in data.nearby if all(x in valid for x in ticket)) | ||
|
||
investigate = collections.defaultdict(lambda: set(range(len(data.own) + 1))) | ||
for ticket in tickets: | ||
for name, values in data.rules.items(): | ||
investigate[name] &= { | ||
idx for idx, value in enumerate(ticket) if value in values | ||
} | ||
|
||
translate = {} | ||
while investigate: | ||
translate.update( | ||
{ | ||
code: opcodes.pop() | ||
for code, opcodes in investigate.items() | ||
if len(opcodes) == 1 | ||
} | ||
) | ||
investigate = { | ||
code: opcodes - set(translate.values()) | ||
for code, opcodes in investigate.items() | ||
if len(opcodes) > 1 | ||
} | ||
|
||
return prod( | ||
data.own[idx] | ||
for name, idx in translate.items() | ||
if name.startswith("departure") | ||
) | ||
|
||
|
||
if __name__ == "__main__": | ||
import aoc.lib.main | ||
|
||
aoc.lib.main.main(Solution) |