mirror of
https://github.com/mx42/adventofcode.git
synced 2026-01-14 05:49:52 +01:00
Adding day 5
This commit is contained in:
14
day5/part1.scala
Normal file
14
day5/part1.scala
Normal file
@@ -0,0 +1,14 @@
|
||||
import scala.io.StdIn._
|
||||
|
||||
val ASCII_MIN_MAJ_INTERVAL = 32 // 'a' - 'A'
|
||||
|
||||
val res = readLine
|
||||
.foldLeft(""){
|
||||
case (acc, chr) => chr match {
|
||||
case _ if acc.isEmpty => chr.toString
|
||||
case _ if (acc.last - chr).abs == ASCII_MIN_MAJ_INTERVAL => acc.dropRight(1)
|
||||
case _ => acc + chr
|
||||
}
|
||||
}
|
||||
|
||||
println(res.size)
|
||||
21
day5/part2.scala
Normal file
21
day5/part2.scala
Normal file
@@ -0,0 +1,21 @@
|
||||
import scala.io.StdIn._
|
||||
|
||||
val ASCII_MIN_MAJ_INTERVAL = 32
|
||||
|
||||
def reactingPolymer(poly: String, unit: Char): String = unit match {
|
||||
case _ if poly.isEmpty => unit.toString
|
||||
case _ if (poly.last - unit).abs == ASCII_MIN_MAJ_INTERVAL => poly.dropRight(1)
|
||||
case _ => poly + unit
|
||||
}
|
||||
|
||||
val polymer_v1 = readLine.foldLeft("")(reactingPolymer)
|
||||
val unitTypes = polymer_v1.toLowerCase.toSet
|
||||
|
||||
unitTypes
|
||||
.map(u => (u, polymer_v1.filter(c => c != u && c + ASCII_MIN_MAJ_INTERVAL != u)))
|
||||
.map { case (u, poly) => (u, poly.foldLeft("")(reactingPolymer).size) }
|
||||
.toList
|
||||
.sortBy(_._2)
|
||||
.map { case (u, size) => s"Could reach size $size by removing $u" }
|
||||
.take(1)
|
||||
.foreach(println)
|
||||
Reference in New Issue
Block a user