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 2015/src/main/scala/aoc2015/Day18.scala
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ object Day18 extends AoC:
if overlay(y, x) then on else conf(y)(x)

def neighbours(x: Int, y: Int): Vector[Light] =
Vector((-1,-1),(0,-1),(1,-1),(-1, 0),(1, 0),(-1,1),(0, 1),(1, 1))
Vector((-1, -1), (0, -1), (1, -1), (-1, 0),(1, 0), (-1, 1), (0, 1), (1, 1))
.map((dx, dy) => (x + dx, y + dy))
.filter((px, py) => px >= minX && px <= maxX && py >= minY && py <= maxY)
.map(light)
Expand Down
4 changes: 2 additions & 2 deletions 2016/src/main/scala/aoc2016/Day13.scala
Original file line number Diff line number Diff line change
Expand Up @@ -52,5 +52,5 @@ object Day13 extends AoC:

cache.toMap

override lazy val answer1: Int = solve1(start = Pos.of(1, 1), target = Pos.of(31, 39))
override lazy val answer2: Int = solve2(start = Pos.of(1,1)).size
override lazy val answer1: Int = solve1(start = (1, 1), target = (31, 39))
override lazy val answer2: Int = solve2(start = (1,1)).size
4 changes: 2 additions & 2 deletions 2016/src/main/scala/aoc2016/Day17.scala
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@ object Day17 extends AoC:


/** hardcoded for 4x4 grid */
case class Path(passcode: String, path: String = "", target: Pos = Pos.of(0, 0)):
case class Path(passcode: String, path: String = "", target: Pos = (0, 0)):

def withinGrid: Boolean =
target.x >= 0 && target.x < 4 && target.y >= 0 && target.y < 4

def reachedVault: Boolean =
target == Pos.of(3, 3)
target == (3, 3)

def openDoors: String =
md5
Expand Down
12 changes: 6 additions & 6 deletions 2016/src/main/scala/aoc2016/Day22.scala
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ object Day22 extends AoC:
.filter(_.startsWith("/dev/grid/node"))
.map: line =>
val Array(x, y, _, used, avail, _) = line.split("\\D+").tail.map(_.toInt)
Pos.of(x, y) -> Node(used, avail)
(x, y) -> Node(used, avail)
.toMap

def viable(nodes: Map[Pos, Node]): Vector[(Node, Node)] =
Expand All @@ -42,10 +42,10 @@ object Day22 extends AoC:
y <- 0 to maxY
x <- 0 to maxX
do
val pos = Pos.of(x, y)
val pos = (x, y)
val node = nodes(pos)
if pos == Pos.of(0, 0) then sb.append('T')
else if pos == Pos.of(maxX, 0) then sb.append('S')
if pos == (0, 0) then sb.append('T')
else if pos == (maxX, 0) then sb.append('S')
else sb.append(node.toChar)
if x == maxX then sb.append('\n')
sb.append('\n').toString
Expand Down Expand Up @@ -100,8 +100,8 @@ object Day22 extends AoC:
*
*/

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

Expand Down
52 changes: 37 additions & 15 deletions 2016/src/main/scala/aoc2016/Day24.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,33 +3,55 @@ package aoc2016
import nmcb.*
import nmcb.pos.{*, given}

import scala.util.control.Breaks._

object Day24 extends AoC:

type Grid = Set[Pos]
type Nodes = Map[Int,Pos]
type Distances = Map[Int,Map[Int,Int]]
type Nodes = Map[Int, Pos]
type Distances = Map[Int, Map[Int, Int]]
type Routes = Vector[Vector[Int]]

val(grid: Grid, nodes: Nodes) =
val grid = for y <- lines.indices ; x <- lines.head.indices if lines(y)(x) != '#' yield Pos.of(x, y)
val nodes = for y <- lines.indices ; x <- lines.head.indices if lines(y)(x).isDigit yield lines(y)(x).asDigit -> Pos.of(x, y)
(grid.toSet, nodes.toMap)
val grid: Grid =
val grid =
for
y <- lines.indices
x <- lines.head.indices if lines(y)(x) != '#'
yield
(x = x, y = y)
grid.toSet

val nodes: Nodes =
val nodes =
for
y <- lines.indices
x <- lines.head.indices if lines(y)(x).isDigit
yield
lines(y)(x).asDigit -> Pos.of(x, y)
nodes.toMap

/** breath first search */
def distance(grid: Grid, start: Pos, end: Pos): Int =
import scala.collection.*
val steps = mutable.Map(start -> 0)
val todo = mutable.Queue(start)

