advent-of-code/2015/day03.py
Tyrel Souza b2ad0448ba 2015
2022-12-09 16:41:14 -05:00

68 lines
1.3 KiB
Python

import shared
import matrix
from collections import Counter
import numpy as np
size = 75
def pt2(dirs):
r = size
c = size
mx = np.array(matrix.matrix_of_size(r*2+1,c*2+1))
mx[r][c] = '2' # initial house
for d in dirs[::2]:
mx,r,c = move(mx, r, c, d)
r=size
c=size
for d in dirs[1::2]:
mx,r,c = move(mx, r, c, d)
matrix.pmx(mx,pad=False,space=False)
count(mx)
def pt1(dirs):
r = size
c = size
mx = np.array(matrix.matrix_of_size(r*2+1,c*2+1))
mx[r][c] = '1' # initial house
for d in dirs:
mx,r,c = move(mx, r, c, d)
matrix.pmx(mx,pad=False,space=False)
count(mx)
def move(mx, r, c, d):
h, w = mx.shape
d_r = 0
d_c = 0
match d:
case '<':
d_c = -1
case '>':
d_c = 1
case '^':
d_r = -1
case 'v':
d_r = 1
c += d_c
r += d_r
mx[r][c] += 1
return mx,r,c
def add_pad(mx, d):
return np.pad(mx, d, mode='constant', constant_values=0)
def count(mx):
flat = np.ravel(mx).tolist()
total = len([x for x in flat if x])
print(total)
def main():
with open(shared.get_fname(3), "r") as f:
inp = [x for x in f.read().rstrip()]
pt1(inp)
pt2(inp)
if __name__ == "__main__":
main()