This commit is contained in:
Paul-Henri Froidmont 2022-12-04 08:40:37 +01:00
parent 01f9ca8a76
commit 7028e6edf0
Signed by: phfroidmont
GPG key ID: BE948AFD7E7873BE
2 changed files with 1029 additions and 0 deletions

1000
aoc/resources/day4Input.txt Normal file

File diff suppressed because it is too large Load diff

29
aoc/src/Day4.scala Normal file
View file

@ -0,0 +1,29 @@
import scala.io.Source
object Day4 extends App:
val input = Source
.fromURL(getClass.getResource("day4Input.txt"))
.mkString
.split('\n')
.toList
def parsePair(string: String): (List[Int], List[Int]) =
string.split(',').map(parseRange) match
case Array(elf1, elf2) => (elf1, elf2)
def parseRange(string: String): List[Int] =
string.split('-').map(_.toInt) match
case Array(from, to) => List.range(from, to + 1)
val part1 = input
.map(parsePair)
.count(pair => pair._1.containsSlice(pair._2) || pair._2.containsSlice(pair._1))
println(s"Part 1: $part1")
val part2 = input
.map(parsePair)
.count(pair => pair._1.intersect(pair._2).size > 0)
println(s"Part 2: $part2")