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
3 changes: 2 additions & 1 deletion 2015/src/main/scala/aoc2015/Day01.scala
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package aoc2015

import nmcb.*
import nmcb.predef.*

object Day01 extends AoC:

Expand Down Expand Up @@ -28,4 +29,4 @@ object Day01 extends AoC:
.zipWithIndex
.find((f,_) => f == -1)
.getOrElse(sys.error("not found"))
._2
.index
7 changes: 4 additions & 3 deletions 2015/src/main/scala/aoc2015/Day07.scala
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ object Day07 extends AoC:
/** Modeling */

type Wire = String
type Env = Map[Wire,Int]
type Env = Map[Wire, Int]

sealed trait Rule:
def args: List[Wire]
Expand All @@ -19,10 +19,11 @@ object Day07 extends AoC:

def run(env: Env): Option[Int] =
val List(lhs, rhs) = args.map(env.get)
for {
for
v1 <- lhs
v2 <- rhs
} yield op(v1)(v2)
yield
op(v1)(v2)

case class Op1(op: Int => Int, args: List[Wire], ret: Wire) extends Rule:

Expand Down
17 changes: 5 additions & 12 deletions 2015/src/main/scala/aoc2015/Day08.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@ import scala.annotation.*

object Day08 extends AoC:

case class Str(quoted: String):
extension (quoted: String)

def unquoted: String =
quoted.drop(1).dropRight(1)

def unescaped: String =
@tailrec
def loop(todo: List[Char], ret: String = ""): String =
todo match
case Nil =>
Expand All @@ -27,6 +28,7 @@ object Day08 extends AoC:
loop(unquoted.toList)

def escaped: String =
@tailrec
def loop(todo: List[Char], ret: String = ""): String =
todo match
case Nil => "\"" + ret + "\""
Expand All @@ -36,14 +38,5 @@ object Day08 extends AoC:

loop(quoted.toList)

val strings: IndexedSeq[Str] =
def parser(s: String): Str =
s match
case s"""$unescaped""" => Str(unescaped)
case str: String => sys.error(s"could not parse '$str'")

lines.map(parser)


lazy val answer1: Int = strings.map(str => str.quoted.size - str.unescaped.size).sum
lazy val answer2: Int = strings.map(str => str.escaped.size - str.quoted.size).sum
lazy val answer1: Int = lines.map(str => str.length - str.unescaped.length).sum
lazy val answer2: Int = lines.map(str => str.escaped.length - str.length).sum
3 changes: 2 additions & 1 deletion 2015/src/main/scala/aoc2015/Day17.scala
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package aoc2015

import nmcb.*
import nmcb.predef.*

object Day17 extends AoC:

Expand All @@ -11,7 +12,7 @@ object Day17 extends AoC:
.zipWithIndex
.toSet
.subsets
.map(_.toList.map(_._1))
.map(_.toList.map(_.element))
.filter(_.sum == 150)
.toList

Expand Down
5 changes: 3 additions & 2 deletions 2015/src/main/scala/aoc2015/Day20.scala
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package aoc2015

import nmcb.*
import nmcb.predef.*

object Day20 extends AoC:

Expand All @@ -18,7 +19,7 @@ object Day20 extends AoC:
.zipWithIndex
.find((count,_) => count >= puzzle)
.getOrElse(sys.error("not found"))
._2
.index

lazy val answer2: Int =
val size = puzzle / 10 + 1
Expand All @@ -34,4 +35,4 @@ object Day20 extends AoC:
.zipWithIndex
.find((count,_) => count >= puzzle)
.getOrElse(sys.error("not found"))
._2
.index
8 changes: 2 additions & 6 deletions 2016/src/main/scala/aoc2016/Day22.scala
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,6 @@ object Day22 extends AoC:
yield
(a,b)

extension (location: (Pos, Node))
def pos: Pos = location._1
def node: Node = location._2

