Skip to content

Instantly share code, notes, and snippets.

@JRuumis
JRuumis / Day2CubeConundrum.scala
Last active December 2, 2023 12:55
Regex sucks
import scala.util.matching.Regex
object Day2CubeConundrum extends App {
// Cube RGB
case class CubeReveal(red: Int, green: Int, blue: Int) {
lazy val isValid: Boolean = red <= CubeReveal.redMax && green <= CubeReveal.greenMax && blue <= CubeReveal.blueMax
lazy val cubePower: Int = red * green * blue
def minRequiredCubes(other: CubeReveal): CubeReveal = CubeReveal(this.red max other.red, this.green max other.green, this.blue max other.blue)
@JRuumis
JRuumis / Day1Trebuchet.scala
Last active December 1, 2023 11:08
Day 1 - Trebuchet
object Day1Trebuchet2 extends App {
val calibrations: Vector[String] = scala.io.Source.fromFile("./Sources/2023/Day1Trebuchet.txt").getLines().toVector
case class Digit(digitString: String, digitInt: Int)
val allDigitsPartOne: Vector[Digit] = Vector(
Digit("1", 1),
Digit("2", 2),
Digit("3", 3),
Digit("4", 4),
@JRuumis
JRuumis / Day19Robots2.scala
Created December 24, 2022 17:32
Day19 JR1
import scala.util.matching.Regex
object Day19Robots2 extends App {
val blueprintsRaw = scala.io.Source.fromFile("./Sources/Day19Robots_TEST.txt").getLines().toVector
val blueprintPattern: Regex = """Blueprint ([0-9]+): Each ore robot costs ([0-9]+) ore. Each clay robot costs ([0-9]+) ore. Each obsidian robot costs ([0-9]+) ore and ([0-9]+) clay. Each geode robot costs ([0-9]+) ore and ([0-9]+) obsidian.""".r
abstract class Resources {
val ore: Int
val clay: Int
import scala.annotation.tailrec
object Day24Blizzard extends App {
val blizzardRaw: Vector[Vector[Char]] = scala.io.Source.fromFile("./Sources/Day24Blizzard.txt").getLines().toVector.map(_.toVector)
val blizzardWithoutLF = blizzardRaw.map(r => r.slice(1, r.length-1))
val gridBody: Vector[Vector[Char]] = blizzardWithoutLF.slice(1, blizzardWithoutLF.length-1)
val gridX: Int = gridBody(0).length
val gridY: Int = gridBody.length
@JRuumis
JRuumis / Day17Tetris.scala
Created December 18, 2022 12:22
Day17 cycle detect
def gridReach(inputGrid: Grid, currentFront: Set[Coord], accuRocksToKeep: Set[Coord]): Set[Coord] = {
if(currentFront.isEmpty) accuRocksToKeep
else {
val neigboursToCheck: Set[Coord] = currentFront.flatMap(c => c.neighbours)
val checkedNeighbours: Set[(Coord, Char)] = neigboursToCheck.map(c => (c,gridGet(inputGrid,c)) match {
case (_, None) => None
case (_, Some('f')) => None
case (_, Some('w')) => None
case (c, Some('.')) => Some((c,'f'))
@JRuumis
JRuumis / Day16Valves.scala
Created December 17, 2022 01:01
Day16 JR defeat
import scala.annotation.tailrec
import scala.util.matching.Regex
object Day16Valves extends App {
val valvesInputRaw: Vector[String] = scala.io.Source.fromFile("./Sources/Day16Valves.txt").getLines.toVector
val valvesPattern: Regex = "Valve ([A-Z][A-Z]) has flow rate=([0-9]+); tunnel(?:[s]?) lead(?:[s]?) to valve(?:[s]?) ([A-Z, ]+)".r
val valvesParsed: Vector[(String, Int, Vector[String])] = valvesInputRaw.map(vr => vr match {
case valvesPattern(v, r, ov) => (v, r.toInt, ov.split(", ").toVector)
@JRuumis
JRuumis / Day15Beacons.scala
Created December 15, 2022 16:36
Day15 JR
import scala.annotation.tailrec
import scala.math.abs
import scala.util.matching.Regex
object Day15Beacons extends App {
type DimCoord = Long
//val checkY: DimCoord = 10 // TEST
//val searchSpaceMax: Long = 20 // TEST
import scala.annotation.tailrec
object Day14DropSand extends App {
// -= Coordinates and Grid =-
type DimCoord = Int
case class Coord(x: DimCoord, y: DimCoord) {
def up: Coord = Coord(x, y - 1)
def down: Coord = Coord(x, y + 1)
import scala.util.parsing.combinator._
object Day13DistressSignal extends App {
// -= Packet =-
abstract class PacketData
case class PacketInteger(integer: Int) extends PacketData {
override def toString: String = integer.toString
}
@JRuumis
JRuumis / Day12HillClimbing.scala
Last active December 12, 2022 11:40
Day12 JR
import scala.annotation.tailrec
object Day12HillClimbing extends App {
val startGrid = scala.io.Source.fromFile("./Sources/Day12HillClimbing.txt").getLines().toVector.map(_.toVector)
val sizeY = startGrid.length
val sizeX = startGrid(0).length
val startPosition: (Int, Int) = (0 to sizeY-1).flatMap(y => (0 to sizeX-1).map(x => if(startGrid(y)(x)== 'S') Some(y,x) else None )).filter(_.isDefined).map(_.get)(0)