2022 Day 4 Scala

This commit is contained in:
Luke Hubmayer-Werner 2022-12-05 21:00:04 +10:30
parent 246bcd4150
commit 5082e1a234
1 changed files with 17 additions and 0 deletions

17
2022/day4.scala Normal file
View File

@ -0,0 +1,17 @@
import scala.io.Source
val numberPattern = raw"((?:(?<!\d)-)?\d+)".r
extension (s: String) def splitHalf: (String, String) = (s.substring(0, s.length()/2), s.substring(s.length()/2))
@main def main() =
val rangePairs: Array[(Range, Range)] = Source.fromFile("input/4").getLines
.map(line => numberPattern.findAllIn(line).map(_.toInt).toArray)
.map(n => ((n(0) to n(1)), (n(2) to n(3))))
.toArray // Can't leave it lazy as Part 1 will consume it
val subsets = rangePairs.map((a,b) => if (a.containsSlice(b) | b.containsSlice(a)) 1 else 0).sum
println(s"Part 1: $subsets elves have no unique work in their pairing")
val overlaps = rangePairs.map((a,b) => if (a.intersect(b).isEmpty) 0 else 1).sum
println(s"Part 2: $overlaps elf pairs have overlapping work in their pairing")