from dataclasses import dataclass
from itertools import count
@dataclass
class Scanner(object):
depth: int
period: int
severity: int
@classmethod
def from_line(cls, line):
depth, range_ = map(int, line.split(":"))
return cls(depth, (range_ - 1) * 2, depth * range_)
def catches(self, delay=0):
return (self.depth + delay) % self.period == 0
def severity(firewall):
return sum(s.severity for s in firewall if s.catches())
def find_delay(firewall):
return next(d for d in count() if not any(s.catches(d) for s in firewall))
def read_firewall(lines):
return [Scanner.from_line(line) for line in lines if line.strip()]