diff --git a/py/2018/10/solve.py b/py/2018/10/solve.py deleted file mode 100644 index 991fb7b..0000000 --- a/py/2018/10/solve.py +++ /dev/null @@ -1,86 +0,0 @@ -import re -import sys - -# PART 1 - -POINT_RE = r"position=<\s*(-?\d+),\s*(-?\d+)> velocity=<\s*(-?\d+),\s*(-?\d+)>\n" - -class Point: - def __init__(self, pos, vel): - self.pos = pos - self.vel = vel - - @classmethod - def from_line(cls, line): - match = re.fullmatch(POINT_RE, line) - x, y, dx, dy = map(int, match.groups()) - return cls((x, y), (dx, dy)) - - def step(self): - x, y = self.pos - dx, dy = self.vel - self.pos = (x + dx, y + dy) - -class Field: - def __init__(self, points=None): - self.points = points or [] - self.steps = 0 - - @classmethod - def from_file(cls, filename): - points = [] - with open(filename, "r") as f: - for line in f: - points.append(Point.from_line(line)) - return cls(points=points) - - def step(self): - for point in self.points: - point.step() - self.steps += 1 - - def step_until_in_limits(self): - while True: - coords = set(point.pos for point in self.points) - xs = set(x for (x, _) in coords) - ys = set(y for (_, y) in coords) - - if max(xs) - min(xs) < 500: - return - - self.step() - - print(min(xs), max(xs), min(ys), max(ys)) - - def render(self): - coords = set(point.pos for point in self.points) - xs = set(x for (x, _) in coords) - ys = set(y for (_, y) in coords) - - for y in range(min(ys), max(ys) + 1): - for x in range(min(xs), max(xs) + 1): - if (x, y) in coords: - print("#", end="") - else: - print(".", end="") - print() - - print(self.steps) - -# PART 2 - -def main(filename): - print(f"Solutions for {filename}") - - print("Part 1:") - field = Field.from_file(filename) - field.step_until_in_limits() - while input() == "": - field.render() - field.step() - - print("Part 2: NYI") - -if __name__ == "__main__": - for filename in sys.argv[1:]: - main(filename) diff --git a/py/aoc/__init__.py b/py/aoc/__init__.py index 58c3163..fe4c179 100644 --- a/py/aoc/__init__.py +++ b/py/aoc/__init__.py @@ -2,7 +2,7 @@ import sys import argparse from pathlib import Path -from .y2018 import d01, d02, d03, d04, d05, d06, d07, d08, d09 +from .y2018 import d01, d02, d03, d04, d05, d06, d07, d08, d09, d10 from .y2020 import d10 from .y2021 import d14 from .y2022 import d01, d02, d03, d04, d05, d06 @@ -17,6 +17,7 @@ DAYS = { "2018_07": y2018.d07.solve, "2018_08": y2018.d08.solve, "2018_09": y2018.d09.solve, + "2018_10": y2018.d10.solve, "2020_10": y2020.d10.solve, "2021_14": y2021.d14.solve, "2022_01": y2022.d01.solve, diff --git a/py/aoc/y2018/d10.py b/py/aoc/y2018/d10.py new file mode 100644 index 0000000..7a053d8 --- /dev/null +++ b/py/aoc/y2018/d10.py @@ -0,0 +1,78 @@ +import re + + +POINT_RE = r"position=<\s*(-?\d+),\s*(-?\d+)> velocity=<\s*(-?\d+),\s*(-?\d+)>" + + +class Point: + def __init__(self, pos, vel): + self.pos = pos + self.vel = vel + + @classmethod + def from_line(cls, line): + match = re.fullmatch(POINT_RE, line) + x, y, dx, dy = map(int, match.groups()) + return cls((x, y), (dx, dy)) + + def step(self): + x, y = self.pos + dx, dy = self.vel + self.pos = (x + dx, y + dy) + + +class Field: + def __init__(self, points=None): + self.points = points or [] + self.steps = 0 + + @classmethod + def from_str(cls, string): + points = [] + for line in string.splitlines(): + points.append(Point.from_line(line)) + return cls(points=points) + + def step(self): + for point in self.points: + point.step() + self.steps += 1 + + def step_until_in_limits(self): + while True: + coords = set(point.pos for point in self.points) + xs = set(x for (x, _) in coords) + ys = set(y for (_, y) in coords) + + if max(xs) - min(xs) < 500: + return + + self.step() + + print(min(xs), max(xs), min(ys), max(ys)) + + def render(self): + coords = set(point.pos for point in self.points) + xs = set(x for (x, _) in coords) + ys = set(y for (_, y) in coords) + + for y in range(min(ys), max(ys) + 1): + for x in range(min(xs), max(xs) + 1): + if (x, y) in coords: + print("#", end="") + else: + print(".", end="") + print() + + print(self.steps) + + +def solve(inputstr): + print("Part 1:") + field = Field.from_str(inputstr) + field.step_until_in_limits() + while input() == "": + field.render() + field.step() + + print("Part 2: NYI")