2015: d16: ex1: add solution
This commit is contained in:
parent
74843640bd
commit
5cce56ced2
1 changed files with 62 additions and 0 deletions
62
2015/d16/ex1/ex1.py
Executable file
62
2015/d16/ex1/ex1.py
Executable file
|
|
@ -0,0 +1,62 @@
|
||||||
|
#!/usr/bin/env python
|
||||||
|
|
||||||
|
import sys
|
||||||
|
from typing import NamedTuple
|
||||||
|
|
||||||
|
|
||||||
|
class Sue(NamedTuple):
|
||||||
|
children: int | None = None
|
||||||
|
cats: int | None = None
|
||||||
|
samoyeds: int | None = None
|
||||||
|
pomeranians: int | None = None
|
||||||
|
akitas: int | None = None
|
||||||
|
vizslas: int | None = None
|
||||||
|
goldfish: int | None = None
|
||||||
|
trees: int | None = None
|
||||||
|
cars: int | None = None
|
||||||
|
perfumes: int | None = None
|
||||||
|
|
||||||
|
def matches(self, reading: "Sue") -> bool:
|
||||||
|
for it, reading_it in zip(self, reading):
|
||||||
|
if it is None or reading_it is None:
|
||||||
|
continue
|
||||||
|
if it != reading_it:
|
||||||
|
return False
|
||||||
|
return True
|
||||||
|
|
||||||
|
|
||||||
|
def solve(input: str) -> int:
|
||||||
|
def parse_line(input: str) -> Sue:
|
||||||
|
_, other = input.split(": ", 1)
|
||||||
|
raw_items = other.split(", ")
|
||||||
|
items = {
|
||||||
|
name: int(val) for name, val in (item.split(": ") for item in raw_items)
|
||||||
|
}
|
||||||
|
return Sue(**items)
|
||||||
|
|
||||||
|
def parse(input: str) -> list[Sue]:
|
||||||
|
return [parse_line(line) for line in input.splitlines()]
|
||||||
|
|
||||||
|
aunts = parse(input)
|
||||||
|
reading = Sue(
|
||||||
|
children=3,
|
||||||
|
cats=7,
|
||||||
|
samoyeds=2,
|
||||||
|
pomeranians=3,
|
||||||
|
akitas=0,
|
||||||
|
vizslas=0,
|
||||||
|
goldfish=5,
|
||||||
|
trees=3,
|
||||||
|
cars=2,
|
||||||
|
perfumes=1,
|
||||||
|
)
|
||||||
|
return next(i for i, sue in enumerate(aunts, 1) if sue.matches(reading))
|
||||||
|
|
||||||
|
|
||||||
|
def main() -> None:
|
||||||
|
input = sys.stdin.read()
|
||||||
|
print(solve(input))
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main()
|
||||||
Loading…
Add table
Add a link
Reference in a new issue