8000 Optimize 2020 day 20 part 2 by orienting sea monster instead of grid · sim642/adventofcode@a7c423a · GitHub
[go: up one dir, main page]

Skip to content

Commit a7c423a

Browse files
committed
Optimize 2020 day 20 part 2 by orienting sea monster instead of grid
1 parent b9190fa commit a7c423a

File tree

1 file changed

+10
-8
lines changed

1 file changed

+10
-8
lines changed

src/main/scala/eu/sim642/adventofcode2020/Day20.scala

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ object Day20 {
127127
}
128128

129129
private val seaMonster =
130-
""" #
130+
""" #.
131131
|# ## ## ###
132132
| # # # # # # """.stripMargin
133133

@@ -144,13 +144,14 @@ object Day20 {
144144
} yield Pos(x, y)).toSet
145145
}
146146

147-
val seaMonsterGrid = seaMonster.linesIterator.map(_.toVector).toVector.mapGrid(_ == '#')
148-
val seaMonsterPoss = booleanGrid2Poss(seaMonsterGrid)
149-
val seaMonsterSize = Pos(seaMonsterGrid.head.length, seaMonsterGrid.length)
147+
val gridPoss = booleanGrid2Poss(grid)
148+
val gridSize = Pos(grid.head.length, grid.length)
149+
150+
// orienting sea monster is slightly faster than orienting grid
150151

151-
def checkSeaMonstersGrid(grid: Grid[Boolean]): Option[Int] = {
152-
val gridPoss = booleanGrid2Poss(grid)
153-
val gridSize = Pos(grid.head.length, grid.length)
152+
def checkGridSeaMonster(seaMonsterGrid: Grid[Boolean]): Option[Int] = {
153+
val seaMonsterPoss = booleanGrid2Poss(seaMonsterGrid)
154+
val seaMonsterSize = Pos(seaMonsterGrid.head.length, seaMonsterGrid.length)
154155

155156
Box(Pos.zero, gridSize - seaMonsterSize - Pos(1, 1)).iterator
156157
.map(pos => seaMonsterPoss.map(pos + _))
@@ -159,7 +160,8 @@ object Day20 {
159160
.map(seaMonsterPoss => (gridPoss -- seaMonsterPoss).size)
160161
}
161162

162-
grid.orientations.flatMap(checkSeaMonstersGrid).head
163+
val seaMonsterGrid = seaMonster.linesIterator.map(_.toVector).toVector.mapGrid(_ == '#')
164+
seaMonsterGrid.orientations.flatMap(checkGridSeaMonster).head
163165
}
164166

165167
private val tileIdRegex = """Tile (\d+):""".r

0 commit comments

Comments
 (0)
0