Compare commits
No commits in common. "db8befc81fbb50ff67bd268db5bb661953f12a4b" and "24b33b422179e67bf70f11f19be1a8fe7c432efc" have entirely different histories.
db8befc81f
...
24b33b4221
@ -4,9 +4,7 @@ op ch pad g =
|
||||
(g, (((rho g)[2]) rho ch)) ,% ((1 + (rho g)[1]) rho ch)
|
||||
|
||||
op adj g =
|
||||
h = g + (1 rot g) + (-1 rot g)
|
||||
v = h + (1 flip h) + (-1 flip h)
|
||||
v - 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)
|
||||
|
||||
op solve g =
|
||||
gx = 0 pad (g=="@")
|
||||
@ -23,6 +21,12 @@ op solve2 g =
|
||||
rx = remove gx
|
||||
+/,(gx - rx)
|
||||
|
||||
x = "@" == "." pad sample
|
||||
x
|
||||
adj x
|
||||
""
|
||||
x and 4 > adj x
|
||||
|
||||
solve sample
|
||||
solve2 sample
|
||||
|
||||
|
||||
1182
day05/input
1182
day05/input
File diff suppressed because it is too large
Load Diff
11
day05/sample
11
day05/sample
@ -1,11 +0,0 @@
|
||||
3-5
|
||||
10-14
|
||||
16-20
|
||||
12-18
|
||||
|
||||
1
|
||||
5
|
||||
8
|
||||
11
|
||||
17
|
||||
32
|
||||
57
day05/sol.py
57
day05/sol.py
@ -1,57 +0,0 @@
|
||||
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