Last active
December 5, 2019 18:55
-
-
Save rhowe/9fcaeb0b6613b2a507aa78e664a86ce1 to your computer and use it in GitHub Desktop.
AOC2019day3part2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
set -eu | |
set -o pipefail | |
xorigin=1000 | |
yorigin=1000 | |
wires=() | |
while read -r wire; do | |
wires+=("$wire") | |
done | |
wirepositions() { | |
wire=$1 | |
IFS=, read -r -a directions <<<"$wire" | |
x=$xorigin | |
y=$yorigin | |
length=0 | |
move() { | |
distance=$1 | |
deltax=$2 | |
deltay=$3 | |
while [ $((distance--)) -gt 0 ]; do | |
x=$((x+deltax)) | |
y=$((y+deltay)) | |
length=$((length+1)) | |
echo "$length:$x,$y" | |
done | |
} | |
for instruction in "${directions[@]}"; do | |
case $instruction in | |
U*) move "${instruction#U}" 0 1 ;; | |
D*) move "${instruction#D}" 0 -1 ;; | |
L*) move "${instruction#L}" -1 0 ;; | |
R*) move "${instruction#R}" 1 0 ;; | |
esac | |
done | |
} | |
join -j 2 -t : \ | |
<(wirepositions "${wires[0]}" | sort -t : -k 2) \ | |
<(wirepositions "${wires[1]}" | sort -t : -k 2) | \ | |
sed -e 's/.*:\(.*\):\(.*\)/\1 + \2/' \ | |
| bc | sort -n |head -n 1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment