Skip to content

Instantly share code, notes, and snippets.

@aramperes
Created December 20, 2024 02:28
Show Gist options
  • Save aramperes/d93b38924ae0ccc2b9e518491906fae2 to your computer and use it in GitHub Desktop.
Save aramperes/d93b38924ae0ccc2b9e518491906fae2 to your computer and use it in GitHub Desktop.
Advent of Code 2024 Day 3 - Bash
#!/bin/bash
# cat input.txt | ./solve1.sh
grep -Po 'mul\(\d+,\d+\)' |
sed -r 's/^mul\(([0-9]+),([0-9]+)\)$/\1*\2/' |
xargs |
sed -e 's/\ /+/g' |
xargs -I @@ bash -c 'echo $((@@))'
#!/bin/bash
# cat input.txt | ./solve2.sh
grep -Po "(mul\(\d+,\d+\))|(do\(\))|(don't\(\))" |
tr -d "'" |
xargs |
while read -r orig; do
echo "$orig" |
grep -Po 'dont\(\) .*?do\(\)' |
while read -r rm; do
echo "$rm" |
grep -Po 'mul\(\d+,\d+\)' |
sed -r 's/^mul\(([0-9]+),([0-9]+)\)$/\1*\2/' |
xargs |
sed -e 's/\ /+/g' |
xargs -I @@ bash -c 'echo $((@@))'
done |
paste -sd- - |
while read -r sub; do
echo "$orig" |
grep -Po 'mul\(\d+,\d+\)' |
sed -r 's/^mul\(([0-9]+),([0-9]+)\)$/\1*\2/' |
xargs |
sed -e 's/\ /+/g' |
xargs -I @@ bash -c 'echo $((@@))' |
while read -r sum; do
echo $(("$sum-$sub"))
done
done
done
xmul(2,4)%&mul[3,7]!@^do_not_mul(5,5)+mul(32,64]then(mul(11,8)mul(8,5))
xmul(2,4)&mul[3,7]!^don't()_mul(5,5)+mul(32,64](mul(11,8)undo()?mul(8,5))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment