Day 1. Success!

master
Dustin Swan 1 month ago
commit 12d6457332
Signed by: dustinswan
GPG Key ID: AB49BD6B2B3A6377

File diff suppressed because it is too large Load Diff

@ -0,0 +1,37 @@
app [main] {
pf: platform "https://github.com/roc-lang/basic-cli/releases/download/0.17.0/lZFLstMUCUvd5bjnnpYromZJXkQUrdhbva4xdBInicE.tar.br",
}
import pf.Stdout
import "data.txt" as data : Str
main =
lines = Str.splitOn data "\n"
|> List.keepIf (\l -> Str.countUtf8Bytes l > 0) # the last line is empty. only keep non-empties
# |> List.dropLast 1 # cleaner but scarier
# split the lines into a 2-tuple of lists
lists = List.walk lines ([], []) \state, elem ->
parts = Str.splitOn elem " "
# man I gotta figure out how to deal with errors better, this is not pretty
# tried using try but couldn't get walk to work with functions that return errors
left = Str.toU64 (Result.withDefault (List.first parts) "0")
right = Str.toU64 (Result.withDefault (List.last parts) "0")
(List.appendIfOk state.0 left, List.appendIfOk state.1 right)
sorted = (List.sortAsc lists.0, List.sortAsc lists.1)
Stdout.line! "Distance: $(Num.toStr (part1 sorted))"
Stdout.line! "Similarity: $(Num.toStr (part2 sorted))"
# tried to dry this up by having 1 walk and a record for part1 and part2, but it got ugly
part1 = \(left, right) ->
List.map2 left right Pair
|> List.walk 0 \state, (Pair l r) ->
state + (Num.absDiff l r)
part2 = \(left, right) ->
List.map2 left right Pair
|> List.walk 0 \state, (Pair l _) ->
count = List.countIf right (\e -> e == l)
state + count * l

@ -0,0 +1,6 @@
3 4
4 3
2 5
1 3
3 9
3 3
Loading…
Cancel
Save