Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Part 1
- [string[]]$moves = $puzzleInput -split '\n' | Select -SkipLast 1
- $position = [ordered]@{}
- $position.move = ''
- $position.H = [ordered]@{}
- $position.H.x = 0
- $position.H.y = 0
- $position.T = [ordered]@{}
- $position.T.x = 0
- $position.T.y = 0
- $tHistory = [System.Collections.Generic.List[string]]::New()
- foreach ($move in $moves) {
- $direction, [int]$steps = $move -split '\s'
- $position.move = $move
- for ($step = 1; $step -le $steps; $step++) {
- # Move H
- switch ($direction) {
- 'L' {$position.H.x--}
- 'R' {$position.H.x++}
- 'U' {$position.H.y++}
- 'D' {$position.H.y--}
- }
- # Move Tail?
- $pDiffX = $position.H.x - $position.T.x
- $pDiffY = $position.H.y - $position.T.y
- if ($pDiffX -gt 1 -or $pDiffX -lt -1 -or $pDiffY -gt 1 -or $pDiffY -lt -1) {
- switch ($direction) {
- 'L' {
- $position.T.x = $position.H.x+1
- $position.T.y = $position.H.y
- }
- 'R' {
- $position.T.x = $position.H.x-1
- $position.T.y = $position.H.y
- }
- 'U' {
- $position.T.x = $position.H.x
- $position.T.y = $position.H.y-1
- }
- 'D' {
- $position.T.x = $position.H.x
- $position.T.y = $position.H.y+1
- }
- }
- $tHistoryStr = '{0},{1}' -f $position.T.x,$position.T.y
- $tHistory.Add($tHistoryStr)
- }
- }
- $tHistoryHS = [System.Collections.Generic.HashSet[string]]::New([string[]]$tHistory)
- $tHistoryHS.count
Advertisement
Add Comment
Please, Sign In to add comment