Last active
February 3, 2018 04:08
-
-
Save matsubara0507/10a44fb2a9676bde9658c3eeec456676 to your computer and use it in GitHub Desktop.
「積み木の水槽」問題の Haskell 回答 (問題 http://nabetani.sakura.ne.jp/hena/ord13blocktup/ , 回答 https://rosettacode.org/wiki/Water_collected_between_towers#Haskell)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Data.Bool (bool) | |
import Data.List (scanl1, scanr1) | |
main :: IO () | |
main = print $ all ((==) <$> snd <*> (show . solve . fst)) testSet | |
solve = sum . fmap (sum . (zipWith (-) =<< (zipWith min . scanl1 max <*> scanr1 max)) . fmap (read . (: []))) . words . fmap (bool ' ' <*> (/=) '0') | |
testSet :: [(String, String)] | |
testSet = | |
[ ("83141310145169154671122", "24") -- 0 | |
, ("923111128", "45") -- 1 | |
, ("923101128", "1") -- 2 | |
, ("903111128", "9") -- 3 | |
, ("3", "0") -- 4 | |
, ("31", "0") -- 5 | |
, ("412", "1") -- 6 | |
, ("3124", "3") -- 7 | |
, ("11111", "0") -- 8 | |
, ("222111", "0") -- 9 | |
, ("335544", "0") -- 10 | |
, ("1223455321", "0") -- 11 | |
, ("000", "0") -- 12 | |
, ("000100020003121", "1") -- 13 | |
, ("1213141516171819181716151413121", "56") -- 14 | |
, ("712131415161718191817161514131216", "117") -- 15 | |
, ("712131405161718191817161514031216", "64") -- 16 | |
, ("03205301204342100", "1") -- 17 | |
, ("0912830485711120342", "18") -- 18 | |
, ("1113241120998943327631001", "20") -- 19 | |
, ("7688167781598943035023813337019904732", "41") -- 20 | |
, ("2032075902729233234129146823006063388", "79") -- 21 | |
, ("8323636570846582397534533", "44") -- 22 | |
, ("2142555257761672319599209190604843", "41") -- 23 | |
, ("06424633785085474133925235", "51") -- 24 | |
, ("503144400846933212134", "21") -- 25 | |
, ("1204706243676306476295999864", "21") -- 26 | |
, ("050527640248767717738306306596466224", "29") -- 27 | |
, ("5926294098216193922825", "65") -- 28 | |
, ("655589141599534035", "29") -- 29 | |
, ("7411279689677738", "34") -- 30 | |
, ("268131111165754619136819109839402", "102") -- 31 | |
] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment