Skip to content

Instantly share code, notes, and snippets.

@Thomashrb
Created December 1, 2021 19:47
Show Gist options
  • Save Thomashrb/cfd1d46593a09f7c19a56a58fc6be87d to your computer and use it in GitHub Desktop.
Save Thomashrb/cfd1d46593a09f7c19a56a58fc6be87d to your computer and use it in GitHub Desktop.
use "collections"
primitive Day01
fun val test_input(): String => "199\n200\n208\n210\n200\n207\n240\n269\n260\n263\n"
fun solve_part1(input: String): USize =>
let depths = _parse_entries(input)
var last_depth: USize = 0
// hack because the first will always be an increase
var incs: USize = -1
for d in depths.values() do
if last_depth < d then incs = incs + 1 end
last_depth = d
end
incs
fun solve_part2(input: String): USize =>
let depths = _parse_entries(input)
let w_depths = _window_list(depths)
var last_sum: USize = 0
// hack because the first will always be an increase
var incs: USize = -1
for ds in w_depths.values() do
let w_sum = ds.fold[USize]({(d, a) => a+d}, 0)
if last_sum < w_sum then incs = incs + 1 end
last_sum = w_sum
end
incs
fun _window_list(us: List[USize]): List[List[USize]] =>
let w: USize = 3
var ret: List[List[USize]] = List[List[USize]]
var rem: List[USize] = us
for i in Range(0, us.size() - w) do
rem = us.drop(i)
let cell = List[List[USize]].unit(rem.take(w))
ret.append_list(cell)
end
ret
fun _parse_entries(input: String): List[USize] =>
List[String].from(input.split())
.map[USize]({(s: String) => try s.usize()? else 0 end})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment