Skip to content

Instantly share code, notes, and snippets.

@plecong
Created December 15, 2015 00:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save plecong/8b34d9ccc9c527a9b87c to your computer and use it in GitHub Desktop.
Save plecong/8b34d9ccc9c527a9b87c to your computer and use it in GitHub Desktop.
#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