Try an interactive version of this dialog: Sign up at solve.it.com, click Upload, and pass this URL.
from aocd import get_data
inp = get_data(year=2025, day=4)
len(inp), len(inp.splitlines()), len(inp.splitlines()[0]), inp[:100]
tensor([[0, 0, 1, 1, 0, 1, 1, 1, 1, 0],
[1, 1, 1, 0, 1, 0, 1, 0, 1, 1],
[1, 1, 1, 1, 1, 0, 1, 0, 1, 1],
[1, 0, 1, 1, 1, 1, 0, 0, 1, 0],
[1, 1, 0, 1, 1, 1, 1, 0, 1, 1],
[0, 1, 1, 1, 1, 1, 1, 1, 0, 1],
[0, 1, 0, 1, 0, 1, 0, 1, 1, 1],
[1, 0, 1, 1, 1, 0, 1, 1, 1, 1],
[0, 1, 1, 1, 1, 1, 1, 1, 1, 0],
[1, 0, 1, 0, 1, 1, 1, 0, 1, 0]])nsums = torch.nn.functional.conv2d(grid.unsqueeze(0).unsqueeze(0).float(),
kernel.unsqueeze(0).unsqueeze(0),
padding=1)
nsums
tensor([[[[2., 4., 4., 4., 3., 4., 4., 5., 4., 3.],
[4., 7., 7., 7., 5., 6., 5., 7., 6., 5.],
[5., 8., 7., 8., 6., 6., 3., 5., 5., 5.],
[5., 7., 7., 8., 8., 7., 4., 5., 5., 5.],
[4., 6., 7., 8., 9., 8., 6., 5., 5., 4.],
[4., 5., 7., 6., 8., 7., 7., 6., 7., 5.],
[3., 5., 7., 7., 7., 6., 7., 7., 8., 5.],
[3., 5., 7., 7., 7., 6., 7., 8., 8., 5.],
[3., 6., 6., 8., 7., 8., 7., 8., 6., 4.],
[2., 4., 4., 5., 5., 6., 5., 5., 3., 2.]]]])Copied!
def solvea(s):
grid = torch.tensor([[int(c=='@') for c in line] for line in s.splitlines()])
kernel = torch.ones((3, 3))
nsums = torch.nn.functional.conv2d(grid.unsqueeze(0).unsqueeze(0).float(), kernel.unsqueeze(0).unsqueeze(0), padding=1)
return torch.where((nsums[0][0] < 5) & (grid==1))[0].shape[0]
solvea(samp), solvea(inp)
def solvea(s):
grid = torch.tensor([[int(c=='@') for c in line] for line in s.splitlines()])
kernel = torch.ones((3, 3))
nsums = torch.nn.functional.conv2d(grid.unsqueeze(0).unsqueeze(0).float(), kernel.unsqueeze(0).unsqueeze(0), padding=1)
return torch.where((nsums[0][0] < 5) & (grid==1))[0].shape[0]
solvea(samp), solvea(inp)
def solveb(s):
total = 0
grid = torch.tensor([[int(c=='@') for c in line] for line in s.splitlines()])
while True:
nsums = torch.nn.functional.conv2d(grid.unsqueeze(0).unsqueeze(0).float(), kernel.unsqueeze(0).unsqueeze(0), padding=1)
locs = torch.where((nsums[0][0] < 5) & (grid==1))
grid[locs] = 0
total += locs[0].shape[0]
if locs[0].shape[0]==0: break
return total
solveb(samp), solveb(inp)