diff options
| Commit | 51a684a291963fbe8bb3f81ef04282233d083efa (patch) | |
|---|---|---|
| Author | 2023-01-12 08:52:28 +0000 | |
| Committer | 2023-01-12 08:52:28 +0000 | |
| Files | 974829e48dfa9c722a0eaa9c3104c9d65cc6722a | |
| uni-projects-51a684a291963fbe8bb3f81ef04282233d083efa.tar uni-projects-51a684a291963fbe8bb3f81ef04282233d083efa.tar.gz uni-projects-51a684a291963fbe8bb3f81ef04282233d083efa.zip | ||
| Parent | 9d9bd35065ded1d78c9a07af2953e69fc8eb61d4 (diff) | |
[FP/HW4] decode now accepts a whole file
1 files changed, 7 insertions, 4 deletions
| -rw-r--r-- | Year 2/Functional Programming/hw4.hs | 11 |
diff --git a/Year 2/Functional Programming/hw4.hs b/Year 2/Functional Programming/hw4.hs index f548527..db7bf96 100644 --- a/Year 2/Functional Programming/hw4.hs +++ b/Year 2/Functional Programming/hw4.hs @@ -10,9 +10,12 @@ hasAllCharsIn :: String -> String -> Bool hasAllCharsIn a b = all (\c -> c `elem` b) a decode :: String -> Int -decode i = read $ parseNumbers (getSegments $ sortBy (\s1 s2 -> compare (length s1) (length s2)) +decode f = sum (map (\l -> decodeLine l) (lines f)) + +decodeLine :: String -> Int +decodeLine i = read $ parseNumbers (getSegments $ sortBy (\s1 s2 -> compare (length s1) (length s2)) (take 10 (words i))) - (drop 11 (words i)) + (drop 11 (words i)) where -- След като написах огромна имплементация, която намира всеки сегмент какъв му е знака, забелязах че -- може да използваме само "групи" сегменти, за да различим всички цифри с еднакви дължини. @@ -82,9 +85,9 @@ versionSum i = calc (concat (map toBitString i)) 0 main :: IO() main = do sample <- readFile "sample.txt" - print(sum (map (\l -> decode l) (lines sample))) -- -> 61229 + print(decode sample) -- -> 61229 input <- readFile "input.txt" - print(sum (map (\l -> decode l) (lines input))) -- -> 983026 + print(decode input) -- -> 983026 print(versionSum "8A004A801A8002F478") -- -> 16 print(versionSum "620080001611562C8802118E34") -- -> 12 |
