Skip to content

Instantly share code, notes, and snippets.

@JRuumis
Created December 18, 2022 12:22
Show Gist options
  • Save JRuumis/14faf3f2f22f6a78ae85dd9121e6ac57 to your computer and use it in GitHub Desktop.
Save JRuumis/14faf3f2f22f6a78ae85dd9121e6ac57 to your computer and use it in GitHub Desktop.
Day17 cycle detect
def gridReach(inputGrid: Grid, currentFront: Set[Coord], accuRocksToKeep: Set[Coord]): Set[Coord] = {
if(currentFront.isEmpty) accuRocksToKeep
else {
val neigboursToCheck: Set[Coord] = currentFront.flatMap(c => c.neighbours)
val checkedNeighbours: Set[(Coord, Char)] = neigboursToCheck.map(c => (c,gridGet(inputGrid,c)) match {
case (_, None) => None
case (_, Some('f')) => None
case (_, Some('w')) => None
case (c, Some('.')) => Some((c,'f'))
case (c, Some('#')) => Some((c,'#'))
//case (_, Some('*')) => None
}).filter(_.isDefined).map(_.get)
val newRocksToKeep: Set[Coord] = checkedNeighbours.filter{case(_,i) => i == '#'}.map{case(c,_) => c}
val newFront: Set[Coord] = checkedNeighbours.filter{case(_,i) => i == 'f'}.map{case(c,_) => c}
val gridNewFront: Grid = checkedNeighbours.foldLeft(inputGrid){ case(grid,(coord,char)) => gridUpdateCell(grid,coord,char) }
val gridChangedFrontToWater: Grid = currentFront.foldLeft(gridNewFront) { case(grid, c) => gridUpdateCell(grid, c, 'w') }
gridReach(gridChangedFrontToWater, newFront, accuRocksToKeep ++ newRocksToKeep)
}
}
def removeInvisibleFromBottom: Pit = {
val startFrontCoord: Coord = Coord(0,0)
val gridWithStartFrontSet: Grid = gridUpdateCell(this.grid, Coord(0,0), 'f')
val rocksToKeepCoords = gridReach(gridWithStartFrontSet, Set(startFrontCoord), Set())
val maxYForNewGrid: Int = if(rocksToKeepCoords.isEmpty) 0 else rocksToKeepCoords.map(_.y).max
val newGrid: Grid = (0 to maxYForNewGrid).toVector.map(y => (0 to gridWidth-1).toVector.map(x => {
if(rocksToKeepCoords contains Coord(y,x)) '#' else '.'
}))
Pit(newGrid, depth, shapes, moves, pushFall, fallingShapeCornerCoord)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment