Some parallelism

This commit is contained in:
Christian
2024-12-06 08:55:31 +01:00
parent 981e1aaa3c
commit 74384ddd69
2 changed files with 19 additions and 15 deletions

View File

@@ -4,12 +4,14 @@ import dev.ctsk.aoc._
import scala.compiletime.ops.boolean import scala.compiletime.ops.boolean
import scala.util.boundary.break import scala.util.boundary.break
import scala.compiletime.ops.double import scala.compiletime.ops.double
import scala.collection.parallel.CollectionConverters._
object Day06 extends Solver(6): object Day06 extends Solver(6):
def patrol( def patrol(
grid: Array[Array[Char]], grid: Array[Array[Char]],
start: (Int, Int) start: (Int, Int),
obstacle: (Int, Int)
): (Boolean, () => Vector[(Int, Int)]) = ): (Boolean, () => Vector[(Int, Int)]) =
var seen = var seen =
Array.fill(4)(Array.fill(grid.length)(Array.fill(grid(0).length)(false))) Array.fill(4)(Array.fill(grid.length)(Array.fill(grid(0).length)(false)))
@@ -27,17 +29,18 @@ object Day06 extends Solver(6):
seen(dir)(pos._1)(pos._2) = true seen(dir)(pos._1)(pos._2) = true
while true do while true do
val next = (pos._1 + dirs(dir)._1, pos._2 + dirs(dir)._2) var next = (pos._1 + dirs(dir)._1, pos._2 + dirs(dir)._2)
if next._1 < 0 || next._1 >= grid.length if next._1 < 0 || next._1 >= grid.length
|| next._2 < 0 || next._2 >= grid(0).length || next._2 < 0 || next._2 >= grid(0).length
then return (false, visited) then return (false, visited)
else if grid(next._1)(next._2) == '#' else if grid(next._1)(next._2) == '#' || next == obstacle
then dir = (dir + 1) % 4 then dir = (dir + 1) % 4
else if seen(dir)(next._1)(next._2) else if seen(dir)(next._1)(next._2)
then return (true, visited) then return (true, visited)
else else
seen(dir)(next._1)(next._2) = true seen(dir)(next._1)(next._2) = true
pos = next pos = next
(false, visited) (false, visited)
def run(input: os.ReadablePath): (Timings, Solution) = def run(input: os.ReadablePath): (Timings, Solution) =
@@ -50,15 +53,15 @@ object Day06 extends Solver(6):
if lines(i)(j) == '^' if lines(i)(j) == '^'
yield (i, j)).head yield (i, j)).head
val visited = patrol(lines, start) val (p1_time, visited) = timed { patrol(lines, start, (-1, -1))._2() }
val p1 = visited._2().size val p1_solution = visited.length
var p2 = 0
val (p2_time, _) = timed {
for (i, j) <- visited._2()
do
lines(i)(j) = '#'
if patrol(lines, start)._1 then p2 += 1
lines(i)(j) = '.'
}
return (Timings(0, 0, p2_time), Solution(Int.box(p1), Int.box(p2))) val (p2_time, p2_solution) =
timed {
visited.par.count((i, j) => patrol(lines, start, (i, j))._1)
}
return (
Timings(0, p1_time, p2_time),
Solution(Int.box(p1_solution), Int.box(p2_solution))
)

View File

@@ -5,6 +5,7 @@ import mill._, scalalib._
object aoc extends ScalaModule { object aoc extends ScalaModule {
def scalaVersion = "3.5.2" def scalaVersion = "3.5.2"
def ivyDeps = Agg( def ivyDeps = Agg(
ivy"com.lihaoyi::os-lib:0.11.3" ivy"com.lihaoyi::os-lib:0.11.3",
ivy"org.scala-lang.modules::scala-parallel-collections:1.0.4"
) )
} }