Created
December 7, 2020 10:12
-
-
Save object/b0c4ca2da3c57252d4ce2770b8723d75 to your computer and use it in GitHub Desktop.
AdventOfCode 2020, December 7
This file contains hidden or 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
| open System | |
| open System.IO | |
| type BagColor = string * string | |
| type BagQuantity = { | |
| Color : BagColor | |
| Count : int | |
| } | |
| type BagRule = { | |
| Color : BagColor | |
| RequiredBags : BagQuantity list | |
| } | |
| let rec parseRequiredBags (words : string list) requiredBags = | |
| match words with | |
| | [] -> requiredBags | |
| | words when words.[0] = "no" -> requiredBags | |
| | words -> | |
| let requiredBag = { | |
| Color = words.[1], words.[2] | |
| Count = Int32.Parse (words.[0]) | |
| } | |
| parseRequiredBags words.[4..] (requiredBag :: requiredBags) | |
| let addRule (words : string list) = { | |
| Color = words.[0], words.[1] | |
| RequiredBags = parseRequiredBags words.[4..] [] | |
| } | |
| let rec computeBagCoung initColor initCount rules : int = | |
| rules | |
| |> List.map (fun (rule : BagRule) -> | |
| if rule.Color = initColor | |
| then | |
| let dependentCount = | |
| rule.RequiredBags | |
| |> List.map (fun x -> computeBagCoung x.Color (initCount * x.Count) rules) | |
| |> List.sum | |
| initCount + dependentCount | |
| else | |
| 0) | |
| |> List.sum | |
| File.ReadAllLines("input07.txt") | |
| |> Seq.toList | |
| |> List.filter (not << String.IsNullOrEmpty) | |
| |> List.map (fun x -> x.Split(' ') |> Seq.toList) | |
| |> List.map addRule | |
| |> computeBagCoung ("shiny", "gold") 1 | |
| |> fun x -> x - 1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment