diff --git a/2020/src/day11.nim b/2020/src/day11.nim index 8503874..a8d5f98 100644 --- a/2020/src/day11.nim +++ b/2020/src/day11.nim @@ -4,8 +4,8 @@ type Seat = enum Floor, Empty, Occupied Grid[T] = seq[seq[T]] -proc row[T](g: Grid[T], y: int): seq[T] = g[y] -proc at[T](g: Grid[T], x: int, y: int): T = g.row(y)[x] +template row[T](g: Grid[T], y: int): seq[T] = g[y] +template at[T](g: Grid[T], x: int, y: int): T = g.row(y)[x] proc toSeat(c: char): Seat = case c @@ -22,12 +22,11 @@ proc getAdjacent(g: Grid[Seat], x: int, y: int): seq[Seat] = if sy >= 0 and sx >= 0 and sy < g.len and sx < g.row(sy).len: result.add g.at(sx, sy) -proc stepGrid(g: Grid[Seat]): Grid[Seat] = +proc stepGrid(g: Grid[Seat], ng: var Grid[Seat]) = for y in 0..= 0 and y >= 0 and y < g.len and x < g.row(y).len @@ -66,13 +64,13 @@ proc toSightSeat(g: Grid[Seat], x: int, y: int): SightSeat = var tx = x + vx var ty = y + vy while g.inBounds(tx, ty) and g.at(tx, ty) == Floor: - echo "Still...", tx, ty tx += vx ty += vy if g.inBounds(tx, ty): result[1].add (x: tx, y: ty) + echo (x, y, result) proc asSightGrid(s: Stream): SightGrid = - let g = s.asGrid + let g = s.asGrid() for y in 0..= 6: + if adj.countIt(it == Occupied) >= 5: Empty else: Occupied @@ -107,7 +104,10 @@ proc part2*(s: Stream): int = # TODO: part2 has tons of allocating - needs cleaning up var g = s.asSightGrid var ng: Grid[SightSeat] + var steps = 0 while g != ng: + echo "Step ", steps ng = g g = g.stepSightGrid + inc steps g.foldl(a.concat b).countIt(it[0] == Occupied)