Commit 11f67058 authored by Alexander Gehrke's avatar Alexander Gehrke
Browse files

Update to Scala 3.0.0-M2

parent 06c392ef
val dottyVersion = "3.0.0-M1"
val dottyVersion = "3.0.0-M2"
lazy val root = project
.in(file("."))
......
......@@ -8,14 +8,14 @@ def day2(passwords: List[String]): String =
// part 1, password must contain given amount of character
def validAmount(passLine: String): Boolean =
val (min, max, char, pass) = parse(passLine)
val (min, max, char, pass) = parsePass(passLine)
(min to max) contains pass.count(_ == char)
// part 2, password must contain character at exactly one of the given positions
def validPositions(passLine: String): Boolean =
val (a, b, char, pass) = parse(passLine)
val (a, b, char, pass) = parsePass(passLine)
pass(a-1) == char ^ pass(b-1) == char
def parse(passLine: String): (Int, Int, Char, String) =
def parsePass(passLine: String): (Int, Int, Char, String) =
val s"$min-$max $char:$pass" = passLine
(min.toInt, max.toInt, char(0), pass)
......@@ -4,11 +4,11 @@ import cats._
import cats.implicits.given
def day7(input: List[String]):String =
val rules = parseRules(input)
val rules = parseBags(input)
//findPossibleSuperbags(rules, "shiny gold").size.toString
subBags(rules, "shiny gold").toString
def parseRules(input: List[String]): BagRules =
def parseBags(input: List[String]): BagRules =
input.map(parseSingle).combineAll
val RuleFormat = raw"(.*) bags contain (?:(no other)|(.*)) bags?.".r
......
package aoc2020.lib
/* for splitting input with separator lines */
extension [A](input: List[A])(using Eql[A,A])
extension [A](input: List[A])(using CanEqual[A,A])
def split(separator: A, keepSeparator: Boolean = false): LazyList[List[A]] =
input.span(_ != separator) match {
case (Nil, Nil) => LazyList()
......
......@@ -51,25 +51,25 @@ object Vectors {
}
def manhattan: T = v._1.abs + v._2.abs
def neighbours: List[Vec2D[T]] =
neighbours(2).map(n => v + (n(0), n(1)))
neighbourCoords(2).map(n => v + (n(0), n(1)))
given [T: Numeric] as Vec[Vec3D[T]]:
extension (v: Vec3D[T])
def +(w: Vec3D[T]): Vec3D[T] = (v._1 + w._1, v._2 + w._2, v._3 + w._3)
def neighbours: List[Vec3D[T]] =
neighbours(3).map(n => v + (n(0), n(1), n(2)))
neighbourCoords(3).map(n => v + (n(0), n(1), n(2)))
given [T: Numeric] as Vec[Vec4D[T]]:
extension (v: Vec4D[T])
def +(w: Vec4D[T]): Vec4D[T] = (v._1 + w._1, v._2 + w._2, v._3 + w._3, v._4 + w._4)
def neighbours: List[Vec4D[T]] =
neighbours(4).map(n => v + (n(0), n(1), n(2), n(3)))
neighbourCoords(4).map(n => v + (n(0), n(1), n(2), n(3)))
/* compute these only once per type and dimension*/
import scala.collection.mutable
private var _neighbourCache = mutable.Map[(Numeric[_], Int), List[List[_]]]()
def neighbours[T](dim: Int)(using n: Numeric[T]): List[List[T]] =
def neighbourCoords[T](dim: Int)(using n: Numeric[T]): List[List[T]] =
_neighbourCache.get((n, dim)) match {
case None =>
val self = List.fill(dim)(n.zero)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment