Day 4 part 2

This commit is contained in:
Wesley Moore 2022-12-04 15:54:30 +10:00
parent a0e0065c40
commit 49f1283cf6
No known key found for this signature in database

View file

@ -16,7 +16,11 @@ pub fn pt_1(input: String) -> Int {
} }
pub fn pt_2(input: String) -> Int { pub fn pt_2(input: String) -> Int {
todo string.split(input, on: "\n")
|> iterator.from_list
|> iterator.map(parse_line)
|> iterator.filter(overlaps)
|> iterator.fold(0, fn(sum, _) { sum + 1 })
} }
fn parse_line(line: String) -> #(Range, Range) { fn parse_line(line: String) -> #(Range, Range) {
@ -43,3 +47,12 @@ fn fully_contains(pair: #(Range, Range)) -> Bool {
let b = pair.1 let b = pair.1
b.start >= a.start && b.end <= a.end || a.start >= b.start && a.end <= b.end b.start >= a.start && b.end <= a.end || a.start >= b.start && a.end <= b.end
} }
fn overlaps(pair: #(Range, Range)) -> Bool {
let a = pair.0
let b = pair.1
case a.start < b.start {
True -> b.start <= a.end
False -> a.start <= b.end
}
}