Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Part 1
- [string[]]$bothInstructions = $puzzleInput -split '\n' | Select -SkipLast 1
- $startingPosition = $bothInstructions[0..7]
- $moveInstructions = $bothInstructions[10..$bothInstructions.count]
- # Populate stacks
- $stackCollection = @{}
- $columnidx = 1
- 1..9 | foreach {
- [int]$stack = $_
- $stackCollection.$stack = [System.Collections.Generic.List[char]]::new()
- Write-Verbose "Working stack $stack" -Verbose
- 0..7 | foreach {
- [int]$row = $_
- if (-not [string]::IsNullOrWhiteSpace($startingPosition[$row][$columnidx])) {
- ($stackCollection.$stack).Add($startingPosition[$row][$columnidx])
- }
- }
- $columnidx += 4
- ($stackCollection.$stack).Reverse()
- }
- # Do moves
- foreach ($moveStr in $moveInstructions) {
- [int]$move,[int]$from,[int]$to = $moveStr -split 'move|from|to|\s' | Where {-not [string]::IsNullOrWhiteSpace($_)}
- 1..$move | Foreach {
- [char]$moveItem = ($stackCollection.$from)[-1]
- ($stackCollection.$from).RemoveAt($stackCollection.$from.count-1)
- ($stackCollection.$to).Add($moveItem)
- }
- }
- (1..9 | foreach {($stackCollection.$_)[-1]}) -join ''
- # Part 2
- # using clean $stackCollection
- foreach ($moveStr in $moveInstructions) {
- [int]$move,[int]$from,[int]$to = $moveStr -split 'move|from|to|\s' | Where {-not [string]::IsNullOrWhiteSpace($_)}
- if ($move -gt 1) {
- [char[]]$moveItems = $null
- 1..$move | Foreach {
- [char]$moveItem = ($stackCollection.$from)[-1]
- ($stackCollection.$from).RemoveAt($stackCollection.$from.count-1)
- [char[]]$moveItems += $moveItem
- }
- [array]::Reverse($moveItems)
- $moveItems | Foreach {($stackCollection.$to).Add($_)}
- }
- else {
- [char]$moveItem = ($stackCollection.$from)[-1]
- ($stackCollection.$from).RemoveAt($stackCollection.$from.count-1)
- ($stackCollection.$to).Add($moveItem)
- }
- }
- (1..9 | foreach {($stackCollection.$_)[-1]}) -join ''
Advertisement
Add Comment
Please, Sign In to add comment