]> git.sommitrealweird.co.uk Git - advent-of-code-2020.git/commitdiff
Day 12
authorBrett Parker <iDunno@sommitrealweird.co.uk>
Sat, 12 Dec 2020 17:42:38 +0000 (17:42 +0000)
committerBrett Parker <iDunno@sommitrealweird.co.uk>
Sat, 12 Dec 2020 17:42:38 +0000 (17:42 +0000)
day12/17E_8S_25.txt [new file with mode: 0644]
day12/input.txt [new file with mode: 0644]
day12/ship-2.py [new file with mode: 0755]
day12/ship.py [new file with mode: 0755]
day12/ship.sh [new file with mode: 0755]
day12/summary.txt [new file with mode: 0644]

diff --git a/day12/17E_8S_25.txt b/day12/17E_8S_25.txt
new file mode 100644 (file)
index 0000000..d382291
--- /dev/null
@@ -0,0 +1,5 @@
+F10
+N3
+F7
+R90
+F11
diff --git a/day12/input.txt b/day12/input.txt
new file mode 100644 (file)
index 0000000..c64cbfd
--- /dev/null
@@ -0,0 +1,770 @@
+R90
+F58
+S2
+E4
+F28
+W3
+N2
+F25
+E1
+F24
+W2
+F91
+S3
+F83
+L90
+F24
+R90
+N3
+R90
+F5
+W5
+E1
+S1
+E5
+F47
+F83
+N1
+F64
+W2
+R90
+S1
+R180
+N2
+R180
+S3
+F35
+E2
+S3
+W2
+R270
+F57
+E5
+F100
+N3
+L90
+F70
+E5
+L90
+F65
+L90
+S2
+W5
+F73
+L90
+E3
+R90
+W2
+S2
+L180
+E4
+W3
+R90
+E2
+F18
+N4
+W5
+R90
+S1
+F55
+W2
+L90
+S1
+L180
+E4
+N1
+R90
+F31
+N5
+E4
+R90
+W4
+F18
+W4
+N2
+L90
+F6
+N3
+L180
+F89
+R90
+N4
+L180
+S4
+W1
+F87
+N1
+F80
+E4
+R90
+N4
+F38
+E5
+F74
+S4
+R180
+S1
+L90
+W1
+F88
+S1
+F42
+S1
+R90
+F2
+R90
+E2
+S1
+F57
+E1
+F53
+R180
+F97
+W2
+R90
+F70
+R180
+W1
+R180
+W5
+E5
+N1
+E5
+F63
+N3
+F24
+L90
+S5
+W5
+R90
+W4
+F27
+R90
+E2
+L180
+E4
+F16
+L90
+F47
+S3
+E1
+S2
+L90
+W5
+F80
+E4
+F10
+N5
+E3
+N4
+W2
+L90
+E1
+F83
+R180
+S4
+L90
+E1
+F94
+N2
+R90
+S5
+L180
+E1
+F40
+E5
+L180
+W5
+F57
+E1
+N5
+W5
+F62
+L180
+W3
+F12
+E4
+F93
+R270
+F8
+S3
+W3
+L90
+W1
+N1
+E4
+L90
+E2
+S2
+E1
+L270
+E5
+N5
+F81
+L90
+W5
+F82
+E5
+L90
+F24
+F3
+F14
+L180
+N5
+E1
+F11
+R90
+F22
+L90
+S1
+F82
+S4
+F38
+L90
+F31
+R180
+W4
+F88
+E3
+R90
+N5
+F9
+S3
+W4
+L90
+W5
+R90
+F5
+S4
+F91
+N4
+S4
+W4
+S4
+F78
+N2
+F67
+L180
+N3
+R90
+F65
+N2
+E1
+F46
+E2
+L180
+S4
+E2
+F98
+L90
+W2
+N1
+E3
+F7
+S4
+F90
+S2
+W3
+R90
+F30
+E4
+F2
+L90
+F17
+E4
+R90
+F94
+N3
+E5
+R180
+S4
+F76
+E2
+F94
+R90
+N5
+W2
+F89
+W1
+F83
+N5
+W5
+F62
+S1
+W4
+N5
+E2
+R90
+F14
+R90
+N5
+W4
+R180
+E2
+R90
+L90
+F67
+N4
+L90
+E4
+F93
+W5
+F85
+L180
+F45
+W2
+F78
+N3
+F90
+L90
+W1
+S2
+L90
+S5
+E5
+F82
+S4
+F36
+E5
+L90
+E1
+F13
+S2
+E3
+F65
+L90
+E3
+E4
+F1
+W5
+S3
+F14
+L90
+F47
+L90
+S3
+W4
+F18
+E1
+N3
+E5
+F58
+E5
+S1
+W2
+F48
+W5
+F65
+N5
+E1
+N3
+R90
+N2
+L90
+N4
+F21
+R90
+F58
+W1
+F7
+R90
+E4
+N5
+F47
+W4
+L90
+N4
+R90
+E1
+L180
+F8
+E3
+W4
+F41
+E3
+S5
+E5
+S3
+E3
+F14
+N4
+R90
+W3
+L90
+E5
+R180
+W4
+S1
+N2
+F93
+L90
+F41
+R180
+F37
+R90
+E1
+L180
+N5
+F96
+W3
+R90
+E1
+F88
+S2
+E2
+L90
+N2
+W4
+F34
+E4
+F69
+L90
+N3
+F18
+W3
+S4
+F44
+E2
+L90
+N2
+F55
+R90
+E1
+L90
+S5
+E2
+N3
+L90
+N1
+F55
+W5
+N2
+E2
+F20
+S2
+W5
+S1
+W5
+R180
+F100
+F44
+S2
+L90
+E3
+F98
+N3
+R90
+W4
+F14
+N2
+F87
+W5
+F12
+L180
+S2
+W1
+N1
+W1
+R90
+L180
+F64
+E2
+F41
+L90
+F20
+R270
+F91
+N4
+L180
+F28
+W4
+F40
+R90
+F40
+F36
+E3
+L270
+F76
+L90
+N2
+F15
+N5
+E4
+L180
+S1
+R90
+E1
+F81
+R90
+F96
+N1
+E5
+R90
+S5
+L90
+S1
+S4
+R90
+F30
+E1
+N5
+E2
+S3
+F97
+N3
+R90
+N1
+R90
+E3
+R180
+E3
+F8
+R90
+W2
+F27
+L90
+N5
+W3
+L90
+E5
+R180
+S1
+L90
+F51
+S4
+L180
+N1
+W4
+F71
+W2
+R90
+E4
+F29
+E4
+L90
+L90
+F68
+W2
+F57
+E3
+R90
+S2
+F94
+W4
+S2
+L180
+E3
+N2
+F52
+E3
+S1
+S5
+R90
+E1
+F35
+W3
+F53
+R270
+E3
+F81
+S2
+L90
+W4
+F86
+N5
+E4
+R90
+N5
+F99
+L180
+F65
+R90
+S2
+W3
+F33
+E3
+R270
+F34
+E1
+F56
+S2
+E5
+R180
+N2
+E3
+N3
+F30
+N2
+F22
+E5
+F10
+N2
+F16
+N1
+F31
+R90
+E2
+F78
+E4
+R180
+S3
+R90
+F80
+E5
+S1
+F49
+E1
+S2
+E4
+N4
+R90
+F9
+W1
+E4
+N3
+N5
+E4
+L90
+S5
+E2
+R90
+F74
+R180
+N2
+F98
+S2
+W4
+W4
+F73
+E2
+N4
+E1
+F25
+S2
+E5
+L90
+F96
+N2
+E3
+N3
+F16
+S5
+L90
+F43
+S2
+R90
+S1
+F46
+S3
+F82
+S5
+S4
+E5
+F58
+R90
+F51
+R90
+N4
+L90
+W3
+S4
+R90
+F15
+E1
+S4
+W3
+S2
+W1
+R90
+F76
+S1
+L180
+F5
+R180
+E1
+L180
+W3
+F47
+W4
+F68
+E5
+F75
+W3
+N1
+R90
+S1
+W5
+R90
+E2
+F78
+W2
+L90
+F24
+W5
+R90
+F75
+E1
+F35
+E5
+R90
+F57
+L180
+S1
+L90
+F90
+R180
+F63
+S3
+R180
+W3
+F79
+N5
+E4
+F81
+E3
+F6
+R90
+E4
+L90
+W5
+L180
+W2
+S4
+F26
+L90
+E5
+S3
+L90
+F3
+L90
+S2
+R90
+F55
+W5
+S1
+E3
+F63
+L180
+N2
+E2
+L90
+S4
+E3
+F18
+W4
+N2
+W5
+R90
+E5
+L90
+F96
+N3
+F7
+N1
+F87
+R90
+F53
+R90
+W5
+S3
+F94
diff --git a/day12/ship-2.py b/day12/ship-2.py
new file mode 100755 (executable)
index 0000000..a8ebdad
--- /dev/null
@@ -0,0 +1,80 @@
+#!/usr/bin/python3
+
+import sys
+
+waypoint_horz=10
+waypoint_vert=1
+directions=["E", "S", "W", "N"]
+ship_x=0
+ship_y=0
+
+def do_waypoint_move(direction, amount):
+    global waypoint_horz
+    global waypoint_vert
+    if direction in ["E", "W"]:
+        if direction == "W":
+            amount=amount*-1
+        waypoint_horz+=amount
+    elif direction in ["N", "S"]:
+        if direction == "S":
+            amount=amount*-1
+        waypoint_vert+=amount
+
+def turn(amount,multiplier):
+    global waypoint_horz,waypoint_vert
+    steps=int(amount / 90)
+    steps=steps * multiplier
+
+    print("Started at: {},{} rotating {}".format(waypoint_horz,waypoint_vert,amount*multiplier))
+    print("Steps: {}".format(steps))
+    # translate left in to moves right
+    if (steps < 0):
+        steps=4+steps
+
+    if steps == 4 or steps == 0:
+        return
+
+    print("Steps: {}".format(steps))
+
+    # and now we just step round that many steps to the right
+    # now 1,2 will go round to 2,-1, i.e. x,y -> y,-x
+    for step in range(steps):
+        temp=waypoint_vert
+        waypoint_vert=(waypoint_horz*-1)
+        waypoint_horz=temp
+
+    print("Ended at: {},{}".format(waypoint_horz,waypoint_vert))
+
+def do_ship_move(amount):
+    global waypoint_horz,waypoint_vert,ship_x,ship_y
+    print("Waypoint {},{}, Ship {},{}, Amount {}".format(waypoint_horz, waypoint_vert, ship_x, ship_y, amount))
+    ship_x+=(amount*waypoint_horz)
+    ship_y+=(amount*waypoint_vert)
+    print("Ship {},{}".format(ship_x, ship_y))
+
+def turn_right(amount):
+    turn(amount, 1)
+
+def turn_left(amount):
+    turn(amount, -1)
+
+filename="input.txt"
+
+if len(sys.argv) > 1:
+    filename=sys.argv[1]
+
+for line in open(filename, "r"):
+    line=line.rstrip()
+    command=line[0]
+    amount=int(line[1:])
+
+    if command in [ "N", "S", "E", "W" ]:
+        do_waypoint_move(command, amount)
+    elif command == "R":
+        turn_right(amount)
+    elif command == "L":
+        turn_left(amount)
+    elif command == "F":
+        do_ship_move(amount)
+
+print("Moved to {}, {} - MD: {}".format(ship_x,ship_y,(abs(ship_x)+abs(ship_y))))
diff --git a/day12/ship.py b/day12/ship.py
new file mode 100755 (executable)
index 0000000..6219442
--- /dev/null
@@ -0,0 +1,58 @@
+#!/usr/bin/python3
+
+ship_direction="E"
+ship_horz=0
+ship_vert=0
+directions=["E", "S", "W", "N"]
+
+def do_move(direction, amount):
+    global ship_horz
+    global ship_vert
+    if direction in ["E", "W"]:
+        if direction == "W":
+            amount=amount*-1
+        ship_horz+=amount
+    elif direction in ["N", "S"]:
+        if direction == "S":
+            amount=amount*-1
+        ship_vert+=amount
+
+def turn(amount,multiplier):
+    global ship_direction
+    global directions
+
+    dir_pos=0
+    for a,dir2 in enumerate(directions):
+        if dir2 == ship_direction:
+            dir_pos=a
+            break
+
+    print("Loc: {}".format(dir_pos))
+
+    dir_pos+=int((int(amount) / 90) * multiplier)
+    dir_pos=(dir_pos % len(directions))
+    print("New loc: {}".format(dir_pos))
+    print("Started in direction {}, changing to direction {}, after getting told to go {}".format(ship_direction, directions[dir_pos], (int(amount) * multiplier)))
+    ship_direction=directions[dir_pos]
+
+def turn_right(amount):
+    turn(amount, 1)
+
+def turn_left(amount):
+    turn(amount, -1)
+
+for line in open("input.txt", "r"):
+    line=line.rstrip()
+    command=line[0]
+    amount=int(line[1:])
+
+    if command in [ "N", "S", "E", "W" ]:
+        do_move(command, amount)
+    elif command == "R":
+        turn_right(amount)
+    elif command == "L":
+        turn_left(amount)
+    elif command == "F":
+        do_move(ship_direction, amount)
+
+print("Moved to {}, {} - MD: {}".format(ship_horz,ship_vert,(abs(ship_horz)+abs(ship_vert))))
diff --git a/day12/ship.sh b/day12/ship.sh
new file mode 100755 (executable)
index 0000000..413ce66
--- /dev/null
@@ -0,0 +1,190 @@
+#!/bin/bash
+
+filename=${1:-17E_8S_25.txt}
+
+declare -a possible_directions=("E" "S" "W" "N")
+
+read_file() {
+    local filename="${1}"
+    unset instructions
+    declare -g -a instructions
+    exec 3<"$filename"
+    while read -u 3 line; do
+        instructions+=( $line )
+    done
+}
+
+read_file "$filename"
+
+ship_direction="E"
+
+ship_east=0
+ship_north=0
+
+go_north() {
+    amount=$1
+    ship_north=$((ship_north+$amount))
+}
+
+go_south() {
+    amount=$1
+    go_north $((amount*-1))
+}
+
+go_east() {
+    amount=$1
+    ship_east=$((ship_east+$amount))
+}
+go_west() {
+    amount=$1
+    go_east $((amount*-1))
+}
+
+do_move() {
+    direction=$1
+    amount=$2
+
+    case $direction in
+        N)
+            go_north $count
+            ;;
+        S)
+            go_south $count
+            ;;
+        E)
+            go_east $count
+            ;;
+        W)
+            go_west $count
+            ;;
+    esac
+}
+
+do_turn() {
+    local amount=$1
+    local mult=$2
+    local dir_pos
+    local a
+
+    # well, we want amount to be done
+    # in 90° blocks, so
+    amount=$((amount / 90))
+    amount=$((amount*mult))
+
+    dir_pos=0
+
+    # find where current direction is in the array
+    for a in ${!possible_directions[@]}; do
+        if [ ${possible_directions[$a]} == $ship_direction ]; then
+            dir_pos=$a
+            break
+        fi
+    done
+
+    dir_pos=$((dir_pos+$amount))
+    dir_pos=$((dir_pos % ${#possible_directions[@]}))
+    ship_direction=${possible_directions[$dir_pos]}
+}
+
+do_waypoint_move() {
+    direction=$1
+    amount=$2
+
+    case $direction in
+        N)
+            waypoint_north=$((waypoint_north+$amount))
+            ;;
+        S)
+            waypoint_north=$((waypoint_north-$amount))
+            ;;
+        E)
+            waypoint_east=$((waypoint_east+$amount))
+            ;;
+        W)
+            waypoint_east=$((waypoint_east-$amount))
+            ;;
+    esac
+}
+
+do_waypoint_turn() {
+    local degrees=$1
+    local steps=$((degrees/90))
+    local temp
+
+    if [ $steps -lt 0 ]; then
+        steps=4+$steps
+    fi
+
+    for (( a=0; a<steps; a++ )); do
+        temp=$waypoint_north
+        waypoint_north=$((waypoint_east*-1))
+        waypoint_east=$temp
+    done
+}
+
+do_move_ship_to_waypoint() {
+    local times=$1
+    ship_east=$((ship_east+($times*$waypoint_east)))
+    ship_north=$((ship_north+($times*$waypoint_north)))
+}
+
+echo "Part 1:"
+
+for instruction in "${instructions[@]}"; do
+    direction=${instruction:0:1}
+    count=${instruction:1}
+
+    case $direction in
+        E|S|N|W)
+            do_move $direction $count
+            ;;
+        F)
+            do_move $ship_direction $count
+            ;;
+        L)
+            do_turn $count -1
+            ;;
+        R)
+            do_turn $count 1
+            ;;
+    esac
+done
+
+echo "  Ship ended at $ship_east E, $ship_north N"
+e=${ship_east#-}
+n=${ship_north#-}
+md=$((e+$n))
+echo "  Manhatten distance: $md"
+
+echo "Part 2:"
+
+waypoint_east=10
+waypoint_north=1
+ship_east=0
+ship_north=0
+
+for instruction in "${instructions[@]}"; do
+    direction=${instruction:0:1}
+    count=${instruction:1}
+
+    case $direction in
+        E|S|N|W)
+            do_waypoint_move $direction $count
+            ;;
+        F)
+            do_move_ship_to_waypoint $count
+            ;;
+        L)
+            do_waypoint_turn $((count*-1))
+            ;;
+        R)
+            do_waypoint_turn $((count))
+            ;;
+    esac
+done
+
+echo "  Ship ended at $ship_east E, $ship_north N"
+e=${ship_east#-}
+n=${ship_north#-}
+md=$((e+$n))
+echo "  Manhatten distance: $md"
diff --git a/day12/summary.txt b/day12/summary.txt
new file mode 100644 (file)
index 0000000..21cb660
--- /dev/null
@@ -0,0 +1,70 @@
+--- Day 12: Rain Risk ---
+Your ferry made decent progress toward the island, but the storm came in faster than anyone expected. The ferry needs to take evasive actions!
+
+Unfortunately, the ship's navigation computer seems to be malfunctioning; rather than giving a route directly to safety, it produced extremely circuitous instructions. When the captain uses the PA system to ask if anyone can help, you quickly volunteer.
+
+The navigation instructions (your puzzle input) consists of a sequence of single-character actions paired with integer input values. After staring at them for a few minutes, you work out what they probably mean:
+
+Action N means to move north by the given value.
+Action S means to move south by the given value.
+Action E means to move east by the given value.
+Action W means to move west by the given value.
+Action L means to turn left the given number of degrees.
+Action R means to turn right the given number of degrees.
+Action F means to move forward by the given value in the direction the ship is currently facing.
+The ship starts by facing east. Only the L and R actions change the direction the ship is facing. (That is, if the ship is facing east and the next instruction is N10, the ship would move north 10 units, but would still move east if the following action were F.)
+
+For example:
+
+F10
+N3
+F7
+R90
+F11
+These instructions would be handled as follows:
+
+F10 would move the ship 10 units east (because the ship starts by facing east) to east 10, north 0.
+N3 would move the ship 3 units north to east 10, north 3.
+F7 would move the ship another 7 units east (because the ship is still facing east) to east 17, north 3.
+R90 would cause the ship to turn right by 90 degrees and face south; it remains at east 17, north 3.
+F11 would move the ship 11 units south to east 17, south 8.
+At the end of these instructions, the ship's Manhattan distance (sum of the absolute values of its east/west position and its north/south position) from its starting position is 17 + 8 = 25.
+
+Figure out where the navigation instructions lead. What is the Manhattan distance between that location and the ship's starting position?
+
+Your puzzle answer was 882.
+
+--- Part Two ---
+Before you can give the destination to the captain, you realize that the actual action meanings were printed on the back of the instructions the whole time.
+
+Almost all of the actions indicate how to move a waypoint which is relative to the ship's position:
+
+Action N means to move the waypoint north by the given value.
+Action S means to move the waypoint south by the given value.
+Action E means to move the waypoint east by the given value.
+Action W means to move the waypoint west by the given value.
+Action L means to rotate the waypoint around the ship left (counter-clockwise) the given number of degrees.
+Action R means to rotate the waypoint around the ship right (clockwise) the given number of degrees.
+Action F means to move forward to the waypoint a number of times equal to the given value.
+The waypoint starts 10 units east and 1 unit north relative to the ship. The waypoint is relative to the ship; that is, if the ship moves, the waypoint moves with it.
+
+For example, using the same instructions as above:
+
+F10 moves the ship to the waypoint 10 times (a total of 100 units east and 10 units north), leaving the ship at east 100, north 10. The waypoint stays 10 units east and 1 unit north of the ship.
+N3 moves the waypoint 3 units north to 10 units east and 4 units north of the ship. The ship remains at east 100, north 10.
+F7 moves the ship to the waypoint 7 times (a total of 70 units east and 28 units north), leaving the ship at east 170, north 38. The waypoint stays 10 units east and 4 units north of the ship.
+R90 rotates the waypoint around the ship clockwise 90 degrees, moving it to 4 units east and 10 units south of the ship. The ship remains at east 170, north 38.
+F11 moves the ship to the waypoint 11 times (a total of 44 units east and 110 units south), leaving the ship at east 214, south 72. The waypoint stays 4 units east and 10 units south of the ship.
+After these operations, the ship's Manhattan distance from its starting position is 214 + 72 = 286.
+
+Figure out where the navigation instructions actually lead. What is the Manhattan distance between that location and the ship's starting position?
+
+Your puzzle answer was 28885.
+
+Both parts of this puzzle are complete! They provide two gold stars: **
+
+At this point, you should return to your Advent calendar and try another puzzle.
+
+If you still want to see it, you can get your puzzle input.
+
+You can also [Share] this puzzle.