From 12f551dbfef138880b29ba6abe4576714ae942cb Mon Sep 17 00:00:00 2001 From: blenovo Date: Sat, 2 Aug 2025 03:13:50 +0200 Subject: summer warmup sesh part 2 --- 2020/aoc2020-d11.py | 62 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 2020/aoc2020-d11.py (limited to '2020/aoc2020-d11.py') diff --git a/2020/aoc2020-d11.py b/2020/aoc2020-d11.py new file mode 100644 index 0000000..06c7626 --- /dev/null +++ b/2020/aoc2020-d11.py @@ -0,0 +1,62 @@ +#advent of code 2020 +#day 11 + +directions = [(-1,-1),(-1,0),(-1,1),(0,-1),(0,1),(1,-1),(1,0),(1,1)]; + +#for part 1 +def scan1(grid,pos): + around = ""; + y,x = pos; + for dy,dx in directions: + if (y+dy,x+dx) not in grid: continue; + around += grid[(y+dy,x+dx)]; + return around; +#for part 2 +def scan2(grid,pos): + around = ""; + for dy,dx in directions: + y,x = pos; + while True: + x += dx; + y += dy; + if (y,x) not in grid: + break; + elif grid[(y,x)] != ".": + around += grid[(y,x)]; + break; + return around; + +#correct function is passed as an argument (scan) +def simulate(grid,scan,lim): + score = 0; + while True: + gridDupe = dict(); + for coordinates in grid: + state = grid[coordinates]; + y,x = coordinates; + around = scan(grid,coordinates); + if around.count("#") == 0 and state == "L": + gridDupe[coordinates] = "#"; + elif around.count("#") >= lim and state == "#": + gridDupe[coordinates] = "L"; + for dupe in gridDupe: + grid[dupe] = gridDupe[dupe]; + if len(gridDupe) == 0: + break; + for coordinates in grid: + if grid[coordinates] == "#": score += 1; + return score; + +grid1 = dict(); +PuzzleInput = open("11.in","r"); +for y,line in enumerate(PuzzleInput): + line = line[:-1]; + for x, c in enumerate(line): + grid1[(y,x)] = c; +PuzzleInput.close(); +grid2 = grid1.copy(); + +p1 = simulate(grid1,scan1,4); +p2 = simulate(grid2,scan2,5); +print("part 1 =",p1); +print("part 2 =",p2); -- cgit v1.2.3