From d60f6dc50ea3ea7066fc5e267076276939e2cbc2 Mon Sep 17 00:00:00 2001 From: Tom van der Lee Date: Sun, 11 Dec 2022 13:48:39 +0100 Subject: Day 9 [WIP] --- day9/__init__.py | 108 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 108 insertions(+) create mode 100644 day9/__init__.py (limited to 'day9/__init__.py') diff --git a/day9/__init__.py b/day9/__init__.py new file mode 100644 index 0000000..afa44aa --- /dev/null +++ b/day9/__init__.py @@ -0,0 +1,108 @@ +# -*- coding: utf-8 -*- +from abc import ABC +from copy import copy +from dataclasses import dataclass +from typing import Tuple, Iterator, List + +from aoc import BaseAssignment + + +@dataclass +class Coordinate: + x: int + y: int + + def __hash__(self): + return tuple([self.x, self.y]).__hash__() + + def __sub__(self, other: "Coordinate"): + return Coordinate(self.x - other.x, self.y - other.y) + + @property + def polarity_x(self): + try: + return abs(self.x) / self.x + except ZeroDivisionError: + return 0 + + @property + def polarity_y(self): + try: + return abs(self.y) / self.y + except ZeroDivisionError: + return 0 + + +class Assignment(BaseAssignment, ABC): + def parse_line(self, item: str) -> Tuple[str, int]: + direction, amount = item.split(" ") + return direction, int(amount) + + def move(self, direction: str, amount: int): + pass + + @staticmethod + def next_head(head: Coordinate, direction: str): + match direction: + case "R": + head.x += 1 + case "L": + head.x -= 1 + case "U": + head.y += 1 + case "D": + head.y -= 1 + + @staticmethod + def next_tail(head: Coordinate, tail: Coordinate): + delta = head - tail + + if abs(delta.x) > 1 and delta.y == 0: + tail.x += delta.x - delta.polarity_x + if abs(delta.x) > 1 and abs(delta.y) == 1: + tail.x += delta.x - delta.polarity_x + tail.y += delta.y + if abs(delta.y > 1) and delta.x == 0: + tail.y += delta.y - delta.polarity_y + if abs(delta.y) > 1 and abs(delta.x) == 1: + tail.y += delta.y - delta.polarity_y + tail.x += delta.x + + def tail_positions(self, input: Iterator[str]) -> Iterator[Coordinate]: + head = Coordinate(0, 0) + tail = Coordinate(0, 0) + + for line in input: + direction, amount = self.parse_line(line) + + for _ in range(amount): + self.next_head(head, direction) + self.next_tail(head, tail) + yield tail + + +class AssignmentOne(Assignment): + example_result = 13 + + def run(self, input: Iterator) -> int: + unique_tail_positions = set() + + for position in self.tail_positions(input): + unique_tail_positions.add(copy(position)) + + print(unique_tail_positions) + + for y in range(5): + items = [] + for x in range(6): + if Coordinate(x, y) in unique_tail_positions: + items.append("#") + else: + items.append(".") + print("".join(items)) + + return len(unique_tail_positions) + + +class AssignmentTwo(Assignment): + pass -- cgit v1.2.3