From fad9b41d97eaf85da8f3eec21a31f7e12c0453b1 Mon Sep 17 00:00:00 2001 From: Alexander Heldt Date: Sat, 8 Nov 2025 15:12:31 +0100 Subject: [PATCH] tisbury-treasure-hunt v2 --- .../src/tisbury_treasure_hunt.gleam | 54 +++++++------------ 1 file changed, 20 insertions(+), 34 deletions(-) diff --git a/tisbury-treasure-hunt/src/tisbury_treasure_hunt.gleam b/tisbury-treasure-hunt/src/tisbury_treasure_hunt.gleam index 522793e..a0a9578 100644 --- a/tisbury-treasure-hunt/src/tisbury_treasure_hunt.gleam +++ b/tisbury-treasure-hunt/src/tisbury_treasure_hunt.gleam @@ -3,8 +3,7 @@ import gleam/list pub fn place_location_to_treasure_location( place_location: #(String, Int), ) -> #(Int, String) { - let #(p, l) = place_location - #(l, p) + #(place_location.1, place_location.0) } pub fn treasure_location_matches_place_location( @@ -18,20 +17,11 @@ 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 - } + treasures + |> list.filter(fn(t) { + treasure_location_matches_place_location(place.1, t.1) }) + |> list.length } pub fn special_case_swap_possible( @@ -39,24 +29,20 @@ pub fn special_case_swap_possible( 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 + case found_treasure, desired_treasure, place { + #("Brass Spyglass", _), _, #("Abandoned Lighthouse", _) -> True + #("Amethyst Octopus", _), #("Crystal Crab", _), #("Stormy Breakwater", _) -> + True + #("Amethyst Octopus", _), #("Glass Starfish", _), #("Stormy Breakwater", _) -> + True + #("Vintage Pirate Hat", _), + #("Model Ship in Large Bottle", _), + #("Harbor Managers Office", _) + -> True + #("Vintage Pirate Hat", _), + #("Antique Glass Fishnet Float", _), + #("Harbor Managers Office", _) + -> True + _, _, _ -> False } }