-
Notifications
You must be signed in to change notification settings - Fork 1
/
day18.nim
47 lines (35 loc) · 1 KB
/
day18.nim
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import strutils
const instructions = readFile("./inputs/18.txt").splitLines
type
Grid = array[102, array[102, bool]]
func createGrid(instr: seq[string]): Grid =
for i, line in instr:
for j, c in line:
result[i+1][j+1] = c == '#'
func countNeighbours(grid: Grid, row, col: int): int =
for r in row-1 .. row+1:
for c in col-1 .. col+1:
if grid[r][c]:
inc result
func changeState(grid: Grid, secondPart = false): Grid =
for i in 1 .. 100:
for j in 1 .. 100:
let n = countNeighbours(grid, i, j)
if (grid[i][j] and n in 3..4) or n == 3:
result[i][j] = true
if secondPart:
result[1][1] = true
result[1][100] = true
result[100][1] = true
result[100][100] = true
func solve(grid: Grid, secondPart = false): int =
var grid = grid
for _ in 1 .. 100:
grid = changeState(grid, secondPart)
for row in grid:
for col in row:
if col:
inc result
const grid = createGrid(instructions)
echo solve(grid)
echo solve(grid, true)