Skip to content

Instantly share code, notes, and snippets.

@sullyj3
Last active December 8, 2020 08:00
Show Gist options
  • Save sullyj3/11fb486a6c3917e7f8780bc1de1d0840 to your computer and use it in GitHub Desktop.
Save sullyj3/11fb486a6c3917e7f8780bc1de1d0840 to your computer and use it in GitHub Desktop.
letterChar : Char -> Boolean
letterChar c = List.contains c <| toCharList "abcdefghijklmnopqrstuvwxyz"
groupAnswers : Text -> Map Char Nat
groupAnswers gr =
combine : Map Char Nat -> Char -> Map Char Nat
combine accMap c =
if letterChar c then
use Nat +
putWith (+) c 1 accMap
else accMap
List.foldLeft combine Map.empty <| toCharList gr
List.splitSubSeq : [a] -> [a] -> [[a]]
List.splitSubSeq lst sep =
go : [a] -> [a] -> [[a]] -> [[a]]
go lst' currSeq lists =
match stripPrefix sep lst' with
Some rest -> go rest [] (lists :+ currSeq)
None ->
match lst' with
x +: xs -> go xs (currSeq :+ x) lists
[] -> lists :+ currSeq
go lst [] []
Text.splitSubSeq : Text -> Text -> [Text]
Text.splitSubSeq s sep =
List.map fromCharList (List.splitSubSeq (toCharList s) (toCharList sep))
day6 : Text -> Nat
day6 input =
use List map
groups = Text.splitSubSeq input "\n\n"
theGroupAnswers = map groupAnswers groups
sum <| map Map.size theGroupAnswers
doDay6 : '{IO} ()
doDay6 =
'let
f = openFile fp Read
contents = getText f
printLine (Nat.toText <| day6 contents)
@sullyj3
Copy link
Author

sullyj3 commented Dec 8, 2020

As a data point, it seems like .base.Text.split, a simpler version of this function, is implemented in terms of toCharList

  split : Char -> Text -> [Text]
  split separator text =
    use Universal ==
    go acc rest =
      match indexOf separator rest with
        None   -> acc :+ fromCharList rest
        Some n ->
          use Nat +
          go (acc :+ fromCharList (List.take n rest)) (List.drop (n + 1) rest)
    if text == "" then [] else go [] (toCharList text)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment