Skip to content

Instantly share code, notes, and snippets.

@object
Created December 7, 2020 10:12
Show Gist options
  • Select an option

  • Save object/b0c4ca2da3c57252d4ce2770b8723d75 to your computer and use it in GitHub Desktop.

Select an option

Save object/b0c4ca2da3c57252d4ce2770b8723d75 to your computer and use it in GitHub Desktop.
AdventOfCode 2020, December 7
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