summaryrefslogtreecommitdiffstats
path: root/day9
diff options
context:
space:
mode:
Diffstat (limited to 'day9')
-rw-r--r--day9/__init__.py108
-rw-r--r--day9/example.txt8
-rw-r--r--day9/input.txt2000
3 files changed, 2116 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
diff --git a/day9/example.txt b/day9/example.txt
new file mode 100644
index 0000000..9874df2
--- /dev/null
+++ b/day9/example.txt
@@ -0,0 +1,8 @@
1R 4
2U 4
3L 3
4D 1
5R 4
6D 1
7L 5
8R 2
diff --git a/day9/input.txt b/day9/input.txt
new file mode 100644
index 0000000..3da93c3
--- /dev/null
+++ b/day9/input.txt
@@ -0,0 +1,2000 @@
1L 2
2U 2
3L 1
4D 1
5U 1
6L 2
7R 2
8D 1
9U 2
10D 1
11R 2
12U 1
13L 2
14D 2
15L 2
16R 2
17L 2
18R 1
19L 2
20D 1
21U 1
22L 2
23U 1
24D 1
25R 2
26L 1
27R 1
28L 2
29U 2
30D 2
31U 1
32D 2
33R 1
34D 1
35R 2
36L 2
37D 1
38L 1
39U 2
40R 2
41L 2
42U 2
43D 1
44U 2
45D 2
46R 1
47U 2
48L 2
49D 2
50U 2
51D 2
52L 2
53R 1
54L 2
55D 2
56U 1
57R 2
58U 2
59L 1
60D 2
61R 2
62L 2
63R 1
64L 2
65D 1
66R 2
67L 2
68R 1
69L 2
70D 1
71U 1
72L 1
73D 1
74R 2
75D 1
76L 1
77D 1
78R 1
79D 1
80U 2
81R 1
82L 1
83U 2
84L 2
85D 2
86L 1
87U 1
88D 1
89L 1
90U 1
91D 1
92U 1
93R 2
94L 2
95U 2
96R 2
97L 2
98D 2
99U 1
100L 1
101R 1
102U 2
103R 1
104D 2
105R 1
106L 2
107R 2
108U 2
109D 2
110R 1
111U 2
112L 2
113R 1
114L 2
115D 3
116L 3
117D 3
118L 3
119U 3
120D 2
121R 2
122D 3
123L 1
124R 2
125D 2
126R 2
127D 3
128U 3
129D 1
130R 2
131D 3
132L 1
133D 1
134L 1
135D 3
136R 2
137U 2
138D 3
139R 2
140U 2
141