mirror of
https://github.com/wezm/advent-of-code.git
synced 2024-12-18 18:29:55 +00:00
Day 4 part 1
This commit is contained in:
parent
2e17bfb034
commit
a0e0065c40
2 changed files with 1045 additions and 0 deletions
1000
2022/input/day_4.txt
Normal file
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
45
2022/src/days/day_4.gleam
Normal 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
|
||||||
|
}
|
Loading…
Reference in a new issue