Solve 2021 day 4 and 5
This commit is contained in:
39
2021/d5.py
Normal file
39
2021/d5.py
Normal file
@@ -0,0 +1,39 @@
|
||||
from lib import get_data
|
||||
from lib import ints
|
||||
from collections import defaultdict
|
||||
|
||||
data = get_data(__file__).strip()
|
||||
|
||||
counts = defaultdict(int)
|
||||
for line in data.splitlines():
|
||||
x1, y1, x2, y2 = ints(line)
|
||||
if x1 == x2 or y1 == y2:
|
||||
if x1 == x2:
|
||||
for y in range(min(y1, y2), max(y1, y2) + 1):
|
||||
counts[(x1, y)] += 1
|
||||
elif y1 == y2:
|
||||
for x in range(min(x1, x2), max(x1, x2) + 1):
|
||||
counts[(x, y1)] += 1
|
||||
else:
|
||||
assert False
|
||||
|
||||
r = sum(1 for v in counts.values() if v > 1)
|
||||
print(r)
|
||||
|
||||
|
||||
for line in data.splitlines():
|
||||
x1, y1, x2, y2 = ints(line)
|
||||
if x1 == x2 or y1 == y2:
|
||||
pass
|
||||
else:
|
||||
dx = 1 if x2 > x1 else -1
|
||||
dy = 1 if y2 > y1 else -1
|
||||
|
||||
while (x1, y1) != (x2, y2):
|
||||
counts[(x1, y1)] += 1
|
||||
x1 += dx
|
||||
y1 += dy
|
||||
counts[(x2, y2)] += 1
|
||||
|
||||
r = sum(1 for v in counts.values() if v > 1)
|
||||
print(r)
|
||||
Reference in New Issue
Block a user