aoc-2022/05.sh

27 lines
776 B
Bash
Executable File

#!/bin/sh
set -euf
# hardcoded input. go ahead, kill me
LINES="GWLJBRTD
CWS
MTZR
VPSHCTD
ZDLTPG
DCQJZRBF
RTFMJDBS
MVTBRHL
VSDPQ
x" # the x is here so that command substitution does not remove trailing newlines
LINES2="$LINES" # part 2
IFS=","
while read -r l; do
set -- $l # 1 = number, 2 = from, 3 = to
# I'm able to save .2 seconds of runtime by replacing all printfs with a here-doc, but that makes this unreadable as fuck
b="$(printf "%s" "$LINES" | sed "${2}q;d" | cut -c -${1})" # p2
c="$(printf "%s" "$b" | rev)" # p1
LINES="$(printf "%s" "$LINES" | sed -E -e "${3}s/^/$b/" -e "${2}s/^.{$1}//")"
LINES2="$(printf "%s" "$LINES2" | sed -E -e "${3}s/^/$c/" -e "${2}s/^.{$1}//")"
done < 05_i2
printf "PART 1\n---\n%s---\nPART 2\n---\n%s" "${LINES%x}" "${LINES2%x}"