-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAdvent Of Code Day 7 part2.ps1
41 lines (39 loc) · 1.32 KB
/
Advent Of Code Day 7 part2.ps1
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
$inputdata = (get-content -Raw .\aocd7input.txt) -split "`n"
$colorrules=@{}
foreach($rule in $inputdata){
if($rule -eq ""){
continue
} else {
$rule = $rule -replace "bags", ""
$rule = $rule -replace "bag", ""
$outerbag, $innerbags = $rule -split " contain ", 2
$innerbagsrule= $innerbags-split ","
$outerbag=$outerbag.trim() -replace '[^a-zA-Z]',""
$colorrules[$outerbag]=@{}
foreach($myrule in $innerbagsrule){
$count, $color = $myrule.trim() -split " ",2
$color = $color.trim() -replace '[^a-zA-Z]',""
#write-host "Color:$color-Count:$count"
$colorrules[$outerbag].add($color,$count)
}
}
}
function get-innerbags(){
param(
$BagColor
)
if($bagcolor -eq "other"){return 1} else {
$directbags = $colorrules.$BagColor.values |?{$_ -ne "no"}|measure-object -sum|select-object -ExpandProperty Sum
foreach($bag in $colorrules.$BagColor.keys){
$numofbag = $colorrules.$BagColor.$bag
if($numofbag -eq "no"){
} else {
for($i=0; $i -lt $numofbag;$i++){
get-innerbags -BagColor $bag
}
$numofbag
}
}
}
}
get-innerbags -BagColor "shinygold" | measure-object -Sum