This commit is contained in:
Paul-Henri Froidmont 2024-12-02 08:52:55 +01:00
parent 9ced28a855
commit 8411bfbc15
Signed by: phfroidmont
GPG key ID: BE948AFD7E7873BE
2 changed files with 1046 additions and 0 deletions

1000
input/day02 Normal file

File diff suppressed because it is too large Load diff

46
src/day02.scala Normal file
View file

@ -0,0 +1,46 @@
package aoc
package day02
val dayNumber = "02"
@main def part1: Unit =
println(part1(loadInput(dayNumber)))
@main def part2: Unit =
println(part2(loadInput(dayNumber)))
def part1(input: String): String =
def isSafe(rs: Array[Int]): Boolean =
val rsInc = if rs.head > rs.last then rs.reverse else rs
rsInc
.sliding(2)
.map { case Array(a, b) => b - a }
.forall(diff => diff >= 1 && diff <= 3)
input
.split("\n")
.map(_.split(" ").map(_.toInt))
.count(isSafe)
.toString
def part2(input: String): String =
def isSafe(rs: Array[Int]): Boolean =
val rsInc = if rs.head > rs.last then rs.reverse else rs
rsInc
.sliding(2)
.map { case Array(a, b) => b - a }
.forall(diff => diff >= 1 && diff <= 3)
def isSafeWithTolerance(rs: Array[Int]): Boolean =
isSafe(rs) ||
(for i <- 0 to rs.length
yield isSafe(rs.take(i) ++ rs.drop(i + 1)))
.exists(identity)
input
.split("\n")
.map(_.split(" ").map(_.toInt))
.count(isSafeWithTolerance)
.toString