36
loading...
This website collects cookies to deliver better user experience
578,391 -> 578,322
274,585 -> 651,962
482,348 -> 294,348
(\d+),(\d+) -> (\d+),(\d+)
. We also grab the max bounds of the coordinates as well.lines = open("day_5.txt").readlines()
coords = np.array([re.match('(\d+),(\d+) -> (\d+),(\d+)', line).groups() for line in lines]).astype(int)
size = np.max(coords)+1
hv = coords[(coords[:, 0] == coords[:, 2]) | (coords[:, 1] == coords[:, 3])]
def rrange(start, stop):
return range(start, stop+1) if stop >= start else range(start, stop-1, -1)
grid = np.zeros((size, size))
for x1, y1, x2, y2 in hv:
grid[rrange(y1, y2), rrange(x1, x2)] += 1
result = (grid >= 2).sum()
rrange()
function is a small helper to allow range()
to run backwards properly. Because range(1, 5)
works fine, but range(5, 1)
does not unless specifying the step size as -1 with range(5, 1, -1)
. The coordinates are also adjusted to make the values inclusive.grid = np.zeros((size, size))
for x1, y1, x2, y2 in coords:
grid[rrange(y1, y2), rrange(x1, x2)] += 1
result = (grid >= 2).sum()
import numpy as np
import re
lines = open("day_5.txt").readlines()
coords = np.array([re.match('(\d+),(\d+) -> (\d+),(\d+)', line).groups() for line in lines]).astype(int)
size = np.max(coords)+1
def rrange(start, stop):
return range(start, stop+1) if stop >= start else range(start, stop-1, -1)
grid = np.zeros((size, size))
hv = coords[(coords[:, 0] == coords[:, 2]) | (coords[:, 1] == coords[:, 3])]
for x1, y1, x2, y2 in hv:
grid[rrange(y1, y2), rrange(x1, x2)] += 1
result = (grid >= 2).sum()
print("Part 1 result:", result)
grid = np.zeros((size, size))
for x1, y1, x2, y2 in coords:
grid[rrange(y1, y2), rrange(x1, x2)] += 1
result = (grid >= 2).sum()
print("Part 2 result:", result)