This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import scala.collection.immutable | |
object Aoc7: | |
enum Type extends Enum[Type]: | |
case HighCard, OnePair, TwoPairs, ThreeOfAKind, FullHouse, FourOfAKind, FiveOfAKind | |
final case class Hand(cards: List[Card], bid: Int, withJokers: Boolean = false): | |
override def toString: String = s"Hand(${cards.mkString(",")}, $bid)" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
object Aoc6: | |
final case class Boat(buttonPressTime: Int): | |
def distance(timeAllowed: BigInt): BigInt = | |
val travelTime = timeAllowed - buttonPressTime | |
buttonPressTime * travelTime | |
final case class Race(timeAllowed: Int, record: BigInt) | |
def parse1(input: List[String]): List[Race] = |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
object Aoc9: | |
//0 3 6 9 12 15 | |
//1 3 6 10 15 21 | |
//10 13 16 21 30 45 | |
def parse(input: List[String]): List[List[Int]] = | |
input.map(_.split(" ").map(_.toInt).toList) | |
private def derivative(input: List[Int]): List[Int]= | |
input.zip(input.tail).map((a, b) => b - a) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import scala.annotation.tailrec | |
object Aoc8: | |
def parse(input: List[String]) = | |
val pattern = input.head.split("").toList | |
val rules = input.tail.tail.map(_.split(" = ").toList).map { case List(start, lr) => | |
val Array(l,r) = lr.split(", ") | |
start -> (l.stripMargin('('), r.dropRight(1)) | |
}.toMap | |
(pattern, rules) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import scala.annotation.tailrec | |
import scala.math.* | |
final case class Pos(i: Int, j: Int) | |
final case class PartNumber(num: Int, start: Pos, end: Pos) { | |
def isNear(p: Pos, maxI: Int, maxJ: Int): Boolean = | |
val searchArea = for | |
i <- max(0, start.i-1) to min(end.i + 1, maxI) | |
j <- max(0, start.j-1) to min(end.j + 1, maxJ) | |
if ! (i == start.i && (start.j to end.j).contains(j)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import scala.math.* | |
final case class Card(id: Int, winning: Set[Int], my: Set[Int]) { | |
def winningScore: Int = | |
val winningNumbersInMyCard = my.intersect(winning) | |
if (winningNumbersInMyCard.nonEmpty) 1 << winningNumbersInMyCard.size - 1 | |
else 0 | |
def matching: Int = my.intersect(winning).size | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import scala.math._ | |
final case class Game(id: Int, plays: List[Play]) { | |
lazy val minSet: Play = | |
plays.reduce{ case (Play(r1,g1,b1), Play(r2,g2,b2)) => | |
Play(max(r1, r2), max(g1, g2), max(b1, b2)) | |
} | |
def pow: Int = minSet.red * minSet.green * minSet.blue | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
object Aoc1: | |
def findNumbersFirstAndLast(line:String): Int = | |
val nums = line.filter(_.isDigit).map(_.asDigit) | |
val first = nums.head | |
val last = nums.reverse.head | |
s"$first$last".toInt | |
private val word2Number = Map( | |
"0" -> 0, "1" -> 1, "2" -> 2, "3" -> 3, "4" -> 4, "5" -> 5, "6" -> 6, "7" -> 7, "8" -> 8, "9" -> 9, |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
# assign the file, start string, and stop string to variables | |
file=$1 | |
start_string=$2 | |
stop_string=$3 | |
# flag to track if we are inside the desired section | |
inside=false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package lang | |
object lambdaCalc extends App { | |
sealed trait Token | |
object Token { | |
final case object Lparen extends Token | |
final case object Rparen extends Token | |
final case object Dot extends Token | |
final case object Backslash extends Token |
NewerOlder