-
-
Save waynejo/4fd632b0573f3e4aa88ad61435f490b7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.FileInputStream | |
import scala.annotation.tailrec | |
import scala.io.StdIn | |
case class Range(min: Int, max: Int): | |
def fullyContains(other: Range): Boolean = | |
other.min >= min && other.max <= max | |
def overlaps(other: Range): Boolean = | |
(min <= other.min && other.min <= max) || (other.min <= min && min <= other.max) | |
def solve4_1(values: Vector[(Range, Range)]): Int = | |
values.count { case (range1, range2) => | |
range1.fullyContains(range2) || range2.fullyContains(range1) | |
} | |
def solve4_2(values: Vector[(Range, Range)]): Int = | |
values.count(_ overlaps _) | |
@main def solve4(): Unit = | |
val in = new FileInputStream("example4-2.in") | |
System.setIn(in) | |
val inputs = Iterator.continually(StdIn.readLine()) | |
.takeWhile(line => null != line) | |
.map(line => { | |
line.split(",").map { range => | |
val Array(min: String, max: String) = range.split("-") | |
Range(min.toInt, max.toInt) | |
} match | |
case Array(range1, range2) => (range1, range2) | |
}) | |
.toVector | |
println(solve4_1(inputs)) | |
println(solve4_2(inputs)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment