Created
December 15, 2015 00:45
-
-
Save plecong/8b34d9ccc9c527a9b87c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#r "FSharp.Data.dll" | |
open FSharp.Data | |
open System.IO | |
let rec addNumbers (input : JsonValue) : int = | |
match input with | |
| JsonValue.Number n -> int n | |
| JsonValue.Array a -> | |
a |> Seq.map addNumbers |> Seq.sum | |
| JsonValue.Record r -> | |
r |> Seq.map (fun (x, y) -> addNumbers y) |> Seq.sum | |
| _ -> 0 | |
let isRedObject (input : (string * JsonValue) []) = | |
input | |
|> Seq.exists (fun (x, y) -> | |
match y with | |
| JsonValue.String s -> s = "red" | |
| _ -> false | |
) | |
let rec addNumbersPart2 (input : JsonValue) : int = | |
match input with | |
| JsonValue.Number n -> int n | |
| JsonValue.Array a -> | |
a | |
|> Seq.map addNumbersPart2 | |
|> Seq.sum | |
| JsonValue.Record r -> | |
if (isRedObject r) then 0 | |
else | |
r | |
|> Seq.map (fun (x, y) -> addNumbersPart2 y) | |
|> Seq.sum | |
| _ -> 0 | |
let part1 = addNumbers (JsonValue.Parse (File.ReadAllText(@"./AdventOfCode_Day12_input.txt"))) | |
let part2 = addNumbersPart2 (JsonValue.Parse (File.ReadAllText(@"./AdventOfCode_Day12_input.txt"))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment