~deciduously/aoc2020

0791265170f221abbc25cd1a4ca2a69e1525662c — Ben Lovy 7 months ago f08c2fb master
Day 7 Part 2
1 files changed, 17 insertions(+), 4 deletions(-)

M day7.nim
M day7.nim => day7.nim +17 -4
@@ 56,9 56,15 @@ proc readRules(input: seq[string]): RulesTable =
                ret[bag_name].add(bagContentFromStr(entry))
    ret

proc part1(input: seq[string]): int =
    let rules = readRules(input)
    let target = "shiny gold"
proc countSubBags(contents: seq[BagContent], rules: RulesTable): int =
    var ret = 1 # Start with the current bag as 1
    # Count bags in this level, and all children
    for entry in contents:
        let subentry = rules[entry.name]
        ret += entry.amt * subentry.countSubBags(rules)
    ret

proc part1(rules: RulesTable, target: string): int =
    var res = 0
    # Read every single entry.
    for contents in rules.values:


@@ 70,6 76,13 @@ proc part1(input: seq[string]): int =
            continue
    res

proc part2(rules: RulesTable, target: string): int =
    let top = rules[target]
    top.countSubBags(rules) - 1 # Ignore top-level bag

proc day7*() =
    let input = readLinesToStrSeq("puzzles/day7.txt")
    echo "Part 1: ", part1(input)
\ No newline at end of file
    let rules = readRules(input)
    let target = "shiny gold"
    echo "Part 1: ", part1(rules, target)
    echo "Part 2: ", part2(rules, target)
\ No newline at end of file