| 2 | from collections import Counter |
| 3 | |
| 4 | def step(grid: list[str], times: int) -> list[str]: |
| 5 | for _ in range(times): |
| 6 | nextgrid = [] |
| 7 | for r in range(len(grid)): |
| 8 | row = "" |
| 9 | for c in range(len(grid[0])): |
| 10 | ns = [(r-1,c-1),(r-1,c),(r-1,c+1),(r,c-1),(r,c+1),(r+1,c-1),(r+1,c),(r+1,c+1)] |
| 11 | trees,yards = 0,0 |
| 12 | for r1,c1 in ns: |
| 13 | if r1 < 0 or r1 >= len(grid) or c1 < 0 or c1 >= len(grid[0]): |
| 14 | continue |
| 15 | match grid[r1][c1]: |
| 16 | case '|': trees += 1 |
| 17 | case '#': yards += 1 |
| 18 | match grid[r][c]: |
| 19 | case '.': row += '|' if trees >= 3 else '.' |
| 20 | case '|': row += '#' if yards >= 3 else '|' |
| 21 | case '#': row += '#' if yards > 0 and trees > 0 else '.' |
| 22 | nextgrid.append(row) |
| 23 | grid = nextgrid |
| 24 | return grid |
| 25 | |
| 26 | def resource_value(grid: list[str]) -> int: |
| 27 | c = Counter(tile for row in grid for tile in row) |