This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package lascala.aoc2021.day1_10.day5 | |
opaque type Diagram = Map[(Int, Int), Int] | |
extension (d: Diagram) | |
def countValue(cond: Int => Boolean): Int = d.values.count(cond) | |
object Diagram: | |
def fromPoints(points: Seq[(Int, Int)]): Diagram = | |
points.foldLeft(Map.empty[(Int, Int), Int].withDefaultValue(0)) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package lascala.aoc2021.day1_10.day4 | |
case class Bingo( | |
board: BingoBoard, | |
marked: Marked, | |
) | |
extension (b: Bingo) | |
def checkBingo(n: Int): Option[Bingo] = b.board.indexOf(n) match | |
case None => Some(b) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package lascala.aoc2021.day1_10.day3 | |
case class DiagnosticReport( | |
width: Int, | |
bits: Seq[BigInt], | |
) | |
extension (bits: Seq[BigInt]) | |
def mostCommonBit(index: Int): Boolean = | |
bits.map(_.testBit(index)).count(_ == true) * 2 >= bits.size |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package lascala.aoc2021.day1_10.day2 | |
case class Position(horizontal: Int, depth: Int) | |
extension (p: Position) | |
def step(command: String): Position = | |
val (operator, value) = parseCommand(command) | |
operator match | |
case "forward" => p.copy(horizontal = p.horizontal + value) | |
case "down" => p.copy(depth = p.depth + value) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package lascala.aoc2021.day1_10.day1 | |
def countDeeper(depths: Seq[Int]): Int = | |
depths.init zip depths.tail count { case (a, b) => a < b } | |
def countDeeperWindoes(depths: Seq[Int]): Int = | |
val windows = depths.sliding(3, 1).map(_.sum).toSeq | |
countDeeper(windows) | |
def depths(s: String): Seq[Int] = s.split("\n").map(_.toInt) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package sungkmi.aoc2020.day25 | |
def loopSize(subjectNumber: Int, pubKey: Int): Int = | |
@annotation.tailrec | |
def loop(v: Int, i: Int): Int = | |
if v == pubKey then i else | |
val v1 = ((v.toLong * subjectNumber) % 20201227).toInt | |
loop(v1, i + 1) | |
loop(1, 0) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package sungkmi.aoc2020.day24 | |
// x: 3 o'clock, y: 1 o'clock | |
case class D(dx: Int, dy: Int) | |
object D: | |
val reference: D = D(0, 0) | |
extension (d: D) | |
@annotation.targetName("plus") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package sungkmi.aoc2020.day23 | |
case class CupCircle( | |
cups: Vector[Int], | |
pickupMap: Map[Int, Vector[Int]], | |
size: Int, | |
) { | |
override def equals(that: Any): Boolean = that match | |
case thatc: CupCircle => this.toList == thatc.toList | |
case _ => false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package sungkmi.aoc2020.day22 | |
import scala.collection.immutable.Queue | |
case class Decks(p1: Queue[Int], p2: Queue[Int]) | |
extension (d: Decks) | |
def step: Either[Boolean, Decks] = | |
//println(s"===> $d") | |
dealWithTopCards: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package sungkmi.aoc2020.day21 | |
case class Ingredient(name: String) extends AnyVal | |
case class Allergen(name: String) extends AnyVal | |
type Food = (Set[Ingredient], Set[Allergen]) | |
type Pair = (Allergen, Ingredient) | |
def parseLine(line: String): Food = | |
val Array(f, e) = line `split` " \\(contains " | |
val ingredients = (f `split` " ").toSet.map(Ingredient(_)) |