Skip to content

Instantly share code, notes, and snippets.

@jul
Last active December 7, 2025 11:57
Show Gist options
  • Select an option

  • Save jul/7966f20ed12dc49146475e2905fd0597 to your computer and use it in GitHub Desktop.

Select an option

Save jul/7966f20ed12dc49146475e2905fd0597 to your computer and use it in GitHub Desktop.
advent7
from archery import mdict
input="""
......................................................................S......................................................................
.............................................................................................................................................
......................................................................^......................................................................
.............................................................................................................................................
.....................................................................^.^.....................................................................
.............................................................................................................................................
....................................................................^...^....................................................................
.............................................................................................................................................
...................................................................^.^.^.^...................................................................
.............................................................................................................................................
..................................................................^...^.^.^..................................................................
.............................................................................................................................................
.................................................................^.^.^.....^.................................................................
.............................................................................................................................................
................................................................^.^...^.^...^................................................................
.............................................................................................................................................
...............................................................^...^.^.^.^.^.^...............................................................
.............................................................................................................................................
..............................................................^.^...^.^.^.^...^..............................................................
.............................................................................................................................................
.............................................................^.^.^...^.^.^...^.^.............................................................
.............................................................................................................................................
............................................................^...^.^.^.^...^.^...^............................................................
.............................................................................................................................................
...........................................................^.^.^.^...^...^...^...^...........................................................
.............................................................................................................................................
..........................................................^.^.^.^...^.....^.^.^.^.^..........................................................
.............................................................................................................................................
.........................................................^.^...^...^...^.^.^.....^.^.........................................................
.............................................................................................................................................
........................................................^.....^.^.^.^.^.^.^.^.^...^.^........................................................
.............................................................................................................................................
.......................................................^.^.^...^...^.....^...^.......^.......................................................
.............................................................................................................................................
......................................................^.^.^...^.......^.^.....^.^...^.^......................................................
.............................................................................................................................................
.....................................................^.^.^.^.....^.^.^.^...^.^...^.^.^.^.....................................................
.............................................................................................................................................
....................................................^.^.^.^.....^...^.^.^...^...^.^...^.^....................................................
.............................................................................................................................................
...................................................^.^...^.^.^.^.^.^.^.^...^.....^.^.^...^...................................................
.............................................................................................................................................
..................................................^.^.^.^.^...^.^.^.^.^.......^.^.^.^.^...^..................................................
.............................................................................................................................................
.................................................^.^.^.^.^.....^...^.^...^...^.^.^.^.^.^...^.................................................
.............................................................................................................................................
................................................^.^.^...^.^.^...^.^.......^.^.^.^.^.^.^.....^................................................
.............................................................................................................................................
...............................................^.^.^.^.^.....^...^.^...^...^.^.^.^.^.^...^.^.^...............................................
.............................................................................................................................................
..............................................^.....^.^...^.^.^.^.^...^.^.^.^.^.^.^.....^.^...^..............................................
.............................................................................................................................................
.............................................^.^.^.^.^...^.....^...^.^...^.^.^.....^.....^...^.^.............................................
.............................................................................................................................................
............................................^.^.^.^.^.^.^.^.....^.......^.^.^.^.^...^.^...^.^...^............................................
.............................................................................................................................................
...........................................^...^...^.^.....^.^.^.^...^...^.^.^.^...^.^.^.......^.^...........................................
.............................................................................................................................................
..........................................^.^.^.^.^.^.^.^.^.^.....^...^.....^.^.^.^.^.^.^.^.^...^.^..........................................
.............................................................................................................................................
.........................................^.^.^.^.^.^.......^...^.^.^.^.^.^.^...^...^.^.^.^.^.^.^...^.........................................
.............................................................................................................................................
........................................^.........^.^.....^.^.^.^.^...^.^...........^...^.^.^...^.^.^........................................
.............................................................................................................................................
.......................................^.^.^.^...^.^.^...^.^...^.^...^.^...^.^...^...^.^.^.........^.^.......................................
.............................................................................................................................................
......................................^.^.^.^.^.....^.^.^.^.^.^.^...^.^.^.^.........^...^...........^.^......................................
.............................................................................................................................................
.....................................^...^.....^...^.^.^.....^.....^...^.^...^.^.^...^.^.^.^...^.^.^.^.^.....................................
.............................................................................................................................................
....................................^.^.^.^.^...^...^...^.........^.^.^.^.^.^.^.^.^.^...^.^...^.^.......^....................................
.............................................................................................................................................
...................................^.^...^...........^.^.^.^...^...^.^.^.^.^.^.^.^.^.^.^.^.^.^.^.^...^.^.^...................................
.............................................................................................................................................
..................................^...^.^.^...^.....^.^.^...^.^.^.^.....^...^.^.^.....^.^...^.^.^...^.^.^.^..................................
.............................................................................................................................................
.................................^...^.^.^...^.^.^.^.^.^.^.^.^...^...^.^...^...^.....^...^...^.^...^.^...^.^.................................
.............................................................................................................................................
................................^.^.^.^...^.^.^...^.....^.^.....^...^...^.^...^.^.^.^...^.^.^.^.^.^...^.^.^.^................................
.............................................................................................................................................
...............................^...^.^...^.^.....^.^...^.^...^.^.^.......^.^.^.^...^...^.^.^.^.^.^.^.^...^.^.^...............................
.............................................................................................................................................
..............................^.^.^.^.^.^...^...^...^...^.^.^.^.^.....^.^.^.^...^.^.^.^.^.^...^.^...^.^...^.^.^..............................
.............................................................................................................................................
.............................^.^.^.^.^.......^.^.^.....^.^.........^.^.^...^.^.^.^.^.^.^.^.^.^.^...^...^.^...^.^.............................
.............................................................................................................................................
............................^.^.^.^.^.^.^.^.^...^...^.^.....^.^.^.^.^.^.^.^.^.^.^.^.^.^.^...^.^.^.^.^.^.^.......^............................
.............................................................................................................................................
...........................^.^...^...^.^.^.^.^...^.^.^.^...^...^...^.^.^.^.^.^...^.^.^...^...^.^.^...^.^...^...^.^...........................
.............................................................................................................................................
..........................^.....^...^.....^.^.^.^...^.^.^...^...^.^...^.^.^.^...^.....^.^.^...^.^.^...^.^.^.^...^.^..........................
.............................................................................................................................................
.........................^.^.^...^.^.^.^.^.^.^.^.^.^.^.....^...^.^...^.^.^.^...^...^.....^.^.^.....^...^.^.^.^.^.^.^.........................
.............................................................................................................................................
........................^.^...^.^...^.^.^...^...^.^...^.^.^...^.^.^.^.^.^.^.^.^.^...^...^...^.^.^.^.^.^.^.^.^.^.^.^.^........................
.............................................................................................................................................
.......................^.^...^.^.^.^.^.^.^.^.^.^.^.....^.^.^.^.^...^...^.^...^.^...^.^.^...^.^.^.........^...^.^.^...^.......................
.............................................................................................................................................
......................^.^.^.^...^...^.^.^...^.^.^.^.^.^.^.^.^.^...^.^.^...^...^.^...^.....^.^...^.^.....^.....^...^.^.^......................
.............................................................................................................................................
.....................^...^.^.^.....^.....^...^.....^.^.^.^.^.^.^.^...^.^...^.^.^.^.^.^.^.^.^.....^.^.^.^.....^.^...^.^.^.....................
.............................................................................................................................................
....................^.^.^.^.^...^.....^.^.....^.^.^.^.^.^.^.^.^.^.^.^.^.^.^...^...^...^...^.^.^.^.....^.^.....^.^.^.^.^.^....................
.............................................................................................................................................
...................^.^.^.^.^.^.^.^.....^.^.^.^.^.^.^.^.^.......^.^.^.^.^.^...^.^.^...^.......^.^.^.^.^.^.^.^.^.^...^.^.^.^...................
.............................................................................................................................................
..................^...^.^.^.^...^.^.^.^.....^...^.^.^.^.^.^...^...^.^.^...^...^.....^.^.^.^.....^...^.^...^...^.....^.^.^.^..................
.............................................................................................................................................
.................^.^...^...^.^.....^...^.^.^.^.^.^.^...^.^...^...^.^...^.^.^.^.^.^...^.^.....^.^.^.^...^.^.^.^...^.^.......^.................
.............................................................................................................................................
................^...^.^.....^.^...^.^.^.^.^.^.^.^.^.^...^.^.......^.^.^.^.^.^.^.....^.^.....^.^.^...^.^.^...^.^...^...^.^.^.^................
.............................................................................................................................................
...............^.^...^.^.^...^...^.^.^.^.....^.^.^...^...^.^.^.^.^...^.........^...^.^.^.^...^.^.^.^...^.^.^...^.^...^.^.^.^.^...............
.............................................................................................................................................
..............^.^...^.^.^...^...^.^.^.^.^.^.^.....^.^.^...^...^...^.^.^...^.....^...^.^.^.^.....^...^.....^...^.^.^.^.^.....^.^..............
.............................................................................................................................................
.............^.^.^.^.^.^.^.....^.^.^.^.^.^.^.^.^...^.....^.^.^.^...^...^.^.^...^.^.^...........^.^.^...^.^.......^...^.^.^.^...^.............
.............................................................................................................................................
............^.^.^.^.......^.^.^.^.....^...^.^.^...^.^.^.^.^.^.^...^...^.^.^.^...^.^.^.^.^.^.........^...^.^.^.^.^.^...^...^.^.^.^............
.............................................................................................................................................
...........^.^.^.^...^.^.^...^...^...^...^.^.^.^.^.^.^.^.^.^...^...^...^...^.^.^.^.^.^.....^.^.^.^.........^.^.^...^.^.^.^.....^.^...........
.............................................................................................................................................
..........^.^.^...^.^.^.^.^.^.^.^.....^.^.....^.^...^.^...^...^.^...^.^.^...^.^.^.^.^.^.^.....^...^.^.^.^.^.^.......^.^.^.^.^.^.^.^..........
.............................................................................................................................................
.........^...^.^...^.......^...^.^...^...^...^.^.^...^...^.^.^.^.^.....^.....^...^.^...^.^.^.^...^.^.^.^.^.....^.^...^.^.^.^...^.^.^.........
.............................................................................................................................................
........^.^.^.^.^.........^.....^.^.....^...^.......^.^.^.^.......^...^.^...^.^.^.^.^.^.^...^.^...^...^.......^.^.^.^.^.^.^.^...^...^........
.............................................................................................................................................
.......^.^.^.^...^...^.....^.^.^.....^.^.^.^.^.....^...^.^.^.^.^.^.....^.^.^.^...^.^.^.....^.......^.^.^...^.....^.^.^.^.^.^.^.^.....^.......
.............................................................................................................................................
......^.^.^.^.^.^.^.^.^.^.^.^.....^...^.^.^.^.^.^...^.^...^...^...^...^.^.^.^...^...^...^.^.....^.^...^.^.^.^.^...^.....^.^...^.^.^.^.^......
.............................................................................................................................................
.....^...^.^.^.^.^.^...^.^.^.^.^.^.^.^...^.^.....^.^.^.^.^.^.^.^.^.^.....^.^...^.^.^.^.^...^.^.^...^.^...^.....^...^.^.^.^.^.^.^.^.^...^.....
.............................................................................................................................................
....^.^.^...^.^...^.^.^...^.^...^...^.^...^.^.^...^...^...^.^.^.^.^.^.^.^.^.^.....^.^...........^...^.^.^.^.^...^.^...^.^.^...^...^.^.^.^....
.............................................................................................................................................
...^.^.^...^.^...^...^.^.^.^...^.^.^.^.^.^.^.^...^.^.......^.^.....^.^.^...^.^.^.^.^.^.^.^.^.^.........^.^.^.^...^.^.^.^.^.^.^...^.^.^...^...
.............................................................................................................................................
..^.^.^.^.^.^.^.^.^.^...^...^.....^...^.^.^.^...^...^.^...^.^.^.^.^...^.^.^...^.^.^.......^.^.^.^.^...^.^.^.^.^.^.^.^.^...^.^...^.^.^.....^..
.............................................................................................................................................
.^.^.^.^.^.^...^.^.^.^.^...^.^...^.^...^.^.^...^.^.^...^.^.^.^.^.....^.^.......^.^.^.^.^.^.....^...^.^.^.^.^.^.^.^...^.^.^.....^.^...^.^...^.
.............................................................................................................................................
"""
EMPTY=0
SOURCE=1
SPLITTER=2
BEAM=3
str_to_state=dict({
"." : EMPTY,
"S" : SOURCE,
"^" : SPLITTER,
"|" : BEAM,
})
intensity=mdict()
class Matrix:
def __init__(self,size_x,size_y, mutable_sequence):
"""construct a view of a dimension x and y on mutable_sequence
the mutable_sequence must have a dimension compliant with its size
"""
self.size_y=size_y
self.pattern = [ ".", "S", "^", "|" ]
self.size_x=size_x
self.matrix= mutable_sequence
def _oneD_offset(self,ix,iy):
x=ix%self.size_x
y=iy%self.size_y
offset = y*self.size_x+x
if offset >= self.size_x * self.size_y :
print("%d(%d), %d(%d) => %d BOOOM"% (x,ix, y,iy, offset))
return offset
def get(self,x,y):
"""get item at coordinates x,y"""
return self.matrix[self._oneD_offset(x,y)]
def set(self,x,y,val):
"""set value val at coordinates x, y"""
self.matrix[self._oneD_offset(x,y)]=val
def __str__(self):
"""poor man's amazing graphical effects:)"""
to_print=" "
to_print+="' " * (self.size_y//5 )
for x in range(self.size_x):
for y in range(self.size_y):
if (y==0):
to_print+='\n ' if x%5 else "\n-"
#import pdb;pdb.set_trace()
to_print+="%1s%s" % ( self.pattern[self.get(x,y)],(" " ," ")[bool(self.get(x,y))])
return to_print
input2="""
.......S.......
...............
.......^.......
...............
......^.^......
...............
.....^.^.^.....
...............
....^.^...^....
...............
...^.^...^.^...
...............
..^...^.....^..
...............
.^.^.^.^.^...^.
...............
"""
lol=input.split()
X=len(lol)
Y=len(lol[0])
grid = Matrix(X,Y,[0]* X * Y)
for x, line in enumerate(lol):
for y, col in enumerate(line):
grid.set(x,y,str_to_state[col])
print(grid)
new_grid = grid
total=0
split=mdict()
while not total:
grid.matrix=new_grid.matrix
for x in range(X):
for y in range(Y):
state = grid.get(x,y)
new_grid.set(x,y, state)
if state in { SOURCE, BEAM }:
if x==X-1:
total+=1
else:
if grid.get(x+1,y) == SPLITTER:
split[(x,y)]=1
new_grid.set(x+1, y+1,BEAM)
intensity+=mdict({ (x+1,y+1) : int(state==SOURCE) or intensity[(x,y)] })
new_grid.set(x+1, y-1,BEAM)
intensity+=mdict({ (x+1,y-1) : int(state==SOURCE) or intensity[(x,y)] })
else:
new_grid.set(x+1, y,BEAM)
intensity+=mdict({ (x+1,y) : int(state==SOURCE) or intensity[(x,y)] })
print(total)
x=X-1
for y in range(Y):
print(f"{x},{y}= {intensity.get((x,y))}")
print(sum(split.values()))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment