Day 23
This commit is contained in:
@@ -24,7 +24,8 @@ val solvers = Map[Int, Solver](
|
||||
19 -> Day19,
|
||||
20 -> Day20,
|
||||
21 -> Day21,
|
||||
22 -> Day22
|
||||
22 -> Day22,
|
||||
23 -> Day23
|
||||
)
|
||||
|
||||
def runSolver(solver: Solver, input: os.Path): Unit =
|
||||
@@ -34,16 +35,14 @@ def runSolver(solver: Solver, input: os.Path): Unit =
|
||||
println(solution.p1)
|
||||
println(solution.p2)
|
||||
case None =>
|
||||
println(f"==============================================")
|
||||
println(f"Day ${solver.day}%02d ---------------------------------------")
|
||||
println(f"Prep ${timings.prep}%38s μs")
|
||||
println(f"Part 1 ${solution.p1}%20s ${timings.p1}%15s μs")
|
||||
println(f"Part 2 ${solution.p2}%20s ${timings.p2}%15s μs")
|
||||
println(f"----------------------------------------------")
|
||||
|
||||
println(f"Total ${timings.total}%37s μs")
|
||||
|
||||
println(f"==============================================")
|
||||
println(f"=============================================================")
|
||||
println(f"Day ${solver.day}%02d ------------------------------------------------------")
|
||||
println(f"Prep ${timings.prep}%53s μs")
|
||||
println(f"Part 1 ${solution.p1}%40s ${timings.p1}%10s μs")
|
||||
println(f"Part 2 ${solution.p2}%40s ${timings.p2}%10s μs")
|
||||
println(f"-------------------------------------------------------------")
|
||||
println(f"Total ${timings.total}%52s μs")
|
||||
println(f"=============================================================")
|
||||
|
||||
@main def main(day: String, input: String): Unit =
|
||||
val num = """(\d+)""".r
|
||||
|
||||
59
aoc/src/dev/ctsk/aoc/days/Day23.scala
Normal file
59
aoc/src/dev/ctsk/aoc/days/Day23.scala
Normal file
@@ -0,0 +1,59 @@
|
||||
package dev.ctsk.aoc.days
|
||||
|
||||
import dev.ctsk.aoc.*
|
||||
|
||||
object Day23 extends Solver(23):
|
||||
override def run(input: os.ReadablePath): (Timings, Solution) =
|
||||
val edge = "([^-]*)-([^-]*)".r
|
||||
|
||||
def id(s: String): Int = (s(0) - 'a') * 26 + (s(1) - 'a')
|
||||
|
||||
val (pre_time, graph) = timed {
|
||||
os.read
|
||||
.lines(input)
|
||||
.flatMap { case edge(from, to) => Seq((from, to), (to, from)) }
|
||||
.groupMap(_._1)(_._2)
|
||||
.view
|
||||
.mapValues(_.toSet)
|
||||
.toMap
|
||||
}
|
||||
|
||||
val (p1_time, triangles) = timed {
|
||||
(for
|
||||
(u, uOut) <- graph
|
||||
v <- uOut
|
||||
w <- graph(v).intersect(uOut)
|
||||
if Seq(u(0), v(0), w(0)).contains('t')
|
||||
yield Set(u, v, w)).toSet.size
|
||||
}
|
||||
|
||||
type Nodes = Set[String]
|
||||
def cliques(clique: Nodes, cand_ : Nodes, excl_ : Nodes): Set[Nodes] =
|
||||
if cand_.isEmpty && excl_.isEmpty then return Set(clique)
|
||||
if cand_.isEmpty then return Set.empty
|
||||
val pivot = cand_.maxBy(graph(_).size)
|
||||
|
||||
var candidates = cand_
|
||||
var excluded = excl_
|
||||
var acc = Set.empty[Set[String]]
|
||||
|
||||
for v <- cand_ -- graph(pivot)
|
||||
do
|
||||
acc ++= cliques(clique + v, candidates & graph(v), excluded & graph(v))
|
||||
candidates = candidates - v
|
||||
excluded = excluded + v
|
||||
|
||||
acc
|
||||
|
||||
val (p2_time, password) = timed {
|
||||
cliques(Set.empty, graph.keySet, Set.empty)
|
||||
.maxBy(_.size)
|
||||
.toArray
|
||||
.sorted
|
||||
.mkString(",")
|
||||
}
|
||||
|
||||
(
|
||||
Timings(pre_time, p1_time, p2_time),
|
||||
Solution(Int.box(triangles), password)
|
||||
)
|
||||
2
data/23.ans
Normal file
2
data/23.ans
Normal file
@@ -0,0 +1,2 @@
|
||||
1064
|
||||
aq,cc,ea,gc,jo,od,pa,rg,rv,ub,ul,vr,yy
|
||||
3380
data/23.in
Normal file
3380
data/23.in
Normal file
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user