-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday22.py
More file actions
196 lines (168 loc) · 6.3 KB
/
Copy pathday22.py
File metadata and controls
196 lines (168 loc) · 6.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
from __future__ import annotations
from rich.console import Console
from dataclasses import dataclass
from typing import List, Dict
from copy import deepcopy
console = Console()
@dataclass(order=True, frozen=True)
class Coords:
x: int
y: int
z: int
def __repr__(self) -> str:
return f"<{self.x},{self.y}>"
def init_procedure(instructions) -> int:
map: Dict[Coords, int] = {}
for instruct in instructions:
is_turn_on, xRange, yRange, zRange = instruct
if (
xRange[0] < -50
or xRange[1] > 50
or yRange[0] < -50
or yRange[1] > 50
or zRange[0] < -50
or zRange[1] > 50
):
continue
for x in range(xRange[0], xRange[1] + 1):
for y in range(yRange[0], yRange[1] + 1):
for z in range(zRange[0], zRange[1] + 1):
if is_turn_on:
map[Coords(x, y, z)] = 1
else:
map.pop(Coords(x, y, z), None)
# for x in range(xRange[0], xRange[1] + 1):
# for y in range(yRange[0], yRange[1] + 1):
# for z in range(zRange[0], zRange[1] + 1):
# if is_turn_on:
# map[Coords(0, y, z)] = 1
# else:
# map.pop(Coords(0, y, z), None)
# print(len(map))
return len(map)
def read_ranges(input: str):
commaSplit = input.split(",")
xRange = commaSplit[0].removeprefix("x=").split("..")
yRange = commaSplit[1].removeprefix("y=").split("..")
zRange = commaSplit[2].removeprefix("z=").split("..")
return (
(int(xRange[0]), int(xRange[1])),
(int(yRange[0]), int(yRange[1])),
(int(zRange[0]), int(zRange[1])),
)
def read_input(lines: List[str]):
results = []
for line in lines:
spaceSplit = line.strip().split()
is_on = spaceSplit[0] == "on"
ranges = read_ranges(spaceSplit[1])
results.append((is_on, *ranges))
return results
class Range:
def __init__(self, start: int, end: int, split: SplitMap):
self.start = start
self.end = end
self.split = split
def count_lit(self) -> int:
count = self.end - self.start + 1
if self.split is not None:
count *= self.split.count_lit()
return count
def __repr__(self):
map_str = "" if self.split is None else f":{self.split.map}"
return f"<{self.start},{self.end}>{map_str}"
class SplitMap:
def __init__(self):
self.map: List[Range] = []
def count_lit(self) -> int:
return sum([a.count_lit() for a in self.map])
def split_range(self, turn_on: bool, cuts):
# console.print(self.map)
# console.print(cuts)
(cutMin, cutMax) = cuts.pop(0)
newmap = []
while len(self.map) > 0:
interval = self.map[0]
if cutMin is None:
newmap.append(interval)
self.map.pop(0)
elif interval.end < cutMin:
newmap.append(interval)
self.map.pop(0)
elif interval.start > cutMax:
if turn_on:
newRange = Range(
cutMin, cutMax, None if len(cuts) == 0 else SplitMap()
)
if len(cuts) > 0:
newRange.split.split_range(turn_on, list(cuts))
newmap.append(newRange)
cutMin = None
else:
if cutMin < interval.start:
if turn_on:
newRange = Range(
cutMin,
interval.start - 1,
None if len(cuts) == 0 else SplitMap(),
)
if len(cuts) > 0:
newRange.split.split_range(turn_on, list(cuts))
newmap.append(newRange)
cutMin = interval.start
elif cutMin > interval.start:
newRange = Range(
interval.start, cutMin - 1, deepcopy(interval.split)
)
newmap.append(newRange)
interval.start = cutMin
assert interval.start == cutMin
if cutMax < interval.end:
newRange = Range(interval.start, cutMax, deepcopy(interval.split))
if len(cuts) > 0:
newRange.split.split_range(turn_on, list(cuts))
newmap.append(newRange)
elif turn_on:
newmap.append(newRange)
interval.start = cutMax + 1
newmap.append(interval)
self.map.pop(0)
cutMin = None
else:
if cutMax > interval.end:
cutMin = interval.end + 1
else:
cutMin = None
if len(cuts) > 0:
interval.split.split_range(turn_on, list(cuts))
newmap.append(interval)
elif turn_on:
newmap.append(interval)
self.map.pop(0)
if cutMin is not None and turn_on:
newRange = Range(cutMin, cutMax, None if len(cuts) == 0 else SplitMap())
if len(cuts) > 0:
newRange.split.split_range(turn_on, list(cuts))
newmap.append(newRange)
self.map = newmap
# console.print(self.map)
def range_splitter(instructions) -> int:
map = SplitMap()
for instruct in instructions:
is_turn_on, xRange, yRange, zRange = instruct
map.split_range(is_turn_on, [xRange, yRange, zRange])
# console.print(map.map)
# console.print(map.count_lit())
console.print(map.count_lit())
with open("day22.txt", "r") as file:
input = read_input(file.readlines())
console.print("[b yellow]Day 22[/b yellow]")
console.print(init_procedure(input))
# map = SplitMap()
# map.split_range(True, [(6, 10)])
# console.print(map.map)
# map.split_range(True, [(11, 20)])
# console.print(map.map)
# map.split_range(False, [(8, 17)])
# console.print(map.map)
range_splitter(input)