Last active
December 4, 2025 12:54
-
-
Save jul/71e664f23be629a8b410bdf60910dd4e to your computer and use it in GitHub Desktop.
advent of code day4
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 = [ ".", "@", "X" ] | |
| 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=""" | |
| ..@@.@@@@. | |
| @@@.@.@.@@ | |
| @@@@@.@.@@ | |
| @.@@@@..@. | |
| @@.@@@@.@@ | |
| .@@@@@@@.@ | |
| .@.@.@.@@@ | |
| @.@@@.@@@@ | |
| .@@@@@@@@. | |
| @.@.@@@.@. | |
| """ | |
| 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,col=="@") | |
| print(grid) | |
| new_grid = Matrix(X,Y,[0]* X * Y) | |
| total=0 | |
| diff=1 | |
| while diff: | |
| diff=0 | |
| for x in range(grid.size_x): | |
| for y in range(grid.size_y): | |
| new_grid.set(x,y,int(grid.get(x,y))) | |
| neighbour=0 | |
| if grid.get(x,y): | |
| for dx in (-1,0, 1): | |
| for dy in (-1,0,1): | |
| if X > x+dx >= 0 and Y > y+dy >=0 and (dx,dy) != (0,0): | |
| neighbour+=bool(grid.get(x+dx,y+dy)) | |
| if neighbour==0: | |
| neighbour=1 | |
| if 0 < neighbour < 4 : | |
| new_grid.set(x,y,0) | |
| total+=1 | |
| diff+=1 | |
| print(f"{diff} removed") | |
| print(new_grid) | |
| grid.matrix=new_grid.matrix.copy() | |
| print(total) | |
| """ | |
| ' ' | |
| -. . @|@|. @|@|@|@|. | |
| @|@|@|. @|. @|. @|@| | |
| @|@|@|@|@|. @|. @|@| | |
| @|. @|@|@|@|. . @|. | |
| @|@|. @|@|@|@|. @|@| | |
| -. @|@|@|@|@|@|@|. @| | |
| . @|. @|. @|. @|@|@| | |
| @|. @|@|@|. @|@|@|@| | |
| . @|@|@|@|@|@|@|@|. | |
| @|. @|. @|@|@|. @|. | |
| """ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment