crlogic

Day9

Dec 9th, 2022
27
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.90 KB | None | 0 0
  1. # Part 1
  2. [string[]]$moves = $puzzleInput -split '\n' | Select -SkipLast 1
  3. $position = [ordered]@{}
  4. $position.move = ''
  5. $position.H = [ordered]@{}
  6. $position.H.x = 0
  7. $position.H.y = 0
  8. $position.T = [ordered]@{}
  9. $position.T.x = 0
  10. $position.T.y = 0
  11.  
  12. $tHistory = [System.Collections.Generic.List[string]]::New()
  13.  
  14. foreach ($move in $moves) {
  15. $direction, [int]$steps = $move -split '\s'
  16. $position.move = $move
  17. for ($step = 1; $step -le $steps; $step++) {
  18. # Move H
  19. switch ($direction) {
  20. 'L' {$position.H.x--}
  21. 'R' {$position.H.x++}
  22. 'U' {$position.H.y++}
  23. 'D' {$position.H.y--}
  24. }
  25. # Move Tail?
  26. $pDiffX = $position.H.x - $position.T.x
  27. $pDiffY = $position.H.y - $position.T.y
  28. if ($pDiffX -gt 1 -or $pDiffX -lt -1 -or $pDiffY -gt 1 -or $pDiffY -lt -1) {
  29. switch ($direction) {
  30. 'L' {
  31. $position.T.x = $position.H.x+1
  32. $position.T.y = $position.H.y
  33. }
  34. 'R' {
  35. $position.T.x = $position.H.x-1
  36. $position.T.y = $position.H.y
  37. }
  38. 'U' {
  39. $position.T.x = $position.H.x
  40. $position.T.y = $position.H.y-1
  41. }
  42. 'D' {
  43. $position.T.x = $position.H.x
  44. $position.T.y = $position.H.y+1
  45. }
  46. }
  47. $tHistoryStr = '{0},{1}' -f $position.T.x,$position.T.y
  48. $tHistory.Add($tHistoryStr)
  49. }
  50. }
  51. $tHistoryHS = [System.Collections.Generic.HashSet[string]]::New([string[]]$tHistory)
  52. $tHistoryHS.count
  53.  
Advertisement
Add Comment
Please, Sign In to add comment