summaryrefslogtreecommitdiffstats
path: root/day9/__init__.py
diff options
context:
space:
mode:
authorGravatar Tom van der Lee <t0m.vd.l33@gmail.com>2022-12-11 13:48:39 +0100
committerGravatar Tom van der Lee <t0m.vd.l33@gmail.com>2022-12-11 13:49:10 +0100
commitd60f6dc50ea3ea7066fc5e267076276939e2cbc2 (patch)
tree08544e7b3c880d65fb279ad749ab6769372e797b /day9/__init__.py
parent43c89839d73a4da4a14e0853f161f7f5362e5638 (diff)
download2022-d60f6dc50ea3ea7066fc5e267076276939e2cbc2.tar.gz
2022-d60f6dc50ea3ea7066fc5e267076276939e2cbc2.tar.bz2
2022-d60f6dc50ea3ea7066fc5e267076276939e2cbc2.zip
Day 9 [WIP]
Diffstat (limited to 'day9/__init__.py')
-rw-r--r--day9/__init__.py108
1 files changed, 108 insertions, 0 deletions
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 @@
1# -*- coding: utf-8 -*-
2from abc import ABC
3from copy import copy
4from dataclasses import dataclass
5from typing import Tuple, Iterator, List
6
7from aoc import BaseAssignment
8
9
10@dataclass
11class Coordinate:
12 x: int
13 y: int
14
15 def __hash__(self):
16 return tuple([self.x, self.y]).__hash__()
17
18 def __sub__(self, other: "Coordinate"):
19 return Coordinate(self.x - other.x, self.y - other.y)
20
21 @property
22 def polarity_x(self):
23 try:
24 return abs(self.x) / self.x
25 except ZeroDivisionError:
26 return 0
27
28 @property
29 def polarity_y(self):
30 try:
31 return abs(self.y) / self.y
32 except ZeroDivisionError:
33 return 0
34
35
36class Assignment(BaseAssignment, ABC):
37 def parse_line(self, item: str) -> Tuple[str, int]:
38 direction, amount = item.split(" ")
39 return direction, int(amount)
40
41 def move(self, direction: str, amount: int):
42 pass
43
44 @staticmethod
45 def next_head(head: Coordinate, direction: str):
46 match direction:
47 case "R":
48 head.x += 1
49 case "L":
50 head.x -= 1
51 case "U":
52 head.y += 1
53 case "D":
54 head.y -= 1
55
56 @staticmethod
57 def next_tail(head: Coordinate, tail: Coordinate):
58 delta = head - tail
59
60 if abs(delta.x) > 1 and delta.y == 0:
61 tail.x += delta.x - delta.polarity_x
62 if abs(delta.x) > 1 and abs(delta.y) == 1:
63 tail.x += delta.x - delta.polarity_x
64 tail.y += delta.y
65 if abs(delta.y > 1) and delta.x == 0:
66 tail.y += delta.y - delta.polarity_y
67 if abs(delta.y) > 1 and abs(delta.x) == 1:
68 tail.y += delta.y - delta.polarity_y
69 tail.x += delta.x
70
71 def tail_positions(self, input: Iterator[str]) -> Iterator[Coordinate]:
72 head = Coordinate(0, 0)
73 tail = Coordinate(0, 0)
74
75 for line in input:
76 direction, amount = self.parse_line(line)
77
78 for _ in range(amount):
79 self.next_head(head, direction)
80 self.next_tail(head, tail)
81 yield tail
82
83
84class AssignmentOne(Assignment):
85 example_result = 13
86
87 def run(self, input: Iterator) -> int:
88 unique_tail_positions = set()
89
90 for position in self.tail_positions(input):
91 unique_tail_positions.add(copy(position))
92
93 print(unique_tail_positions)
94
95 for y in range(5):
96 items = []
97 for x in range(6):
98 if Coordinate(x, y) in unique_tail_positions:
99 items.append("#")
100 else:
101 items.append(".")
102 print("".join(items))
103
104 return len(unique_tail_positions)
105
106
107class AssignmentTwo(Assignment):
108 pass