Day 4: Camp Cleanup
by @bishabosha
Puzzle description
https://adventofcode.com/2022/day/4
Final Code
def part1(input: String): Int =
foldPairs(input, subsumes)
def part2(input: String): Int =
foldPairs(input, overlaps)
def subsumes(x: Int, y: Int)(a: Int, b: Int): Boolean = x <= a && y >= b
def overlaps(x: Int, y: Int)(a: Int, b: Int): Boolean = x <= a && y >= a || x <= b && y >= b
def foldPairs(input: String, hasOverlap: (Int, Int) => (Int, Int) => Boolean): Int =
val matches =
for line <- input.linesIterator yield
val Array(x,y,a,b) = line.split("[,-]").map(_.toInt): @unchecked
hasOverlap(x,y)(a,b) || hasOverlap(a,b)(x,y)
matches.count(identity)
Run it in the browser
Part 1
Part 2
Solutions from the community
- Solution of Jan Boerman.
- Solution of SimY4.
- Solution by Cosmin Ciobanu
- Solution by Niels Prins
- Solution by Richard W
- Solution part1 and part2 by Erik van Oosten
- Solution by Daniel Naumau
- Solution by Artem Sierikov
- Solution by Paweł Cembaluk
- Solution using ZIO by Rafał Piotrowski
- Solution by Rui Alves
Share your solution to the Scala community by editing this page.