Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion 2021/src/main/scala/aoc2021/Day01.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ object Day01 extends AoC:

val depths: Vector[Int] = lines.map(_.toInt)

def solve(l: Vector[Int]): Int = l.sliding(2).count(l => l(1) > l(0))
def solve(l: Vector[Int]): Int = l.sliding(2).count(i => i(1) > i(0))

override lazy val answer1: Int = solve(depths)
override lazy val answer2: Int = solve(depths.sliding(3).map(_.sum).toVector)
24 changes: 14 additions & 10 deletions 2021/src/main/scala/aoc2021/Day02.scala
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,19 @@ object Day02 extends AoC:
def move(n: Long): Sub = copy(heading = heading + n)
def tilt(n: Long): Sub = copy(aim = aim + n)

def run(is: Vector[Inst], part2: Boolean = false): Sub =
is.foldLeft(this): (cur,is) =>
def run1(is: Vector[Inst]): Sub =
is.foldLeft(this): (cur, is) =>
is match
case Up(d) =>
if part2 then cur.tilt(-d) else cur.dive(-d)
case Down(d) =>
if part2 then cur.tilt(d) else cur.dive(d)
case Forward(d) =>
if part2 then cur.move(d).dive(cur.aim * d) else cur.move(d)
case Up(d) => cur.dive(-d)
case Down(d) => cur.dive(d)
case Forward(d) => cur.move(d)

def run2(is: Vector[Inst]): Sub =
is.foldLeft(this): (cur, is) =>
is match
case Up(d) => cur.tilt(-d)
case Down(d) => cur.tilt(d)
case Forward(d) => cur.move(d).dive(cur.aim * d)

def solution: Long =
heading * depth
Expand All @@ -42,5 +46,5 @@ object Day02 extends AoC:

val instructions: Vector[Inst] = lines.map(Inst.fromLine)

override lazy val answer1: Long = Sub.init.run(instructions).solution
override lazy val answer2: Long = Sub.init.run(instructions, part2 = true).solution
override lazy val answer1: Long = Sub.init.run1(instructions).solution
override lazy val answer2: Long = Sub.init.run2(instructions).solution
14 changes: 7 additions & 7 deletions 2021/src/main/scala/aoc2021/Day03.scala
Original file line number Diff line number Diff line change
Expand Up @@ -27,20 +27,20 @@ object Day03 extends AoC:

def rating(bits: Vector[Bits], commonOf: Bits => Char): Int =
@tailrec
def filter(todo: Vector[Bits], idx: Int = 0): Bits =
def filter(todo: Vector[Bits], index: Int = 0): Bits =
if todo.size == 1 then
todo.head
else
val common = commonOf(todo.transpose.apply(idx))
val next = todo.filter(bit => bit(idx) == common)
filter(next, idx + 1)
val common = commonOf(todo.transpose.apply(index))
val next = todo.filter(bit => bit(index) == common)
filter(next, index + 1)
filter(bits).toInt


lazy val gamma: Int = diagnostics.transpose.map(_.mostCommon).toInt
lazy val epsilon: Int = diagnostics.transpose.map(_.leastCommon).toInt
override lazy val answer1: Int = gamma * epsilon

lazy val oxygenRating: Int = rating(diagnostics, _.mostCommon)
lazy val co2SchrubberRating: Int = rating(diagnostics, _.leastCommon)
override lazy val answer2: Int = oxygenRating * co2SchrubberRating
lazy val oxygenRating: Int = rating(diagnostics, _.mostCommon)
lazy val co2Rating: Int = rating(diagnostics, _.leastCommon)
override lazy val answer2: Int = oxygenRating * co2Rating
33 changes: 16 additions & 17 deletions 2021/src/main/scala/aoc2021/Day04.scala
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,20 @@ object Day04 extends AoC:

val Line: Regex = """\s*(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s*""".r

def boards: Vector[Board[Int]] =
val boards: Vector[Board[Int]] =
lines
.drop(1)
.tail
.filterNot(_.isBlank)
.grouped(5)
.map: lines =>
lines.foldLeft(Board.empty[Int]):
case (board, Line(n0,n1,n2,n3,n4)) =>
board.addRow(Vector(n0.toInt,n1.toInt,n2.toInt,n3.toInt,n4.toInt))
case (_,line) =>
case (board, Line(n0, n1, n2, n3, n4)) =>
board.addRow(Vector(n0.toInt, n1.toInt, n2.toInt, n3.toInt, n4.toInt))
case (_, line) =>
sys.error(s"unable to read line=$line")
.toVector

case class Board[A](rows: Vector[Vector[A]], draws: Vector[A] = Vector.empty):
case class Board[A](rows: Vector[Vector[A]], draws: Vector[A] = Vector.empty) derives CanEqual:

def addRow(row: Vector[A]): Board[A] =
copy(rows = rows :+ row)
Expand All @@ -55,19 +55,18 @@ object Day04 extends AoC:

object Board:

given [A] => CanEqual[Board[A], Board[A]] = CanEqual.derived

def empty[A]: Board[A] =
Board[A](Vector.empty)

def playWhoWinsFirst[A](draws: Vector[A], boards: Vector[Board[A]] = boards): Board[A] =
@tailrec
def playWhoWinsFirst[A](draws: Vector[A], boards: Vector[Board[A]]): Board[A] =
val round = boards.map(_.draw(draws.head))
round
.find(_.hasBingo)
.getOrElse(playWhoWinsFirst(draws.tail, round))
round.find(_.hasBingo) match
case Some(board) => board
case None => playWhoWinsFirst(draws.tail, round)

@tailrec
def playWhoWinsLast[A](draws: Vector[A], game: Vector[Board[A]] = boards)(using CanEqual[A, A]): Board[A] =
def playWhoWinsLast[A](draws: Vector[A], game: Vector[Board[A]])(using CanEqual[A, A]): Board[A] =
val round = game.map(_.draw(draws.head))
val todo = round.filterNot(_.hasBingo)
if todo.nonEmpty then
Expand All @@ -76,8 +75,8 @@ object Day04 extends AoC:
round.filter(_.lastDraw == draws.head).head


lazy val board1: Board[Int] = playWhoWinsFirst(draws)
override lazy val answer1: Int = board1.unmarked.sum * board1.lastDraw
lazy val board1: Board[Int] = playWhoWinsFirst(draws, boards)
override lazy val answer1: Int = board1.unmarked.sum * board1.lastDraw

lazy val board2: Board[Int] = playWhoWinsLast(draws)
override lazy val answer2: Int = board2.unmarked.sum * board2.lastDraw
lazy val board2: Board[Int] = playWhoWinsLast(draws, boards)
override lazy val answer2: Int = board2.unmarked.sum * board2.lastDraw
2 changes: 0 additions & 2 deletions nmcb/src/main/scala/nmcb/pos.scala
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package nmcb

import scala.CanEqual.derived

object pos:

enum Dir derives CanEqual:
Expand Down