-
Notifications
You must be signed in to change notification settings - Fork 0
/
LuggageCombosTwo.lua
executable file
·66 lines (51 loc) · 1.95 KB
/
LuggageCombosTwo.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#!/bin/env lua
-- ___ __ _____ ___ _ ___
-- | \ __ _ _ _ / \__ | | _ \__ _ _ _| |_ |_ )
-- | |) / _` | || | | () |/ / | _/ _` | '_| _| / /
-- |___/\__,_|\_, | \__//_/ |_| \__,_|_| \__| /___|
-- |__/
--
-- "Handy Haversacks"
--
-- Challenge:
-- Reverse of LuggageCombos.lua, we need to traverse the potential children of
-- the given bag type and count how many bags "one gold shiny bag" contains.
-- Grab all my functions from Part One
dofile "LuggageCombos.lua"
INPUT = 'luggage_rules.txt'
-- Collect all puzzle rules into a table like this:
-- rules[parent-color][child-color] == allowable-quantity
rules = {}
-- Find the children of a particular bag
function getchildren (parentcolor, depth)
if not depth then depth = 0 end
quantity = 0
-- Given bag type has no allowable parents. This is the inner-most nesting doll
if isempty(rules[parentcolor]) then
print("|" .. string.rep(" ", depth) .. parentcolor)
-- No bags inside this one
return quantity
end
-- For the types of bags this can contain, how many?
for childcolor, requiredquantity in pairs(rules[parentcolor]) do
print("|" .. string.rep(" ", depth) .. "- " .. parentcolor .. " > " .. requiredquantity .. " x " .. childcolor)
-- This bag contains: required number of this type
quantity = quantity + requiredquantity
-- And all its children
quantity = quantity + (requiredquantity * getchildren(childcolor, depth + 1))
-- Report on what we've collected at this level
print("|" .. string.rep(" ", depth) .. " " .. quantity)
end
return quantity
end
--
-- MAIN:
--
for rule in io.lines(INPUT) do
parserule(rule, "parttwo")
end
print("PART TWO: Looking for all descendants of a shiny gold bag")
totalcount = getchildren("shiny gold")
print("\n** Within the shiny gold bag, there are " .. math.floor(totalcount) .. " bags.")
-- Part Two answer:
-- ** Within the shiny gold bag, there are 45018 bags.