Cleanup
This commit is contained in:
@@ -10,21 +10,22 @@ object Day02 extends Solver(2):
|
|||||||
.map(line => line.split(" ").map(_.toInt).toList)
|
.map(line => line.split(" ").map(_.toInt).toList)
|
||||||
.toList
|
.toList
|
||||||
|
|
||||||
def valid(list: List[Int]): Boolean =
|
def safe(list: List[Int]): Boolean =
|
||||||
val isSorted = (list.sorted == list || list.sorted.reverse == list)
|
list.length < 2 || list
|
||||||
val notTooLarge = list.sliding(2).map(x => (x(1) - x(0)).abs).forall(_ <= 3)
|
.sliding(2)
|
||||||
val notTooSmall = list.sliding(2).map(x => (x(1) - x(0)).abs).forall(_ >= 1)
|
.collect { case List(a, b) => b - a }
|
||||||
isSorted && notTooLarge && notTooSmall
|
.forall(1 to 3 contains _)
|
||||||
|
|
||||||
def part1(lists: List[List[Int]]): Int = lists.count(valid)
|
def safeWithGap(list: List[Int]): Boolean =
|
||||||
|
list.indices.exists(i => safe(list.take(i) ++ list.drop(i + 1)))
|
||||||
|
|
||||||
|
def part1(lists: List[List[Int]]): Int =
|
||||||
|
lists.count(list => safe(list) || safe(list.reverse))
|
||||||
|
|
||||||
def part2(lists: List[List[Int]]): Int =
|
def part2(lists: List[List[Int]]): Int =
|
||||||
lists.count(list =>
|
lists.count(list => safeWithGap(list) || safeWithGap(list.reverse))
|
||||||
list.indices.exists(i => valid(list.take(i) ++ list.drop(i + 1)))
|
|
||||||
)
|
|
||||||
|
|
||||||
def run(input: String): (Timings, Solution) =
|
def run(input: String): (Timings, Solution) =
|
||||||
|
|
||||||
val (pre_time, pre_input) = timed { pre(input) }
|
val (pre_time, pre_input) = timed { pre(input) }
|
||||||
val (p1_time, p1_solution) = timed { part1(pre_input) }
|
val (p1_time, p1_solution) = timed { part1(pre_input) }
|
||||||
val (p2_time, p2_solution) = timed { part2(pre_input) }
|
val (p2_time, p2_solution) = timed { part2(pre_input) }
|
||||||
|
|||||||
Reference in New Issue
Block a user