2017: d20: ex2: add solution
This commit is contained in:
parent
7108246f19
commit
2cde2f7d82
1 changed files with 57 additions and 0 deletions
57
2017/d20/ex2/ex2.py
Executable file
57
2017/d20/ex2/ex2.py
Executable file
|
|
@ -0,0 +1,57 @@
|
|||
#!/usr/bin/env python
|
||||
|
||||
import collections
|
||||
import sys
|
||||
from typing import NamedTuple
|
||||
|
||||
|
||||
class Point(NamedTuple):
|
||||
x: int
|
||||
y: int
|
||||
z: int
|
||||
|
||||
|
||||
class Particle(NamedTuple):
|
||||
pos: Point
|
||||
vel: Point
|
||||
acc: Point
|
||||
|
||||
@classmethod
|
||||
def from_str(cls, input: str) -> "Particle":
|
||||
p, v, a = input.split(", ")
|
||||
return cls(
|
||||
Point(*map(int, p[3:-1].split(","))),
|
||||
Point(*map(int, v[3:-1].split(","))),
|
||||
Point(*map(int, a[3:-1].split(","))),
|
||||
)
|
||||
|
||||
def tick(self) -> "Particle":
|
||||
pos, vel, acc = self
|
||||
vel = Point(*((v + a) for v, a in zip(vel, acc)))
|
||||
pos = Point(*((p + v) for p, v in zip(pos, vel)))
|
||||
return Particle(pos, vel, acc)
|
||||
|
||||
|
||||
def solve(input: str) -> int:
|
||||
def parse(input: str) -> list[Particle]:
|
||||
return [Particle.from_str(line) for line in input.splitlines()]
|
||||
|
||||
def tick(particles: list[Particle]) -> list[Particle]:
|
||||
particles = [p.tick() for p in particles]
|
||||
positions = collections.Counter(p.pos for p in particles)
|
||||
return [p for p in particles if positions[p.pos] == 1]
|
||||
|
||||
particles = parse(input)
|
||||
# Guesstimate that 1000 iterations is enough to process all colisions
|
||||
for _ in range(1000):
|
||||
particles = tick(particles)
|
||||
return len(particles)
|
||||
|
||||
|
||||
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