-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAdventOfCode - Day 7.2.ps1
50 lines (39 loc) · 1.26 KB
/
AdventOfCode - Day 7.2.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
42
43
44
45
46
47
48
49
50
$rules = @()
function countChildren($ruleName, $rules) {
$bagCount = 0
$rule = $rules | ? { $_.name -eq $ruleName }
if ($rule.contents.Count -gt 0) {
$rule.contents | % {
$bagCount += $_.amount
$bagCount += $_.amount * (countChildren $_.name $rules)
}
return $bagCount
} else {
return 0
}
}
Get-Content -Path '.\luggagerules day 7.txt' | % {
$rulePart = ($_ -split 'contain')[0]
$contentPart = ($_ -split 'contain')[1]
if ($rulePart -match "([a-z| ]+) bags") {
$ruleName = $Matches[1]
$contentParts = $contentPart -split ','
$contents = @()
$contentParts | % {
if ($_ -match "(\d) ([a-z| ]+) bag[s]?\.?") {
$contentAmount = [int]::Parse($Matches[1])
$contentName = $Matches[2]
$contents += new-object -TypeName PSObject -Property @{
'amount'=$contentAmount;
'name'=$contentName
}
}
}
}
$rules += new-object -TypeName PSObject -Property @{
'name'=$ruleName;
'contents'=$contents
}
}
$ruleName = "shiny gold"
countChildren $ruleName $rules