extension (nodes: Map[Pos, Node])
def maxX: Int = nodes.maxBy(_.pos.x).pos.x
def maxY: Int = nodes.maxBy(_.pos.y).pos.y
Expand Down Expand Up @@ -106,8 +102,8 @@ object Day22 extends AoC:

val source: Pos = Pos.of(nodes.maxX, 0)
val target: Pos = Pos.of(0, 0)
val empty: Pos = nodes.find(_.node.isEmpty).map(_.pos).get
val massive: Vector[Pos] = nodes.filter(_.node.isMassive).map(_.pos).toVector
val empty: Pos = nodes.find(_.element.isEmpty).map(_.pos).get
val massive: Vector[Pos] = nodes.filter(_.element.isMassive).map(_.pos).toVector

/**
* This brings the problem back to the following two sub-problems this time
Expand Down
5 changes: 3 additions & 2 deletions 2017/src/main/scala/aoc2017/Day07.scala
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package aoc2017

import nmcb.*
import nmcb.predef.*

/** @see Credits - https://github.com/sim642 */
object Day07 extends AoC:
Expand Down Expand Up @@ -59,10 +60,10 @@ object Day07 extends AoC:
if totalWeightChildren.size <= 1 then
Right(node.weight + childTotalWeights.values.sum)
else
val badChild = totalWeightChildren.find(_._2.size == 1).get._2.head
val badChild = totalWeightChildren.find(_.right.size == 1).get.right.head
val badChildWeight = nodes(badChild).weight
val badChildTotalWeight = childTotalWeights(badChild)
val goodTotalWeight = totalWeightChildren.find(_._2.size > 1).get._1
val goodTotalWeight = totalWeightChildren.find(_.right.size > 1).get.left
Left(goodTotalWeight - (badChildTotalWeight - badChildWeight)))

val root = nodes.root
Expand Down
6 changes: 4 additions & 2 deletions 2018/src/main/scala/aoc2018/Day07.scala
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package aoc2018

import nmcb.*
import nmcb.predef.*

import scala.annotation.*
import scala.collection.immutable.SortedMap

Expand Down Expand Up @@ -30,7 +32,7 @@ object Day07 extends AoC:
loop(next, time + 1, working, done :++ processed)

loop(edges.foldLeft(SortedMap[A,Set[A]]())((ds,e) =>
ds + (e._1 -> ds.getOrElse(e._1, Set())) + (e._2 -> (ds.getOrElse(e._2, Set()) + e._1))
ds + (e.left -> ds.getOrElse(e.left, Set())) + (e.right -> (ds.getOrElse(e.right, Set()) + e.left))
))

val steps: Vector[(Char,Char)] =
Expand All @@ -40,5 +42,5 @@ object Day07 extends AoC:
case _ => sys.error("boom!")
lines.map(parser)

lazy val answer1: String = solve(steps, timer = _ => 1, parallelization = 1)._2.mkString("")
lazy val answer1: String = solve(steps, timer = _ => 1, parallelization = 1).right.mkString("")
lazy val (answer2, _) = solve(steps, timer = _.toInt - 4, parallelization = 5)
2 changes: 1 addition & 1 deletion 2018/src/main/scala/aoc2018/Day12.scala
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ object Day12 extends AoC:

/** note that we only collect pot indices that contain a plant */
val plants = lines(0) match
case s"initial state: $pots" => pots.zipWithIndex.filter(_._1 == '#').map(_._2).toSet
case s"initial state: $pots" => pots.zipWithIndex.filter(_.element == '#').map(_.index).toSet

/** note that we only collect rules that yield a plant */
val rules = lines.drop(2).toSet.collect:
Expand Down
4 changes: 3 additions & 1 deletion 2018/src/main/scala/aoc2018/Day22.scala
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package aoc2018

import nmcb.*
import nmcb.predef.*

import scala.collection.*

