100 lines
2.5 KiB
Python
100 lines
2.5 KiB
Python
#!/bin/python3
|
|
import sys,time,re,os,copy
|
|
from pprint import pprint
|
|
sys.path.insert(0, '../../')
|
|
from fred import *
|
|
|
|
start_time = time.time()
|
|
|
|
if sys.argv[1] == 'test':
|
|
input_f = 'test'
|
|
elif sys.argv[1] == 'input':
|
|
input_f = 'input'
|
|
else:
|
|
print('No argv provided')
|
|
exit()
|
|
|
|
|
|
#########################################
|
|
# #
|
|
# Part 1 #
|
|
# #
|
|
#########################################
|
|
|
|
def shoot_rays(grid:list, pos:set, rays:list, score:list):
|
|
rays.append(pos)
|
|
down = get_value_in_direction(grid, pos, 'down')
|
|
|
|
if down in rays:
|
|
return rays,score
|
|
if down == '.':
|
|
pos = addTuples(pos,(1,0))
|
|
return shoot_rays(grid,pos,rays,score)
|
|
elif down == '^':
|
|
if addTuples(pos,(1,0)) not in score:
|
|
score.append(addTuples(pos,(1,0)))
|
|
left = addTuples(pos,(1,-1))
|
|
right = addTuples(pos,(1,1))
|
|
if not grid_valid(left[0],left[1],grid):
|
|
return rays,score
|
|
if left not in rays:
|
|
rays.append(left)
|
|
r, s = shoot_rays(grid,left,rays,score)
|
|
if right not in rays:
|
|
rays.append(right)
|
|
return shoot_rays(grid,right,rays,score)
|
|
|
|
return rays,score
|
|
|
|
|
|
def part1():
|
|
score = []
|
|
rays = []
|
|
grid = toGrid(input_f)
|
|
|
|
start = findInGrid(grid,'S')
|
|
|
|
rays, score = shoot_rays(grid,start,rays,score)
|
|
if sys.argv[1] == 'test':
|
|
nprint(grid,sign='|',positions=rays)
|
|
return len(set(score))
|
|
|
|
|
|
return score
|
|
|
|
start_time = time.time()
|
|
p1 = part1()
|
|
print('Part 1:',p1, '\t\t', round((time.time() - start_time)*1000), 'ms')
|
|
|
|
#########################################
|
|
# #
|
|
# Part 2 #
|
|
# #
|
|
#########################################
|
|
|
|
def part2():
|
|
score = []
|
|
rays = []
|
|
grid = toGrid(input_f)
|
|
|
|
start = findInGrid(grid,'S')
|
|
count = []
|
|
|
|
for i in range(0,len(grid[0])):
|
|
count.append(0)
|
|
|
|
count[start[1]] = 1
|
|
for x, cols in enumerate(grid):
|
|
for y, rows in enumerate(cols):
|
|
if grid[x][y] == '^':
|
|
count[y+1] += count[y]
|
|
count[y-1] += count[y]
|
|
count[y] = 0
|
|
print(count)
|
|
return sum(count)
|
|
|
|
start_time = time.time()
|
|
p2 = part2()
|
|
print('Part 2:',p2, '', round((time.time() - start_time)*1000), 'ms')
|
|
|