-
Notifications
You must be signed in to change notification settings - Fork 0
/
Day04Test.scala
53 lines (41 loc) · 1.16 KB
/
Day04Test.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package org.lemon.advent.year2022
import org.lemon.advent._
class Day04Test extends UnitTest {
private def parseRange(in: String) =
val bounds = in.split('-')
bounds(0).toInt to bounds(1).toInt
private def contains(lhs: Range, rhs: Range) =
lhs.containsSlice(rhs) || rhs.containsSlice(lhs)
private def part1(input: String) = input.linesIterator
.map(_.split(',').map(parseRange))
.count(r => contains(r(0), r(1)))
private def part2(input: String) = input.linesIterator
.map(_.split(',').map(parseRange))
.count(r => r(0).intersect(r(1)).nonEmpty)
test("part 1 example") {
val input = """
|2-4,6-8
|2-3,4-5
|5-7,7-9
|2-8,3-7
|6-6,4-6
|2-6,4-8""".stripMargin.strip
part1(input) shouldBe 2
}
test("part 1") {
part1(read(file(2022)(4))) shouldBe 485
}
test("part 2 example") {
val input = """
|2-4,6-8
|2-3,4-5
|5-7,7-9
|2-8,3-7
|6-6,4-6
|2-6,4-8""".stripMargin.strip
part2(input) shouldBe 4
}
test("part 2") {
part2(read(file(2022)(4))) shouldBe 857
}
}