input = […]
input_set = set(input)
for a, b in itertools.combinations(input, 2):
if 2020 - a - b in input_set:
return a, b, 2020 - a - b
Default
impl can be replaced by #[derive(Default)]
. but maybe you liked the explicitness?#!/bin/bash
# Advent of Code 02
shopt -s lastpipe
p1f=$(grep forward "$1" | cut -d' ' -f2 | tr '\n' '+ ' | rev | cut -c2- | rev | bc)
p1d=$(grep down "$1" | cut -d' ' -f2 | tr '\n' '+ ' | rev | cut -c2- | rev | bc)
p1u=$(grep up "$1" | cut -d' ' -f2 | tr '\n' '+ ' | rev | cut -c2- | rev | bc)
while read -r l; do
ln=$(echo "$l" | cut -d' ' -f2)
lt=$(echo "$l" | cut -d' ' -f1)
if [ "$lt" == "forward" ]; then
p2f="$(( p2f + ln ))"
p2dp="$(( p2dp + (ln * p2a) ))"
elif [ "$lt" == "up" ]; then
p2a="$(( p2a - ln ))"
elif [ "$lt" == "down" ]; then
p2a="$(( p2a + ln ))"
fi
done < "$1"
echo "Part 1:"; echo "$p1f * ($p1d - $p1u)" | bc
echo "Part 2:"; echo "$p2f * $p2dp" | bc
#!/bin/bash
# Advent of Code 02
shopt -s lastpipe
p1f=$(grep forward "$1" | cut -d' ' -f2 | tr '\n' '+ ' | rev | cut -c2- | rev | bc)
p1d=$(grep down "$1" | cut -d' ' -f2 | tr '\n' '+ ' | rev | cut -c2- | rev | bc)
p1u=$(grep up "$1" | cut -d' ' -f2 | tr '\n' '+ ' | rev | cut -c2- | rev | bc)
while read -r l; do
ln=$(echo "$l" | cut -d' ' -f2)
lt=$(echo "$l" | cut -d' ' -f1)
if [ "$lt" == "forward" ]; then
p2f="$(( p2f + ln ))"
p2dp="$(( p2dp + (ln * p2a) ))"
elif [ "$lt" == "up" ]; then
p2a="$(( p2a - ln ))"
elif [ "$lt" == "down" ]; then
p2a="$(( p2a + ln ))"
fi
done < "$1"
echo "Part 1:"; echo "$p1f * ($p1d - $p1u)" | bc
echo "Part 2:"; echo "$p2f * $p2dp" | bc
while (cargoManifestOnYearResultSet.next()) {