while todo.nonEmpty do
val current = todo.dequeue
if current == end then return steps(current)
val update = steps(current) + 1
current.adjoint4.intersect(grid).foreach: next =>
if !steps.contains(next) || update < steps(next) then
steps(next) = update
todo.enqueue(next)
sys.error("boom!")
var result: Int = 0
breakable:
while todo.nonEmpty do
val current = todo.dequeue

if current == end then
result = steps(current)
break()

val update = steps(current) + 1

current.adjoint4.intersect(grid).foreach: next =>
if !steps.contains(next) || update < steps(next) then
steps(next) = update
todo.enqueue(next)
result

def distances(grid: Grid, nodes: Nodes): Distances =
nodes.transform: (_, start) =>
Expand Down
12 changes: 6 additions & 6 deletions 2017/src/main/scala/aoc2017/Day03.scala
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ object Day03 extends AoC:
val quadrant = base / size
val offset = base % size
quadrant match
case 0 => Pos.of(half, offset + 1 - half)
case 1 => Pos.of(half - 1 - offset, half)
case 2 => Pos.of(-half, half - 1 - offset)
case 3 => Pos.of(offset + 1 - half, -half)
case 0 => (half, offset + 1 - half)
case 1 => (half - 1 - offset, half)
case 2 => (-half, half - 1 - offset)
case 3 => (offset + 1 - half, -half)

def spiralSquares(to: Int):Int =

Expand All @@ -29,10 +29,10 @@ object Day03 extends AoC:
x <- -1 to 1
if !(x == 0 && y == 0)
yield
Pos.of(x, y)
(x, y)

@tailrec
def loop(n: Int, squares: Map[Pos,Int] = Map(Pos.of(0,0) -> 1)): Int =
def loop(n: Int, squares: Map[Pos,Int] = Map((0, 0) -> 1)): Int =
val point = position(n)
val result = spiralScan.map(_ + point).flatMap(squares.get).sum
if result > to then
Expand Down
2 changes: 1 addition & 1 deletion 2017/src/main/scala/aoc2017/Day14.scala
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ object Day14 extends AoC:
case (acc,(row, y)) =>
row.foldLeft(acc):
case (acc, (square, x)) =>
if square == '1' then acc + Pos.of(x,y) else acc
if square == '1' then acc + Pos.of(x, y) else acc

def regions(used: Set[Pos]): Set[Set[Pos]] =
val graph = used.map(square => square -> square.adjacent.intersect(used)).toMap
Expand Down
4 changes: 2 additions & 2 deletions 2017/src/main/scala/aoc2017/Day19.scala
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ object Day19 extends AoC:
extension (g: Grid)

def start: Pos =
Pos.of(g.head.indexOf('|'), 0)
(g.head.indexOf('|'), 0)

def charAt(p: Pos): Char =
grid.lift(p.y).flatMap(_.lift(p.x)).getOrElse(' ')
Expand All @@ -38,7 +38,7 @@ object Day19 extends AoC:

object Tracer:
def start(grid: Grid): Tracer =
Tracer(grid, grid.start, Pos.of(0,1))
Tracer(grid, grid.start, (0,1))

val tracer: Tracer = Tracer.start(grid)
val path: Seq[Tracer] = Iterator.iterate(tracer)(_.next).takeWhile(_.isPath).toList
Expand Down
6 changes: 3 additions & 3 deletions 2017/src/main/scala/aoc2017/Day22.scala
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ object Day22 extends AoC:
private val carrier: Carrier =

val current: Pos =
Pos.of(lines(0).length / 2, lines.length / 2)
(lines(0).length / 2, lines.length / 2)

val nodes: Map[Pos,Status] =
lines
Expand All @@ -75,8 +75,8 @@ object Day22 extends AoC:
row
.zipWithIndex
.collect:
case ('#',x) => Pos.of(x,y) -> Infected
case ('.',x) => Pos.of(x,y) -> Clean
case ('#',x) => (x, y) -> Infected
case ('.',x) => (x, y) -> Clean
.toMap
.withDefaultValue(Clean)

Expand Down
8 changes: 4 additions & 4 deletions 2017/src/main/scala/aoc2017/Day23.scala
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,10 @@ object Day23 extends AoC:
def step: CPU =
if running then
prog(pc) match
case SET(x,y) => copy(mem = mem + (x -> y.get), pc = pc + 1)
case SUB(x,y) => copy(mem = mem + (x -> (x.get - y.get)), pc = pc + 1)
case MUL(x,y) => copy(mem = mem + (x -> (x.get * y.get)), pc = pc + 1, countMUL = countMUL + 1)
case JNZ(x,y) => copy(pc = if x.get != 0 then pc + y.get.toInt else pc + 1)
case SET(x, y) => copy(mem = mem + (x -> y.get), pc = pc + 1)
case SUB(x, y) => copy(mem = mem + (x -> (x.get - y.get)), pc = pc + 1)
case MUL(x, y) => copy(mem = mem + (x -> (x.get * y.get)), pc = pc + 1, countMUL = countMUL + 1)
case JNZ(x, y) => copy(pc = if x.get != 0 then pc + y.get.toInt else pc + 1)
else
this

Expand Down
4 changes: 2 additions & 2 deletions 2018/src/main/scala/aoc2018/Day03.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import nmcb.pos.*
object Day03 extends AoC:

type Claims = Vector[Vector[Pos]]
type Overlaps = Map[Pos,Int]
type Overlaps = Map[Pos, Int]

val (claims: Claims, overlaps: Overlaps) =
val claims = lines.map:
Expand All @@ -15,7 +15,7 @@ object Day03 extends AoC:
val x1 = w.toInt + x0
val y0 = y.toInt
val y1 = h.toInt + y0
(for x <- x0 until x1 ; y <- y0 until y1 yield Pos.of(x,y)).toVector
(for x <- x0 until x1 ; y <- y0 until y1 yield (x = x, y = y)).toVector

val overlaps = claims.flatten.groupMapReduce(identity)(_ => 1)(_ + _)
(claims, overlaps)
Expand Down
4 changes: 2 additions & 2 deletions 2018/src/main/scala/aoc2018/Day06.scala
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ object Day06 extends AoC:
val maxY: Int = coordinates.maxBy(_.y).y

val positions: Vector[Pos] =
(for x <- minX to maxX ; y <- minY to maxY yield Pos.of(x,y)).toVector
(for x <- minX to maxX ; y <- minY to maxY yield (x, y)).toVector

type UnitDistance = (Pos, Long)

Expand Down Expand Up @@ -51,7 +51,7 @@ object Day06 extends AoC:


val coordinates: Vector[Pos] = lines.map:
case s"$x, $y" => Pos.of(x.toInt, y.toInt)
case s"$x, $y" => (x.toInt, y.toInt)

override lazy val answer1: Long = Grid(coordinates).largestAreaSize
override lazy val answer2: Int = Grid(coordinates).withinManhattanSumLimit(10000).size
4 changes: 2 additions & 2 deletions 2018/src/main/scala/aoc2018/Day10.scala
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ object Day10 extends AoC:
def fromString(s: String): Point =
s.trim match
case s"position=<$x, $y> velocity=<$vx, $vy>" =>
Point(Pos.of(x.trim.toInt, y.trim.toInt), Pos.of(vx.trim.toInt, vy.trim.toInt))
Point((x.trim.toInt, y.trim.toInt), (vx.trim.toInt, vy.trim.toInt))


// returns the range containing the first domain which yields at least `x` as its codomain, starting from `min`.
Expand Down Expand Up @@ -77,7 +77,7 @@ object Day10 extends AoC:
val positionsSet = positions.toSet

(min.y to max.y).foldLeft(StringBuffer())((sb,y) => (min.x to max.x).foldLeft(sb)((sb,x) =>
if positionsSet.contains(Pos.of(x,y)) then sb.append('#') else sb.append('.')
if positionsSet.contains((x, y)) then sb.append('#') else sb.append('.')
).append('\n')).toString

val (sky, fastest) =
Expand Down
23 changes: 12 additions & 11 deletions 2018/src/main/scala/aoc2018/Day11.scala
Original file line number Diff line number Diff line change
Expand Up @@ -22,38 +22,39 @@ object Day11 extends AoC:
def asIdString(size: Int): String =
s"${cell.x - size + 1},${cell.y - size + 1}"

/* Note:
/**
* Note:
*
* Given a value table i(x,y) a summed area table can be computed as:
* Given a value table i(x, y) a summed area table can be computed as:
*
* I(x,y) = i(x,y) + I(x-1,y) + I(x,y-1) - I(x-1,y-1)
* I(x, y) = i(x, y) + I(x-1, y) + I(x, y-1) - I(x-1, y-1)
*
* After which the sum of an area can be computed as:
*
* A(x0,y0,x1,y1) = I(x0,y0) + I(x1,y1) - I(x1,y0) - I(x0,y1)
* A(x0, y0, x1, y1) = I(x0, y0) + I(x1, y1) - I(x1, y0) - I(x0, y1)
*/

/** (x,y) cells for a 1 to 300 grid */
/** (x, y) cells for a 1 to 300 grid */
val cells: Seq[Cell] =
for y <- 1 to 300 ; x <- 1 to 300 yield Pos.of(x,y)
for y <- 1 to 300 ; x <- 1 to 300 yield (x, y)

/** power level value table, i.e. the i(x,y) value table for named cells */
/** power level value table, i.e. the i(x, y) value table for named cells */
val grid: Map[Cell,Int] =
cells.map(c => c -> c.powerLevel).toMap

/** summed power level table, i.e. the summed area table I(x,y) for named grid */
/** summed power level table, i.e. the summed area table I(x, y) for named grid */
val table: Map[Cell,Int] =
cells.foldLeft(immutable.Map.empty[Cell,Int].withDefaultValue(0)): (result, cell) =>
result + (cell -> (grid(cell) + result(cell + Pos.of(-1,0)) + result(cell + Pos.of(0,-1)) - result(cell + Pos.of(-1,-1))))
result + (cell -> (grid(cell) + result(cell + (-1, 0)) + result(cell + (0, -1)) - result(cell + (-1, -1))))

/** the area sizes, cells and total power levels for named summed power level table and given area size */
def area(size: Int): Iterator[(Int,Cell,Int)] =
for
y <- (size to 300).iterator
x <- (size to 300).iterator
yield
val cell = Pos.of(x,y)
val total = table(cell) + table(cell + Pos.of(-size,-size)) - table(cell + Pos.of(-size,0)) - table(cell + Pos.of(0,-size))
val cell = (x, y)
val total = table(cell) + table(cell + (-size,-size)) - table(cell + (-size,0)) - table(cell + (0,-size))
(size, cell, total)

extension (result: (Int,Cell,Int))
Expand Down
10 changes: 5 additions & 5 deletions 2018/src/main/scala/aoc2018/Day13.scala
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,11 @@ object Day13 extends AoC:
y <- (0 until matrix.sizeY).toVector
x <- (0 until matrix.sizeX).toVector
yield
matrix.charAt(Pos.of(x,y)) match
case Some(c) if c == '^' => Some(Cart(Pos.of(x,y), N))
case Some(c) if c == '>' => Some(Cart(Pos.of(x,y), E))
case Some(c) if c == 'v' => Some(Cart(Pos.of(x,y), S))
case Some(c) if c == '<' => Some(Cart(Pos.of(x,y), W))
matrix.charAt((x, y)) match
case Some(c) if c == '^' => Some(Cart((x, y), N))
case Some(c) if c == '>' => Some(Cart((x, y), E))
case Some(c) if c == 'v' => Some(Cart((x, y), S))
case Some(c) if c == '<' => Some(Cart((x, y), W))
case _ => None

val grid =
Expand Down
2 changes: 1 addition & 1 deletion 2018/src/main/scala/aoc2018/Day15.scala
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ object Day15 extends AoC:
for
(row,y) <- parseGrid(input).view.zipWithIndex.toList
(tile,x) <- row.view.zipWithIndex
fighter <- parseFighter(tile, Pos.of(x,y))
fighter <- parseFighter(tile, (x, y))
yield fighter

val grid: Grid =
Expand Down
8 changes: 4 additions & 4 deletions 2018/src/main/scala/aoc2018/Day17.scala
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ object Day17 extends AoC:
case Stopped =>
val minX = iterate(p)(_.e).dropWhile(next => go(next.s) == Stopped && !blocked(next.e)).next
val maxX = iterate(p)(_.w).dropWhile(next => go(next.s) == Stopped && !blocked(next.w)).next
val surface = for x <- minX.x to maxX.x yield Pos.of(x, p.y)
val surface = for x <- minX.x to maxX.x yield (x, p.y)
if blocked(minX.e) && blocked(maxX.w) then
stopped ++= surface
Stopped
Expand All @@ -54,15 +54,15 @@ object Day17 extends AoC:
val clay =
lines
.flatMap:
case s"x=$x, y=$start..$end" => (start.toInt to end.toInt).map(y => Pos.of(x.toInt, y))
case s"y=$y, x=$start..$end" => (start.toInt to end.toInt).map(x => Pos.of(x, y.toInt))
case s"x=$x, y=$start..$end" => (start.toInt to end.toInt).map(y => (x = x.toInt, y = y))
case s"y=$y, x=$start..$end" => (start.toInt to end.toInt).map(x => (x = x, y = y.toInt))
.toSet

Area(
clay = clay,
minY = clay.map(_.y).min,
maxY = clay.map(_.y).max,
spring = Pos.of(500,0)
spring = (500,0)
)

def solve1(area: Area): Int =
Expand Down
Loading