object Day22 extends AoC:
Expand Down Expand Up @@ -96,7 +98,7 @@ object Day22 extends AoC:
/** a-star with manhattan distance as heuristic */
def travelTime(from: State, to: State): Int =
val times = collection.mutable.Map(from -> 0)
val queue = collection.mutable.PriorityQueue(from -> 0)(using Ordering.by[(State,Int),Int](_._2).reverse)
val queue = collection.mutable.PriorityQueue(from -> 0)(using Ordering.by[(State,Int),Int](_.right).reverse)

def heuristic(state: State) =
state.region manhattan to.region
Expand Down
2 changes: 1 addition & 1 deletion 2019/src/main/scala/aoc2019/Day18.scala
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ object Day18 extends AoC:

cost.keys.map(pos => Move(start, pos, cost(pos), keys(pos))).toVector

type Tile = (Pos,Char)
type Tile = (Pos, Char)

extension (tile: Tile)
def pos: Pos = tile._1
Expand Down
4 changes: 2 additions & 2 deletions 2019/src/main/scala/aoc2019/cpu/CPU.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ package aoc2019.cpu

type Value = Long
type Pointer = Int
type PointerValue = (Pointer,Value)
type PointerValue = (Pointer, Value)

extension (pv: PointerValue)
def pointer: Pointer = pv._1
def value: Value = pv._2

case class Mem(underlying: Map[Pointer,Value]):
case class Mem(underlying: Map[Pointer, Value]):
def apply(p: Pointer): Value = underlying(p)
def updated(p: Pointer, v: Value): Mem = Mem(underlying.updated(p, v))
def +(pv: PointerValue): Mem = updated(pv.pointer, pv.value)
Expand Down
2 changes: 1 addition & 1 deletion 2020/src/main/scala/aoc2020/Day24.scala
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ object Day24 extends AoC:
case 'w' => go(point.sw, remaining.drop(2))
go(Tile(0, 0), directions)
.groupMapReduce(identity)(_ => 1)(_ + _)
.filter(_._2 % 2 == 1)
.filter((_, count) => count % 2 == 1)
.keySet


Expand Down
15 changes: 6 additions & 9 deletions 2021/src/main/scala/aoc2021/Day13.scala
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package aoc2021

import nmcb.*
import nmcb.pos.*

object Day13 extends AoC:

Expand All @@ -9,13 +10,9 @@ object Day13 extends AoC:

import Axis.*

type Dots = Set[(Int, Int)]
type Dots = Set[Pos]
type Folds = Vector[(Axis, Int)]

extension (dot: (Int, Int))
def x: Int = dot._1
def y: Int = dot._2

val dots: Dots =
lines
.collect:
Expand All @@ -31,11 +28,11 @@ object Day13 extends AoC:

def origami(dots: Dots, folds: Folds): Dots =

def vertical(line: Int)(x: Int, y: Int): (Int, Int) =
(if x < line then x else 2 * line - x, y)
def vertical(line: Int)(p: Pos): Pos =
(x = if p.x < line then p.x else 2 * line - p.x, y = p.y)

def horizontal(line: Int)(x: Int, y: Int): (Int, Int) =
(x, if y < line then y else 2 * line - y)
def horizontal(line: Int)(p: Pos): Pos =
(x = p.x, y = if p.y < line then p.y else 2 * line - p.y)

folds.foldLeft(dots):
case (dots, (Ver, line)) => dots.map(vertical(line))
Expand Down
3 changes: 2 additions & 1 deletion 2022/src/main/scala/aoc2022/Day25.scala
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package aoc2022

import nmcb.*
import nmcb.predef.*

import scala.annotation.tailrec
import scala.io.*
Expand Down Expand Up @@ -41,7 +42,7 @@ object Day25 extends AoC:
def toLong: Long =
digits.reverse.foldLeft((0L,1L)) { case ((a, p), d) =>
(a + (p * d.toLong), 5L * p)
}._1
}.left

override def toString: String =
val l = toLong
Expand Down
3 changes: 2 additions & 1 deletion 2023/src/main/scala/aoc2023/Day07.scala
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package aoc2023

import nmcb.*
import nmcb.predef.*

