Browse Source

2020 day 4 cleanup

master
Terrana Ninetailed 2 years ago
parent
commit
bb0eb91580
  1. 1
      .gitattributes
  2. 5
      2020/day04/part1.hs
  3. 12
      2020/day04/part2.hs

1
.gitattributes

@ -0,0 +1 @@
input text eol=lf

5
2020/day04/part1.hs

@ -27,8 +27,9 @@ dlfsplitonce :: String -> (String, String)
dlfsplitonce [] = ([], [])
dlfsplitonce (ca:cb:rest)
| ca == '\n' && cb == '\n' = ([], rest)
| otherwise = (ca:before, after) where (before, after) = dlfsplitonce (cb : rest)
dlfsplitonce (c:rest) = (c:before, after) where (before, after) = dlfsplitonce rest
| otherwise = (ca:before, after)
where (before, after) = dlfsplitonce (cb : rest)
dlfsplitonce c = (c, [])
-- Split a string across all spaces and newlines

12
2020/day04/part2.hs

@ -32,7 +32,7 @@ valid_hgt v = case unit of
"cm" -> inrange 150 193 $ read val
"in" -> inrange 59 76 $ read val
otherwise -> False
where (val, unit) = break (not . isDigit) v
where (val, unit) = span isDigit v
-- Check a passport has all required fields
has_required_fields :: [(String, String)] -> Bool
@ -57,9 +57,9 @@ fields str = (k, tail v) where (k, v) = break (==':') str
-- Split a string into a list across all the double newlines in it
dlfsplit :: String -> [String]
dlfsplit str = uncurry (:) $ (a, case b of
dlfsplit str = a : case b of
[] -> []
b -> dlfsplit b)
b -> dlfsplit b
where (a,b) = dlfsplitonce str
-- Split a string into two across the first double newline found
@ -67,9 +67,9 @@ dlfsplitonce :: String -> (String, String)
dlfsplitonce [] = ([], [])
dlfsplitonce (ca:cb:rest)
| ca == '\n' && cb == '\n' = ([], rest)
| otherwise = (ca:before, after) where (before, after) = dlfsplitonce (cb : rest)
dlfsplitonce (c:rest) = (c:before, after) where (before, after) = dlfsplitonce rest
| otherwise = (ca:before, after)
where (before, after) = dlfsplitonce (cb:rest)
dlfsplitonce c = (c, [])
-- Split a string across all spaces and newlines
spacesplit :: String -> [String]

Loading…
Cancel
Save