-
Notifications
You must be signed in to change notification settings - Fork 0
/
day5.ps1
127 lines (80 loc) · 3.1 KB
/
day5.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# Day 5 : Supply Stacks
#########
## Part 1
#########
# used to check for valid boxes
$upper = [char[]]([char]'A'..[char]'Z')
$instr = get-content .\input.txt
$graph = Get-Content .\graph.txt
# read file from bottom up and put into an array
$line = 1
$list = 1..$graph.Count | ForEach-Object {$graph[-$Line]; $Line++}
#get the position of the columns
$columns = 1..9 | foreach-object { $list[1].IndexOf("$($_)")}
#create a stack for each
Get-Variable -Name Stack_* | Remove-Variable
1..9 | foreach-object { new-object "system.collections.stack" | New-Variable -Name $("stack_$_")}
# fill each stack
foreach ($col in $columns) {
$n = $columns.IndexOf($col) + 1
for ($y=2 ; $y -lt $list.count; $y++) {
$box = $list[$y].Substring($col,1)
if ($box -in $upper) { $(Get-Variable -name "stack_$n").value.push($box) }
}
}
foreach ($op in $instr) {
$op = invoke-expression $($op -replace "move","" -replace "from","," -replace "to","," -replace " ","")
$srcstack = $op[1]
$dststack = $op[2]
$moves = $op[0]
for ($x = 1; $x -le $moves; $x++) {
$box = $(Get-Variable -name "stack_$srcstack").value.pop()
$(Get-Variable -name "stack_$dststack").value.push($box)
}
}
#all moves complete. Pick top box on each
$sb = [System.Text.StringBuilder]::new()
1..9 | foreach-object {$box = $(get-Variable -Name "stack_$_").value.peek(); [void]$sb.Append($box)}
$sb.ToString()
#########
## Part 2
#########
# used to check for valid boxes
$upper = [char[]]([char]'A'..[char]'Z')
$instr = get-content .\input.txt
$graph = Get-Content .\graph.txt
# read file from bottom up and put into an array
$line = 1
$list = 1..$graph.Count | ForEach-Object {$graph[-$Line]; $Line++}
#get the position of the columns
$columns = 1..9 | foreach-object { $list[1].IndexOf("$($_)")}
#create a stack for each
Get-Variable -Name Stack_* | Remove-Variable
1..9 | foreach-object { new-object "system.collections.stack" | New-Variable -Name $("stack_$_")}
# fill each stack
foreach ($col in $columns) {
$n = $columns.IndexOf($col) + 1
for ($y=2 ; $y -lt $list.count; $y++) {
$box = $list[$y].Substring($col,1)
if ($box -in $upper) { $(Get-Variable -name "stack_$n").value.push($box) }
}
}
foreach ($op in $instr) {
$op = invoke-expression $($op -replace "move","" -replace "from","," -replace "to","," -replace " ","")
$srcstack = $op[1]
$dststack = $op[2]
$moves = $op[0]
$tmpstack = new-object "system.collections.stack"
for ($x = 1; $x -le $moves; $x++) {
$box = $(Get-Variable -name "stack_$srcstack").value.pop()
$tmpstack.push($box)
}
while ($tmpstack.count -gt 0 ) {
$box = $tmpstack.pop()
$(Get-Variable -name "stack_$dststack").value.push($box)
}
}
#all moves complete. Pick top box on each
$sb = [System.Text.StringBuilder]::new()
1..9 | foreach-object {$box = $(get-Variable -Name "stack_$_").value.peek(); [void]$sb.Append($box)}
$sb.ToString()