From 43b718737105f113dc2b93739d055dcdbb869129 Mon Sep 17 00:00:00 2001 From: Daniel Flanagan Date: Fri, 11 Dec 2020 01:49:49 -0600 Subject: [PATCH] WIP part 2 --- 2020/src/day11.nim | 108 ++++++++++++++++++++++++++++++--------------- 1 file changed, 73 insertions(+), 35 deletions(-) diff --git a/2020/src/day11.nim b/2020/src/day11.nim index 7c7f0c7..8503874 100644 --- a/2020/src/day11.nim +++ b/2020/src/day11.nim @@ -1,29 +1,11 @@ -import streams, sequtils, strutils +import streams, sequtils type Seat = enum Floor, Empty, Occupied - Grid = seq[seq[Seat]] + Grid[T] = seq[seq[T]] -doAssert @[@[Occupied, Empty], @[Floor, Empty]] == @[@[Occupied, Empty], @[Floor, Empty]] - -proc gridSummary(g: Grid): (int, int) = - ## Used during debugging - var o, v = 0 - for y in 0..= 0 and sx >= 0 and sy < g.len and sx < g[sy].len: - result.add g[sy][sx] + 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): Grid = - #echo "Step" +proc stepGrid(g: Grid[Seat]): Grid[Seat] = for y in 0..= 0 and y >= 0 and y < g.len and x < g.row(y).len + +proc toSightSeat(g: Grid[Seat], x: int, y: int): SightSeat = + echo "toSightSeat" + result[0] = g.at(x, y) + for vy in -1..1: + for vx in -1..1: + if vy == 0 and vx == 0: continue + 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) + +proc asSightGrid(s: Stream): SightGrid = + let g = s.asGrid + for y in 0..= 6: + Empty + else: + Occupied + result[y].add (newSeat, sights) proc part2*(s: Stream): int = - 0 + # TODO: part2 has tons of allocating - needs cleaning up + var g = s.asSightGrid + var ng: Grid[SightSeat] + while g != ng: + ng = g + g = g.stepSightGrid + g.foldl(a.concat b).countIt(it[0] == Occupied)