Browse Source

2020 day 4 cleanup

master
Terrana Ninetailed 4 months ago
parent
commit
bb0eb91580
3 changed files with 10 additions and 8 deletions
  1. +1
    -0
      .gitattributes
  2. +3
    -2
      2020/day04/part1.hs
  3. +6
    -6
      2020/day04/part2.hs

+ 1
- 0
.gitattributes View File

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

+ 3
- 2
2020/day04/part1.hs View File

@@ -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

+ 6
- 6
2020/day04/part2.hs View File

@@ -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