From: Brett Parker Date: Sat, 12 Dec 2020 17:42:38 +0000 (+0000) Subject: Day 12 X-Git-Url: https://git.sommitrealweird.co.uk/advent-of-code-2020.git/commitdiff_plain/b00850e41d65e881da505d79350c06f06b64fcba?ds=sidebyside Day 12 --- diff --git a/day12/17E_8S_25.txt b/day12/17E_8S_25.txt new file mode 100644 index 0000000..d382291 --- /dev/null +++ b/day12/17E_8S_25.txt @@ -0,0 +1,5 @@ +F10 +N3 +F7 +R90 +F11 diff --git a/day12/input.txt b/day12/input.txt new file mode 100644 index 0000000..c64cbfd --- /dev/null +++ b/day12/input.txt @@ -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 index 0000000..a8ebdad --- /dev/null +++ b/day12/ship-2.py @@ -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 index 0000000..6219442 --- /dev/null +++ b/day12/ship.py @@ -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 index 0000000..413ce66 --- /dev/null +++ b/day12/ship.sh @@ -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