import scala.annotation.tailrec
import scala.math.Ordered.orderingToOrdered
Expand Down Expand Up @@ -101,7 +102,7 @@ object Day07 extends AoC:
case (d, 1) :: rest => loop(rest, High :: found)
case _ => sys.error(s"illegal state: $todo")

val countedCards = hand.groupMapReduce(identity)(_ => 1)(_ + _).toList.sortBy(_._2).reverse
val countedCards = hand.groupMapReduce(identity)(_ => 1)(_ + _).toList.sortBy(_.right).reverse
loop(countedCards).reverse

def orderBy(sortedSet: Cards): Ordering[Card] =
Expand Down
5 changes: 3 additions & 2 deletions 2023/src/main/scala/aoc2023/Day08.scala
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package aoc2023

import nmcb.*
import nmcb.predef.*

import scala.annotation.tailrec

Expand All @@ -15,8 +16,8 @@ object Day08 extends AoC:
case class Network(directions: Directions, nodes: Nodes):
def step(from: String, direction: Char): String =
direction match
case 'L' => nodes.getOrElse(from, sys.error(s"no step left from: $from"))._1
case 'R' => nodes.getOrElse(from, sys.error(s"no step right from: $from"))._2
case 'L' => nodes.getOrElse(from, sys.error(s"no step left from: $from")).left
case 'R' => nodes.getOrElse(from, sys.error(s"no step right from: $from")).right
case _ => sys.error(s"invalid direction: $direction")

@tailrec
Expand Down
3 changes: 2 additions & 1 deletion 2023/src/main/scala/aoc2023/Day17.scala
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package aoc2023

import nmcb.*
import nmcb.predef.*
import nmcb.pos.*

import scala.collection.mutable
Expand Down Expand Up @@ -68,7 +69,7 @@ object Day17 extends AoC:
* @return The target node and associated traversal weight if reachable.
*/
def traverse[A](start: A, target: A => Boolean, reachable: A => Set[(A, Int)]): Option[(A, Int)] =
val todo = mutable.PriorityQueue.empty(using Ordering.Int.on[(Int, A)](_._1).reverse)
val todo = mutable.PriorityQueue.empty(using Ordering.Int.on[(Int, A)](_.left).reverse)
val weights = mutable.Map.empty[A, Int]

def enqueue(node: A, weight: Int): Unit =
Expand Down
3 changes: 2 additions & 1 deletion 2023/src/main/scala/aoc2023/Day22.scala
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package aoc2023

import nmcb.*
import nmcb.predef.*

import scala.annotation.tailrec

Expand Down Expand Up @@ -46,7 +47,7 @@ object Day22 extends AoC:
.toSet
.flatMap: (box, foundation) =>
foundation.map(_ -> box)
.groupMap(_._1)(_._2)
.groupMap(_.left)(_.right)

def disintegrate(box: Box): Int =
@tailrec
Expand Down
5 changes: 3 additions & 2 deletions 2023/src/main/scala/aoc2023/Day24.scala
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package aoc2023

import nmcb.*
import nmcb.predef.*

import scala.math

Expand Down Expand Up @@ -215,7 +216,7 @@ object Day24 extends AoC:
BigDecimal(d).setScale(0, BigDecimal.RoundingMode.HALF_UP).toBigInt

extension (xy: (Double, Double)) def toBigInt: (BigInt, BigInt) =
(xy._1.toBigInt, xy._2.toBigInt)
(xy.left.toBigInt, xy.right.toBigInt)

def collide(stones: Seq[Stone]): Option[(BigInt, BigInt)] =
futureCollide2D(stones(0), stones(1))
Expand Down Expand Up @@ -269,7 +270,7 @@ object Day24 extends AoC:
(location, velocity.copy(z = z))
.map: (location, v) =>
val z = calcT(translated(0), location) * (translated(0).velocity.z - v.z) + translated(0).location.z
Location(x = location._1, y = location._2, z = z)
Location(x = location.left, y = location.right, z = z)
.head

found.x + found.y + found.z
Expand Down
Loading