Compare commits
2 Commits
24b33b4221
...
db8befc81f
| Author | SHA1 | Date | |
|---|---|---|---|
| db8befc81f | |||
| 98b4fafc9c |
@ -4,7 +4,9 @@ op ch pad g =
|
|||||||
(g, (((rho g)[2]) rho ch)) ,% ((1 + (rho g)[1]) rho ch)
|
(g, (((rho g)[2]) rho ch)) ,% ((1 + (rho g)[1]) rho ch)
|
||||||
|
|
||||||
op adj g =
|
op adj g =
|
||||||
(1 rot g) + (-1 rot g) + (1 flip g) + (-1 flip g) + (1 rot 1 flip g) + (-1 rot 1 flip g) + (1 rot -1 flip g) + (-1 rot -1 flip g)
|
h = g + (1 rot g) + (-1 rot g)
|
||||||
|
v = h + (1 flip h) + (-1 flip h)
|
||||||
|
v - g
|
||||||
|
|
||||||
op solve g =
|
op solve g =
|
||||||
gx = 0 pad (g=="@")
|
gx = 0 pad (g=="@")
|
||||||
@ -21,12 +23,6 @@ op solve2 g =
|
|||||||
rx = remove gx
|
rx = remove gx
|
||||||
+/,(gx - rx)
|
+/,(gx - rx)
|
||||||
|
|
||||||
x = "@" == "." pad sample
|
|
||||||
x
|
|
||||||
adj x
|
|
||||||
""
|
|
||||||
x and 4 > adj x
|
|
||||||
|
|
||||||
solve sample
|
solve sample
|
||||||
solve2 sample
|
solve2 sample
|
||||||
|
|
||||||
|
|||||||
1182
day05/input
Normal file
1182
day05/input
Normal file
File diff suppressed because it is too large
Load Diff
11
day05/sample
Normal file
11
day05/sample
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
3-5
|
||||||
|
10-14
|
||||||
|
16-20
|
||||||
|
12-18
|
||||||
|
|
||||||
|
1
|
||||||
|
5
|
||||||
|
8
|
||||||
|
11
|
||||||
|
17
|
||||||
|
32
|
||||||
57
day05/sol.py
Normal file
57
day05/sol.py
Normal file
@ -0,0 +1,57 @@
|
|||||||
|
def parse(input):
|
||||||
|
ranges = []
|
||||||
|
ids = []
|
||||||
|
with open(input) as f:
|
||||||
|
for line in f:
|
||||||
|
line = line.strip()
|
||||||
|
if line == "":
|
||||||
|
break
|
||||||
|
lo, _, hi = line.partition("-")
|
||||||
|
ranges.append((int(lo), int(hi)))
|
||||||
|
for line in f:
|
||||||
|
ids.append(int(line.strip()))
|
||||||
|
return ranges, ids
|
||||||
|
|
||||||
|
def solve(input):
|
||||||
|
# Part 1: count the number of given ids which are within one of the ranges
|
||||||
|
ranges, ids = parse(input)
|
||||||
|
t = 0
|
||||||
|
for x in ids:
|
||||||
|
for lo,hi in ranges:
|
||||||
|
if lo <= x <= hi:
|
||||||
|
t += 1
|
||||||
|
break
|
||||||
|
print(t)
|
||||||
|
|
||||||
|
|
||||||
|
# Part 2: count the number of possible ids which are within one of the ranges --
|
||||||
|
# in other words, the size of the union of all the ranges.
|
||||||
|
#
|
||||||
|
# We start by flattening the ranges so that they don't overlap with each other
|
||||||
|
flattened = []
|
||||||
|
queue = list(ranges)
|
||||||
|
while queue:
|
||||||
|
lo, hi = queue.pop()
|
||||||
|
found = False
|
||||||
|
for i,(x,y) in enumerate(flattened):
|
||||||
|
if not (hi < x or y < lo):
|
||||||
|
found = True
|
||||||
|
break
|
||||||
|
if found:
|
||||||
|
#print("collapsing ({:,} {:,}) and ({:,} {:,}) -> ({:,} {:,})".format(x,y,lo,hi,min(x,lo),max(y,hi)))
|
||||||
|
# if x <= lo <= hi < y then the range is unchanged and we don't need to update flattened[i].
|
||||||
|
# otherwise we need to add the new range to the queue to see if it can be merged with any other entries.
|
||||||
|
if lo < x or hi > y:
|
||||||
|
del flattened[i]
|
||||||
|
queue.append((min(x,lo), max(y,hi)))
|
||||||
|
else:
|
||||||
|
flattened.append((lo,hi))
|
||||||
|
|
||||||
|
# Then we sum up their sizes. Ranges are inclusive, so +1.
|
||||||
|
t = 0
|
||||||
|
for x,y in flattened:
|
||||||
|
t += y-x + 1
|
||||||
|
print(t)
|
||||||
|
|
||||||
|
solve("sample")
|
||||||
|
solve("input")
|
||||||
Loading…
x
Reference in New Issue
Block a user