diff options
Diffstat (limited to 'day9.fsx')
-rw-r--r-- | day9.fsx | 25 |
1 files changed, 25 insertions, 0 deletions
diff --git a/day9.fsx b/day9.fsx new file mode 100644 index 0000000..61cba2a --- /dev/null +++ b/day9.fsx @@ -0,0 +1,25 @@ +open System.IO +open System.Text.RegularExpressions + +let input = (File.ReadAllText "day9.txt").Trim() +let exclRegex = new Regex("!.") +let garbageRegex = new Regex("<.*?>") + +let part1 input = + let stripped = garbageRegex.Replace(exclRegex.Replace(input, ""), "") |> String.filter (fun x -> x <> ',') + let rec sumLevels input level acc = + if input = "" then acc else + + if input[0] = '{' then + sumLevels input[1..] (level + 1) (acc + level) + else + sumLevels input[1..] (level - 1) acc + + sumLevels stripped 1 0|> printfn "%A" + +let part2 input = + let stripped = exclRegex.Replace(input, "") + garbageRegex.Matches(stripped) |> Seq.map (fun x -> x.Groups[0].Value.Length - 2) |> Seq.sum |> printfn "%d" + +part1 input +part2 input \ No newline at end of file |