Skip to content

Instantly share code, notes, and snippets.

package lascala.aoc2021.day1_10.day7
def findMinCost(from: Int, to: Int, cost: Int => Int): Int =
val mid = (from + to) / 2
val fromCost = cost(from)
val toCost = cost(to)
if (from + 1) >= to then (fromCost min toCost)
else if fromCost >= toCost then findMinCost(mid, to, cost)
else findMinCost(from, mid, cost)
package lascala.aoc2021.day1_10.day6
opaque type School = Map[Int, BigInt]
extension (s: School)
def next: School =
val current: School = for (remainingDates, count) <- s if remainingDates > 0
yield (remainingDates - 1, count)
s.get(0).fold(current) { zeroesCount =>
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)) {
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)
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
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)
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)
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)
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")
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