Day 4 part 1

This commit is contained in:
Wesley Moore 2022-12-04 15:51:28 +10:00
parent 2e17bfb034
commit a0e0065c40
No known key found for this signature in database
2 changed files with 1045 additions and 0 deletions

1000
2022/input/day_4.txt Normal file

File diff suppressed because it is too large Load diff

45
2022/src/days/day_4.gleam Normal file
View file

@ -0,0 +1,45 @@
import gleam/int
import gleam/list.{map}
import gleam/iterator
import gleam/string
type Range {
Range(start: Int, end: Int)
}
pub fn pt_1(input: String) -> Int {
string.split(input, on: "\n")
|> iterator.from_list
|> iterator.map(parse_line)
|> iterator.filter(fully_contains)
|> iterator.fold(0, fn(sum, _) { sum + 1 })
}
pub fn pt_2(input: String) -> Int {
todo
}
fn parse_line(line: String) -> #(Range, Range) {
assert [a, b] =
string.split(line, ",")
|> map(parse_range)
#(a, b)
}
fn parse_range(str: String) -> Range {
assert [start, end] =
string.split(str, "-")
|> map(parse_int)
Range(start, end)
}
fn parse_int(str: String) -> Int {
assert Ok(i) = int.parse(str)
i
}
fn fully_contains(pair: #(Range, Range)) -> Bool {
let a = pair.0
let b = pair.1
b.start >= a.start && b.end <= a.end || a.start >= b.start && a.end <= b.end
}