Reformat stuff
This commit is contained in:
parent
4f2f75e9b8
commit
f0c4d30ee9
|
@ -1,2 +1,3 @@
|
||||||
version = "3.7.14"
|
version = "3.7.14"
|
||||||
runner.dialect = scala3
|
runner.dialect = scala3
|
||||||
|
maxColumn = 120
|
||||||
|
|
|
@ -18,8 +18,8 @@ object Parser extends CommonParser:
|
||||||
"blue" ^^^ ((h: Hand) => h.copy(blue = num))
|
"blue" ^^^ ((h: Hand) => h.copy(blue = num))
|
||||||
val hand = rep1sep(update, ",").map(_.foldRight(Hand(0, 0, 0))(_(_)))
|
val hand = rep1sep(update, ",").map(_.foldRight(Hand(0, 0, 0))(_(_)))
|
||||||
|
|
||||||
val game = "Game" ~ num ~ ":" ~ repsep(hand, ";") ^^ {
|
val game = "Game" ~ num ~ ":" ~ repsep(hand, ";") ^^ { case (_ ~ num ~ _ ~ hands) =>
|
||||||
case (_ ~ num ~ _ ~ hands) => Game(num, hands)
|
Game(num, hands)
|
||||||
}
|
}
|
||||||
end Parser
|
end Parser
|
||||||
|
|
||||||
|
@ -31,9 +31,7 @@ def part1 =
|
||||||
val games = getGames.toList
|
val games = getGames.toList
|
||||||
val res =
|
val res =
|
||||||
games
|
games
|
||||||
.filter(g =>
|
.filter(g => g.hands.forall(h => h.red <= 12 && h.green <= 13 && h.blue <= 14))
|
||||||
g.hands.forall(h => h.red <= 12 && h.green <= 13 && h.blue <= 14)
|
|
||||||
)
|
|
||||||
.map(_.id)
|
.map(_.id)
|
||||||
.sum
|
.sum
|
||||||
println(res)
|
println(res)
|
||||||
|
|
12
Day3.scala
12
Day3.scala
|
@ -25,17 +25,13 @@ def adjacent(x: Int, y: Int) =
|
||||||
|
|
||||||
def adjacentCells(x: Int, y: Int) =
|
def adjacentCells(x: Int, y: Int) =
|
||||||
adjacent(x, y)
|
adjacent(x, y)
|
||||||
.filter((x, y) =>
|
.filter((x, y) => x >= 0 && x < board.length && y >= 0 && y < board(x).length)
|
||||||
x >= 0 && x < board.length && y >= 0 && y < board(x).length
|
|
||||||
)
|
|
||||||
.map((x, y) => board(x)(y))
|
.map((x, y) => board(x)(y))
|
||||||
|
|
||||||
def part1 =
|
def part1 =
|
||||||
val numbersWithSyms = numbers
|
val numbersWithSyms = numbers
|
||||||
.filter((x, y, num) =>
|
.filter((x, y, num) =>
|
||||||
(y until y + num.length()).exists(y =>
|
(y until y + num.length()).exists(y => adjacentCells(x, y).exists(c => !c.isDigit && c != '.'))
|
||||||
adjacentCells(x, y).exists(c => !c.isDigit && c != '.')
|
|
||||||
)
|
|
||||||
)
|
)
|
||||||
.map(v => BigInt(v._3))
|
.map(v => BigInt(v._3))
|
||||||
.sum
|
.sum
|
||||||
|
@ -44,9 +40,7 @@ def part1 =
|
||||||
// Part 2
|
// Part 2
|
||||||
|
|
||||||
def adjacentNums(x: Int, y: Int) =
|
def adjacentNums(x: Int, y: Int) =
|
||||||
numbers.filter((nx, ny, n) =>
|
numbers.filter((nx, ny, n) => (x - nx).abs <= 1 && ny <= y + 1 && ny + n.length() >= y)
|
||||||
(x - nx).abs <= 1 && ny <= y + 1 && ny + n.length() >= y
|
|
||||||
)
|
|
||||||
|
|
||||||
def part2 =
|
def part2 =
|
||||||
val gears = board.toIterator.zipWithIndex
|
val gears = board.toIterator.zipWithIndex
|
||||||
|
|
|
@ -12,8 +12,8 @@ case class Card(id: Int, winning: List[Int], got: List[Int]):
|
||||||
|
|
||||||
object Parser extends CommonParser:
|
object Parser extends CommonParser:
|
||||||
val nums = rep1(num)
|
val nums = rep1(num)
|
||||||
val card = "Card " ~ num ~ ":" ~ nums ~ "|" ~ nums ^^ {
|
val card = "Card " ~ num ~ ":" ~ nums ~ "|" ~ nums ^^ { case (_ ~ id ~ _ ~ winning ~ _ ~ got) =>
|
||||||
case (_ ~ id ~ _ ~ winning ~ _ ~ got) => Card(id, winning, got)
|
Card(id, winning, got)
|
||||||
}
|
}
|
||||||
end Parser
|
end Parser
|
||||||
|
|
||||||
|
|
10
Day5.scala
10
Day5.scala
|
@ -7,8 +7,7 @@ import scala.collection.immutable.NumericRange.Exclusive
|
||||||
|
|
||||||
case class Conversion(source: Long, dest: Long, range: Long):
|
case class Conversion(source: Long, dest: Long, range: Long):
|
||||||
val gap = dest - source
|
val gap = dest - source
|
||||||
def apply(input: Long) = if source <= input && input < source + range then
|
def apply(input: Long) = if source <= input && input < source + range then Some(dest + (input - source))
|
||||||
Some(dest + (input - source))
|
|
||||||
else None
|
else None
|
||||||
|
|
||||||
def apply(r: LRange): (Option[LRange], Option[LRange]) =
|
def apply(r: LRange): (Option[LRange], Option[LRange]) =
|
||||||
|
@ -20,8 +19,7 @@ case class Conversion(source: Long, dest: Long, range: Long):
|
||||||
)
|
)
|
||||||
else None
|
else None
|
||||||
val rest =
|
val rest =
|
||||||
if r.`end` >= source + range then
|
if r.`end` >= source + range then Some((source + range max r.start) until r.`end`)
|
||||||
Some((source + range max r.start) until r.`end`)
|
|
||||||
else None
|
else None
|
||||||
(overlap, rest)
|
(overlap, rest)
|
||||||
|
|
||||||
|
@ -68,8 +66,8 @@ object Parser extends CommonParser:
|
||||||
|
|
||||||
val seeds = "seeds:" ~> nums
|
val seeds = "seeds:" ~> nums
|
||||||
|
|
||||||
val mapName = """\w+""".r ~ "-to-" ~ """\w+""".r ~ "map" ^^ {
|
val mapName = """\w+""".r ~ "-to-" ~ """\w+""".r ~ "map" ^^ { case (src ~ _ ~ dest ~ _) =>
|
||||||
case (src ~ _ ~ dest ~ _) => (src, dest)
|
(src, dest)
|
||||||
}
|
}
|
||||||
val mapEntry = long ~ long ~ long ^^ { case (dest ~ src ~ range) =>
|
val mapEntry = long ~ long ~ long ^^ { case (dest ~ src ~ range) =>
|
||||||
Conversion(src, dest, range)
|
Conversion(src, dest, range)
|
||||||
|
|
|
@ -16,8 +16,8 @@ object Parser extends CommonParser:
|
||||||
val instructions = rep(instruction)
|
val instructions = rep(instruction)
|
||||||
|
|
||||||
val name = """[\dA-Z]{3}""".r
|
val name = """[\dA-Z]{3}""".r
|
||||||
val node = name ~ "= (" ~ name ~ "," ~ name ~ ")" ^^ {
|
val node = name ~ "= (" ~ name ~ "," ~ name ~ ")" ^^ { case (name ~ _ ~ a ~ _ ~ b ~ _) =>
|
||||||
case (name ~ _ ~ a ~ _ ~ b ~ _) => Node(name, Array(a, b))
|
Node(name, Array(a, b))
|
||||||
}
|
}
|
||||||
|
|
||||||
// part 1
|
// part 1
|
||||||
|
@ -83,9 +83,7 @@ def part2 =
|
||||||
traverse(start, toManuallySimulate) match
|
traverse(start, toManuallySimulate) match
|
||||||
case Right(v) => println(v)
|
case Right(v) => println(v)
|
||||||
case Left(states) =>
|
case Left(states) =>
|
||||||
val loops = loopInfos.map((before, loop) =>
|
val loops = loopInfos.map((before, loop) => loop.shiftBy(toManuallySimulate - before))
|
||||||
loop.shiftBy(toManuallySimulate - before)
|
|
||||||
)
|
|
||||||
val size = loops.foldLeft(1L)((v, loop) => lcm(v, loop.loopSize))
|
val size = loops.foldLeft(1L)((v, loop) => lcm(v, loop.loopSize))
|
||||||
if loops.forall(l => l.goodPositions == Set(l.loopSize - 3))
|
if loops.forall(l => l.goodPositions == Set(l.loopSize - 3))
|
||||||
then // dirty hack from input
|
then // dirty hack from input
|
||||||
|
|
|
@ -28,9 +28,7 @@ def part2 =
|
||||||
println(
|
println(
|
||||||
inputs
|
inputs
|
||||||
.map(
|
.map(
|
||||||
calculate(depth =>
|
calculate(depth => (sum, ns) => sum + ns.head * (if depth % 2 == 0 then 1 else -1))
|
||||||
(sum, ns) => sum + ns.head * (if depth % 2 == 0 then 1 else -1)
|
|
||||||
)
|
|
||||||
)
|
)
|
||||||
.sum
|
.sum
|
||||||
)
|
)
|
||||||
|
|
Loading…
Reference in a new issue