Day 02
This commit is contained in:
parent
9ced28a855
commit
8411bfbc15
2 changed files with 1046 additions and 0 deletions
1000
input/day02
Normal file
1000
input/day02
Normal file
File diff suppressed because it is too large
Load diff
46
src/day02.scala
Normal file
46
src/day02.scala
Normal 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
|
||||||
Loading…
Add table
Add a link
Reference in a new issue