Skip to content

Instantly share code, notes, and snippets.

@thexa4
Last active December 11, 2018 00:20
Show Gist options
  • Save thexa4/83d5a3f3f531d218750a67f275b0b99c to your computer and use it in GitHub Desktop.
Save thexa4/83d5a3f3f531d218750a67f275b0b99c to your computer and use it in GitHub Desktop.
Advent calendar 2018
#!/bin/bash
(printf "puts "; cat) | tr -d "\n" | ruby
#!/bin/bash
set -euo pipefail
#set -x
sum=0
seenfile="$(mktemp)"
echo $seenfile
echo 0 > "$seenfile"
while true; do
cat "$1"
done | tr -d '+' | while read -r number; do
sum=$(( $sum + $number ));
#echo -e "$number\t$sum"
if grep -q -E "^$sum\$" "$seenfile"; then
echo $sum
exit 0
fi
echo "$sum" >> "$seenfile"
done | head -n 1
#!/bin/bash
while read -r serial; do
sed 's/\(.\)/\1\n/g' <<<"$serial" | sort | uniq -c | grep -E ' [23] ' | cut -d' ' -f7 | sort | uniq
done | sort | uniq -c | sed -E 's/\s+([0-9]+).*/\1/g' | sed '1 s/$/ */' | tr -d "\n" | bc -i
#!/bin/bash
for i in {1..26}; do
cut -c"$(seq 1 26 | grep -vE "^$i\$" | perl -pe 'chomp if eof' | tr "\n" ",")" <"$1" | sort | uniq -c | grep ' 2 '
done
#!/bin/bash
tmpfile="$(mktemp)"
cut -d' ' -f3,4 | tr -d ':' | cut --output-delimiter=' ' -d, -f1,2 | cut --output-delimiter=' ' -dx -f1,2 | awk '{ print $1,$2,$1+$3-1,$2+$4-1 }' >"$tmpfile"
echo "$tmpfile"
size=$(cut -d' ' --output-delimiter='@' -f3,4 <"$tmpfile" | tr '@' "\n" | sort -nr | head -n1)
magic="convert -size ${size}x${size} xc:black -fill rgba\(255,255,255,0.01\)"
while read -r x1 y1 x2 y2; do
magic="$magic -draw rectangle\ ${x1},${y1}\ ${x2},${y2}"
done <"$tmpfile"
magic="$magic txt:"
echo "$magic" | bash | grep -v 'pixel enumeration' | grep -v black | grep -v 028F028F028FFFFF | wc -l
#!/bin/bash
tempfile=$(mktemp)
cat >"$tempfile"
for i in $(seq 1 $(wc -l "$tempfile" | cut -d' ' -f1 )); do
echo "echo $i; awk \"!(NR==$i)\" $tempfile | ./day3a 2>/dev/null | tail -n1"
done | parallel --bar --jobs 8 | tee day3b-results
#!/bin/bash
tmpfile=$(mktemp)
guard="none"
sleeptime="bad"
sort | while read -r year time noun id rest; do
if [ "$noun" = "Guard" ]; then
guard="$id"
fi
if [ "$noun" = "falls" ]; then
sleeptime="$(cut -d':' -f2 <<<"$time" | tr -d ']')"
fi
if [ "$noun" = "wakes" ]; then
waketime="$(cut -d':' -f2 <<<"$time" | tr -d ']')"
total=$(( 10#$waketime - 10#$sleeptime ))
echo $guard $total $sleeptime $waketime
fi
done >"$tmpfile"
guardid=$(awk '{a[$1]+=$2}END{for(i in a) print a[i], i}' "$tmpfile" | sort -n | tail -n1 | cut -d' ' -f2 | tr -d '#')
timesfile="$(mktemp)"
echo "$tmpfile"
grep "#$guardid " "$tmpfile" | while read -r guardid length start end; do
seq $start $(( $end - 1 ))
done >"$timesfile"
echo $timesfile
minutes=$(sort $timesfile | uniq -c | sort -n | tail -n1 | xargs | cut -d' ' -f2)
set -x
echo "$minutes * $guardid" | bc -i
#!/bin/bash
tmpfile=$(mktemp)
guard="none"
sleeptime="bad"
sort | while read -r year time noun id rest; do
if [ "$noun" = "Guard" ]; then
guard="$id"
fi
if [ "$noun" = "falls" ]; then
sleeptime="$(cut -d':' -f2 <<<"$time" | tr -d ']')"
fi
if [ "$noun" = "wakes" ]; then
waketime="$(cut -d':' -f2 <<<"$time" | tr -d ']')"
total=$(( 10#$waketime - 10#$sleeptime ))
echo $guard $total $sleeptime $waketime
fi
done >"$tmpfile"
echo "$tmpfile"
while read -r guardid length start end; do
seq $start $(( 10#$end - 1 )) | sed -e "s/^/$guardid /g"
done <"$tmpfile" | sort | uniq -c | sort -n | tail -n1 | sed -E 's/.*#([0-9]+) ([0-9]+).*/\1 * \2/' | bc -i
#!/bin/bash
sed="sed -Ee s/"
for match in aA Aa bB Bb cC Cc dD Dd eE Ee fF Ff gG Gg hH Hh iI Ii jJ Jj kK Kk lL Ll mM Mm nN Nn oO Oo pP Pp qQ Qq rR Rr sS Ss tT Tt uU Uu vV Vv wW Ww xX Xx yY Yy zZ Zz; do
sed="${sed}$match|"
done
sed="${sed}213412341234//g"
prev="unmatch"
read current
while [ "$prev" != "$current" ]; do
prev="$current"
current="$($sed <<<"$prev")"
done
echo "$current" |perl -pe 'chomp' | wc -c
#!/bin/bash
for element in a\\\|A b\\\|B c\\\|C d\\\|D e\\\|E f\\\|F g\\\|G h\\\|H i\\\|I j\\\|J k\\\|K l\\\|L m\\\|M n\\\|N o\\\|O p\\\|P q\\\|Q r\\\|R s\\\|S t\\\|T u\\\|U v\\\|V w\\\|W x\\\|X y\\\|Y z\\\|Z; do
echo "sed -Ee s/$element//g $1 | ./day5a"
done | parallel | sort -n | head -n1
#!/bin/bash
input="$1"
tmpdir="$(mktemp)"
function pixel {
x=$1
y=$2
nl -v 10 "$input" | tr -d ',' | while read -r id xp yp; do
dx=$(( $xp - $x ))
dy=$(( $yp - $y ))
delta=$(( ${dx#-} + ${dy#-} ))
echo $delta $id
done | sort -n >"$tmpdir"
lowest=$(head -n1 "$tmpdir" | cut -d' ' -f1)
multiple=$(( $(grep -cE "^$lowest " "$tmpdir") - 1 ))
if [ $multiple = "0" ]; then
echo "=$(head -n1 "$tmpdir" | cut -d' ' -f2)="
#echo $lowest
else
echo '=..='
fi
}
maxx=$(( $(cut -d',' -f1 "$input" | sort -n | tail -n1) ))
maxy=$(( $(cut -d',' -f2 "$input" | sort -n | tail -n1 | xargs) ))
minx=$(( $(cut -d',' -f1 "$input" | sort -n | head -n1) ))
miny=$(( $(cut -d',' -f2 "$input" | sort -n | head -n1 | xargs) ))
edge="$(mktemp)"
{
for x in $( seq $minx $maxx ); do
pixel $x $miny
pixel $x $maxy
done
for y in $( seq $miny $maxy ); do
pixel $minx $y
pixel $maxx $y
done
echo '=..='
} | sort | uniq >"$edge"
echo $edge
for y in $( seq $miny $maxy ); do
echo "$(( $y - $miny )) / $(( $maxy - $miny ))" 1>&2
for x in $( seq $minx $maxx ); do
pixel $x $y
done
done | tee debug | grep -vFf "$edge" | sort | uniq -c | sort -n | tail -n1 | xargs | cut -d' ' -f1 | tr -d '='
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment