Files
exercism-workspace/tisbury-treasure-hunt/src/tisbury_treasure_hunt.gleam
2025-11-08 15:04:55 +01:00

63 lines
1.6 KiB
Gleam

import gleam/list
pub fn place_location_to_treasure_location(
place_location: #(String, Int),
) -> #(Int, String) {
let #(p, l) = place_location
#(l, p)
}
pub fn treasure_location_matches_place_location(
place_location: #(String, Int),
treasure_location: #(Int, String),
) -> Bool {
place_location_to_treasure_location(place_location) == treasure_location
}
pub fn count_place_treasures(
place: #(String, #(String, Int)),
treasures: List(#(String, #(Int, String))),
) -> Int {
let #(_, place_location) = place
list.fold(treasures, 0, fn(acc, t) {
let #(_, treasure_location) = t
case
treasure_location_matches_place_location(
place_location,
treasure_location,
)
{
False -> acc
True -> acc + 1
}
})
}
pub fn special_case_swap_possible(
found_treasure: #(String, #(Int, String)),
place: #(String, #(String, Int)),
desired_treasure: #(String, #(Int, String)),
) -> Bool {
let #(found_treasure_name, _) = found_treasure
let #(desired_treasure_name, _) = desired_treasure
let #(place_name, _) = place
case found_treasure_name {
"Brass Spyglass" -> desired_treasure_name == "Abandoned Lighthouse"
"Amethyst Octopus" ->
place_name == "Stormy Breakwater"
&& {
desired_treasure_name == "Crystal Crab"
|| desired_treasure_name == "Glass Starfish"
}
"Vintage Pirate Hat" ->
place_name == "Harbor Managers Office"
&& {
desired_treasure_name == "Model Ship in Large Bottle"
|| desired_treasure_name == "Antique Glass Fishnet Float"
}
_ -> False
}
}