Compare commits
No commits in common. "24c2bbc3d0f652b4e3fcbcf0a60691da75db2abd" and "e4d2bbeb49abdb6d4749df87ef0c5b292aa87b8a" have entirely different histories.
24c2bbc3d0
...
e4d2bbeb49
33 changed files with 0 additions and 147 deletions
|
@ -1,8 +0,0 @@
|
|||
89010123
|
||||
78121874
|
||||
87430965
|
||||
96549874
|
||||
45678903
|
||||
32019012
|
||||
01329801
|
||||
10456732
|
|
@ -1,70 +0,0 @@
|
|||
import gleam/bool
|
||||
import gleam/dict.{type Dict}
|
||||
import gleam/int
|
||||
import gleam/io
|
||||
import gleam/iterator
|
||||
import gleam/list.{filter, filter_map, fold, map, reverse}
|
||||
import gleam/option.{type Option, None, Some}
|
||||
import gleam/result
|
||||
import gleam/string
|
||||
import stdin.{stdin}
|
||||
|
||||
pub fn main() {
|
||||
stdin()
|
||||
|> read_input
|
||||
|> calculate_trails
|
||||
|> io.debug
|
||||
}
|
||||
|
||||
fn calculate_trails(topomap: Dict(#(Int, Int), Int)) -> Int {
|
||||
topomap
|
||||
|> find_trailheads
|
||||
|> map(fn(x) { count_trails_loop(topomap, x, 0) })
|
||||
|> map(list.length)
|
||||
|> fold(0, int.add)
|
||||
}
|
||||
|
||||
fn count_trails_loop(
|
||||
topomap: Dict(#(Int, Int), Int),
|
||||
pos: #(Int, Int),
|
||||
offset: Int,
|
||||
) -> List(#(Int, Int)) {
|
||||
let #(x, y) = pos
|
||||
case dict.get(topomap, pos) {
|
||||
Ok(9) if offset == 9 -> {
|
||||
[pos]
|
||||
}
|
||||
Ok(n) if n == offset ->
|
||||
[
|
||||
count_trails_loop(topomap, #(x, y - 1), offset + 1),
|
||||
count_trails_loop(topomap, #(x, y + 1), offset + 1),
|
||||
count_trails_loop(topomap, #(x - 1, y), offset + 1),
|
||||
count_trails_loop(topomap, #(x + 1, y), offset + 1),
|
||||
]
|
||||
|> filter(fn(x) { x |> list.is_empty |> bool.negate })
|
||||
|> list.flatten
|
||||
|> list.unique
|
||||
_ -> []
|
||||
}
|
||||
}
|
||||
|
||||
fn find_trailheads(topomap: Dict(#(Int, Int), Int)) -> List(#(Int, Int)) {
|
||||
topomap |> dict.filter(fn(_, v) { v == 0 }) |> dict.keys
|
||||
}
|
||||
|
||||
fn read_input(it: iterator.Iterator(String)) -> Dict(#(Int, Int), Int) {
|
||||
it
|
||||
|> iterator.fold([], fn(acc, line) {
|
||||
case string.trim(line) {
|
||||
"" -> acc
|
||||
str -> {
|
||||
[str |> string.to_graphemes |> filter_map(int.parse), ..acc]
|
||||
}
|
||||
}
|
||||
})
|
||||
|> reverse
|
||||
|> list.index_fold(dict.new(), fn(acc, line, y) {
|
||||
line
|
||||
|> list.index_fold(acc, fn(dic, num, x) { dict.insert(dic, #(x, y), num) })
|
||||
})
|
||||
}
|
|
@ -1,69 +0,0 @@
|
|||
import gleam/bool
|
||||
import gleam/dict.{type Dict}
|
||||
import gleam/int
|
||||
import gleam/io
|
||||
import gleam/iterator
|
||||
import gleam/list.{filter, filter_map, fold, map, reverse}
|
||||
import gleam/option.{type Option, None, Some}
|
||||
import gleam/result
|
||||
import gleam/string
|
||||
import stdin.{stdin}
|
||||
|
||||
pub fn main() {
|
||||
stdin()
|
||||
|> read_input
|
||||
|> calculate_trails
|
||||
|> io.debug
|
||||
}
|
||||
|
||||
fn calculate_trails(topomap: Dict(#(Int, Int), Int)) -> Int {
|
||||
topomap
|
||||
|> find_trailheads
|
||||
|> map(fn(x) { count_trails_loop(topomap, x, 0) })
|
||||
|> map(list.length)
|
||||
|> fold(0, int.add)
|
||||
}
|
||||
|
||||
fn count_trails_loop(
|
||||
topomap: Dict(#(Int, Int), Int),
|
||||
pos: #(Int, Int),
|
||||
offset: Int,
|
||||
) -> List(#(Int, Int)) {
|
||||
let #(x, y) = pos
|
||||
case dict.get(topomap, pos) {
|
||||
Ok(9) if offset == 9 -> {
|
||||
[pos]
|
||||
}
|
||||
Ok(n) if n == offset ->
|
||||
[
|
||||
count_trails_loop(topomap, #(x, y - 1), offset + 1),
|
||||
count_trails_loop(topomap, #(x, y + 1), offset + 1),
|
||||
count_trails_loop(topomap, #(x - 1, y), offset + 1),
|
||||
count_trails_loop(topomap, #(x + 1, y), offset + 1),
|
||||
]
|
||||
|> filter(fn(x) { x |> list.is_empty |> bool.negate })
|
||||
|> list.flatten
|
||||
_ -> []
|
||||
}
|
||||
}
|
||||
|
||||
fn find_trailheads(topomap: Dict(#(Int, Int), Int)) -> List(#(Int, Int)) {
|
||||
topomap |> dict.filter(fn(_, v) { v == 0 }) |> dict.keys
|
||||
}
|
||||
|
||||
fn read_input(it: iterator.Iterator(String)) -> Dict(#(Int, Int), Int) {
|
||||
it
|
||||
|> iterator.fold([], fn(acc, line) {
|
||||
case string.trim(line) {
|
||||
"" -> acc
|
||||
str -> {
|
||||
[str |> string.to_graphemes |> filter_map(int.parse), ..acc]
|
||||
}
|
||||
}
|
||||
})
|
||||
|> reverse
|
||||
|> list.index_fold(dict.new(), fn(acc, line, y) {
|
||||
line
|
||||
|> list.index_fold(acc, fn(dic, num, x) { dict.insert(dic, #(x, y), num) })
|
||||
})
|
||||
}
|
Loading…
Reference in a new issue