Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # ---------------------------- Advent of code 2020 Day 12 --------------------------- #
- # Get input
- Clear-Host
- [System.Collections.Generic.List[psobject]]$puzzleInput = Get-Content -Path "$env:USERPROFILE\Downloads\input.txt"
- $instructions = $puzzleInput | ForEach-Object {
- [PSCustomObject]@{
- "Direction" = $_.Substring(0, 1)
- "Moves" = $_.Substring(1)
- }
- }
- $currentDirection = 90
- $currentPosition = @{"NorthSouth" = 0; "EastWest" = 0 }
- foreach ($instruction in $instructions) {
- "Currently at $($currentPosition.Values)"
- "Current direction is $currentDirection"
- switch ($instruction.direction) {
- "N" {
- "Now moving North by $($instruction.moves)"
- $currentPosition.NorthSouth = $currentPosition.NorthSouth + $instruction.moves
- }
- "S" {
- "Now moving South by $($instruction.moves)"
- $currentPosition.NorthSouth = $currentPosition.NorthSouth - $instruction.moves
- }
- "E" {
- "Now moving East by $($instruction.moves)"
- $currentPosition.EastWest = $currentPosition.EastWest + $instruction.moves
- }
- "W" {
- "Now moving West by $($instruction.moves)"
- $currentPosition.EastWest = $currentPosition.EastWest - $instruction.moves
- }
- "L" {
- "Now turning Left by $($instruction.moves)"
- $currentDirection = ($currentDirection - $instruction.moves) % 360
- if ($currentDirection -lt 0) {
- $currentDirection = $currentDirection + 360
- }
- }
- "R" {
- "Now turning Right by $($instruction.moves)"
- $currentDirection = ($currentDirection + $instruction.moves) % 360
- if ($currentDirection -lt 0) {
- $currentDirection = $currentDirection + 360
- }
- }
- "F" {
- "Now moving Forward by $($instruction.moves)"
- switch ($currentDirection) {
- 0 { $currentPosition.NorthSouth = $currentPosition.NorthSouth + $instruction.moves }
- 90 { $currentPosition.EastWest = $currentPosition.EastWest + $instruction.moves }
- 180 { $currentPosition.NorthSouth = $currentPosition.NorthSouth - $instruction.moves }
- 270 { $currentPosition.EastWest = $currentPosition.EastWest - $instruction.moves }
- }
- }
- }
- "..."
- }
- "Final position is"
- $currentPosition
- "Puzzle solution is"
- [Math]::Abs($currentPosition.NorthSouth) + [Math]::Abs($currentPosition.EastWest)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement