diff --git a/2021/day02.sh b/2021/day02.sh new file mode 100755 index 0000000..7cab5c0 --- /dev/null +++ b/2021/day02.sh @@ -0,0 +1,50 @@ +#!/bin/bash +move() +{ + if [[ $1 == "forward" ]]; then + pos_x=$((pos_x + $2)) + elif [[ $1 == "up" ]]; then + pos_y=$((pos_y - $2)) + else + pos_y=$((pos_y + $2)) + fi; +} + +move2() +{ + if [[ $1 == "down" ]]; then + aim=$((aim + $2)) + elif [[ $1 == "up" ]]; then + aim=$((aim - $2)) + else + pos_x=$((pos_x + $2)) + pos_y=$((pos_y + $2*aim)) + fi; +} + +part1() +{ + pos_x=0 + pos_y=0 + aim=0 + while read -r action valeur + do + move "$action" "$valeur" + done < "inputs/day02.txt" + echo "($pos_x, $pos_y): $((pos_x * pos_y))" +} + +part2() +{ + pos_x=0 + pos_y=0 + aim=0 + while read -r action valeur + do + move2 "$action" "$valeur" + done < "inputs/day02.txt" + echo "($pos_x, $pos_y): $((pos_x * pos_y))" +} + +echo "Partie 1: $(part1)" +echo "Partie 2: $(part2)" diff --git a/2021/inputs/day02.txt b/2021/inputs/day02.txt new file mode 100644 index 0000000..d8ca007 --- /dev/null +++ b/2021/inputs/day02.txt @@ -0,0 +1,1000 @@ +forward 8 +forward 1 +down 9 +down 8 +down 5 +forward 4 +up 9 +forward 7 +forward 2 +down 6 +forward 7 +forward 5 +forward 8 +down 7 +up 8 +forward 1 +forward 6 +down 8 +up 3 +forward 3 +down 3 +down 7 +forward 5 +down 1 +down 9 +forward 4 +up 7 +forward 9 +forward 1 +down 6 +down 5 +down 4 +forward 9 +forward 1 +forward 4 +up 3 +up 6 +forward 1 +forward 9 +forward 4 +down 1 +down 9 +forward 7 +forward 8 +down 8 +down 8 +down 4 +down 5 +up 3 +forward 4 +down 1 +forward 2 +forward 1 +forward 8 +up 2 +forward 5 +forward 4 +forward 5 +forward 8 +down 4 +down 5 +down 8 +down 7 +down 6 +forward 4 +up 8 +forward 6 +down 2 +down 7 +up 7 +down 2 +down 6 +forward 5 +forward 2 +forward 8 +down 5 +down 6 +down 6 +up 7 +down 7 +forward 8 +down 7 +down 7 +down 2 +down 2 +forward 3 +up 5 +down 3 +up 3 +down 4 +up 9 +down 1 +up 1 +forward 3 +up 3 +forward 9 +up 6 +down 3 +down 6 +forward 4 +forward 5 +forward 8 +down 6 +down 9 +down 4 +down 5 +forward 7 +down 3 +forward 8 +forward 3 +up 9 +forward 5 +forward 8 +forward 1 +down 4 +up 2 +forward 1 +down 1 +forward 5 +down 8 +down 2 +forward 3 +forward 2 +forward 1 +down 7 +forward 1 +down 4 +forward 7 +forward 5 +down 8 +down 2 +up 4 +up 8 +forward 6 +up 2 +down 8 +up 9 +forward 3 +up 5 +down 3 +down 5 +up 6 +forward 3 +up 9 +down 4 +forward 8 +up 3 +forward 6 +forward 7 +forward 8 +forward 6 +down 9 +forward 4 +up 8 +forward 3 +forward 3 +forward 6 +down 3 +forward 5 +down 4 +down 4 +up 4 +down 8 +down 4 +down 1 +down 7 +down 7 +up 9 +down 7 +forward 2 +down 1 +down 8 +forward 8 +forward 4 +forward 6 +forward 8 +forward 6 +forward 8 +forward 7 +forward 6 +up 7 +down 6 +down 9 +down 2 +down 8 +down 4 +up 6 +up 8 +forward 3 +down 7 +down 3 +forward 3 +forward 3 +down 4 +down 1 +up 3 +forward 3 +down 5 +up 6 +down 6 +forward 1 +down 3 +down 6 +down 9 +down 3 +forward 8 +down 1 +forward 7 +forward 1 +forward 3 +up 3 +down 8 +forward 4 +down 4 +down 4 +down 8 +down 5 +down 8 +forward 6 +forward 7 +down 9 +forward 2 +up 9 +down 5 +forward 2 +down 5 +down 8 +forward 1 +forward 6 +forward 2 +forward 6 +down 7 +up 7 +down 7 +forward 7 +up 8 +forward 9 +down 5 +up 6 +up 6 +forward 3 +down 5 +forward 1 +down 4 +down 4 +down 1 +up 6 +down 8 +down 9 +down 2 +up 2 +up 7 +forward 5 +down 5 +up 2 +forward 2 +up 1 +forward 2 +up 4 +up 7 +forward 2 +down 4 +down 1 +forward 4 +up 6 +forward 1 +up 7 +down 8 +down 5 +down 1 +up 7 +down 5 +forward 4 +forward 2 +down 2 +down 6 +forward 1 +forward 2 +up 6 +down 1 +forward 2 +down 5 +down 9 +down 6 +up 8 +forward 3 +down 6 +up 3 +down 8 +down 8 +forward 2 +forward 1 +forward 7 +forward 8 +forward 2 +forward 5 +down 6 +down 3 +forward 5 +up 8 +forward 6 +up 9 +forward 7 +up 3 +down 7 +down 3 +up 8 +down 5 +forward 3 +down 1 +forward 1 +forward 2 +down 7 +down 6 +down 9 +forward 3 +down 7 +down 7 +down 5 +forward 4 +down 4 +forward 5 +forward 1 +forward 4 +down 8 +up 4 +down 6 +down 8 +up 1 +forward 7 +up 3 +forward 6 +forward 8 +forward 9 +forward 2 +up 6 +forward 7 +forward 3 +up 3 +up 6 +down 8 +up 4 +forward 9 +down 9 +up 3 +up 3 +down 4 +up 3 +down 2 +down 4 +up 9 +down 1 +forward 6 +forward 3 +down 3 +forward 4 +forward 3 +forward 8 +forward 1 +down 1 +up 4 +up 9 +down 8 +forward 3 +forward 2 +down 4 +down 1 +forward 8 +down 8 +up 8 +up 7 +down 4 +forward 6 +up 5 +down 1 +down 3 +down 9 +forward 3 +forward 9 +down 1 +down 6 +forward 8 +up 1 +down 9 +down 3 +forward 5 +forward 9 +forward 8 +up 7 +down 2 +down 5 +forward 6 +down 9 +down 4 +down 7 +down 4 +forward 7 +down 8 +up 8 +forward 2 +up 3 +down 4 +forward 2 +forward 4 +up 4 +forward 4 +forward 9 +down 9 +up 1 +forward 1 +down 8 +up 7 +up 9 +down 8 +down 7 +forward 9 +forward 1 +up 3 +forward 8 +forward 6 +forward 5 +up 2 +forward 8 +up 7 +down 8 +down 2 +up 9 +down 7 +forward 5 +forward 4 +down 9 +forward 4 +forward 5 +up 1 +down 8 +down 8 +down 4 +forward 3 +down 1 +up 7 +down 8 +forward 4 +down 5 +forward 1 +down 9 +forward 9 +up 7 +up 1 +down 3 +forward 3 +down 8 +forward 9 +down 3 +up 7 +forward 1 +up 8 +up 2 +up 1 +forward 8 +down 3 +forward 5 +up 3 +forward 6 +forward 9 +forward 2 +forward 6 +down 5 +forward 2 +down 7 +up 6 +forward 8 +forward 6 +down 3 +forward 8 +down 1 +forward 2 +down 9 +up 5 +down 6 +up 4 +forward 6 +forward 5 +forward 2 +forward 9 +forward 8 +up 4 +forward 4 +forward 9 +forward 4 +forward 7 +up 6 +down 5 +forward 8 +down 8 +up 3 +down 4 +up 8 +up 5 +forward 3 +down 8 +down 9 +down 1 +forward 7 +down 8 +up 4 +down 8 +up 4 +down 5 +forward 1 +forward 3 +down 6 +down 5 +forward 4 +up 2 +down 7 +down 3 +up 9 +forward 5 +forward 3 +forward 2 +up 2 +down 9 +up 1 +down 2 +forward 3 +forward 8 +forward 5 +down 2 +down 2 +up 3 +down 8 +forward 4 +forward 6 +forward 5 +up 9 +down 7 +up 7 +up 2 +down 2 +up 4 +forward 2 +forward 2 +down 7 +forward 5 +down 2 +up 7 +forward 6 +forward 5 +down 1 +forward 8 +forward 3 +forward 2 +up 3 +down 1 +forward 6 +forward 5 +forward 9 +down 5 +up 3 +down 1 +down 5 +forward 1 +down 1 +down 1 +forward 6 +up 8 +forward 9 +up 5 +up 5 +forward 9 +forward 3 +forward 4 +down 8 +forward 9 +forward 9 +up 2 +down 6 +down 2 +forward 1 +down 7 +up 4 +forward 3 +down 6 +forward 5 +down 6 +up 5 +forward 6 +up 6 +forward 7 +forward 5 +forward 1 +down 9 +forward 7 +forward 4 +forward 1 +forward 6 +up 7 +forward 6 +down 3 +forward 4 +down 1 +down 9 +down 8 +forward 7 +down 7 +up 5 +forward 1 +forward 4 +forward 8 +forward 6 +forward 2 +forward 4 +forward 2 +forward 6 +forward 5 +down 4 +down 3 +forward 6 +forward 3 +up 6 +down 3 +down 2 +forward 1 +down 9 +up 1 +forward 8 +forward 1 +forward 1 +forward 4 +down 5 +forward 7 +forward 5 +up 5 +up 7 +forward 7 +forward 5 +down 3 +down 2 +down 7 +forward 7 +forward 4 +down 9 +forward 2 +down 6 +forward 3 +up 6 +down 8 +down 7 +down 6 +down 7 +up 5 +up 9 +down 4 +up 6 +forward 1 +forward 2 +forward 4 +up 5 +down 2 +down 3 +forward 9 +down 9 +down 3 +forward 5 +forward 3 +down 1 +up 7 +forward 7 +forward 5 +down 8 +up 6 +down 7 +forward 5 +up 7 +up 6 +forward 2 +forward 6 +down 1 +up 8 +down 9 +down 1 +forward 9 +up 9 +down 2 +forward 6 +up 3 +down 1 +up 9 +down 6 +up 2 +up 6 +forward 9 +forward 8 +up 7 +forward 6 +up 3 +up 4 +down 6 +forward 1 +forward 9 +down 9 +down 6 +forward 1 +forward 2 +up 3 +down 4 +down 9 +down 3 +down 2 +forward 7 +down 9 +forward 9 +forward 5 +down 9 +forward 1 +forward 6 +down 6 +up 8 +up 8 +up 3 +forward 7 +forward 7 +forward 8 +down 5 +forward 3 +down 4 +down 6 +forward 1 +down 1 +forward 7 +forward 2 +down 7 +down 1 +up 6 +forward 3 +down 6 +forward 2 +down 6 +forward 8 +down 4 +up 8 +forward 9 +up 5 +forward 3 +down 6 +forward 4 +forward 4 +down 8 +up 5 +up 9 +down 8 +forward 1 +down 6 +down 3 +forward 1 +down 4 +up 6 +down 6 +down 6 +down 5 +forward 5 +forward 6 +down 1 +down 9 +down 4 +up 7 +down 2 +forward 5 +down 8 +down 8 +down 6 +down 1 +up 8 +down 1 +down 4 +forward 6 +down 3 +down 6 +up 4 +down 6 +up 7 +down 3 +forward 2 +up 3 +forward 7 +up 7 +up 4 +forward 5 +down 9 +down 2 +down 9 +up 6 +forward 9 +forward 3 +down 1 +forward 7 +forward 2 +down 5 +forward 3 +forward 7 +forward 4 +forward 7 +forward 6 +forward 3 +down 6 +forward 3 +down 1 +up 6 +up 9 +down 1 +forward 1 +down 1 +up 7 +forward 8 +forward 4 +up 4 +forward 7 +down 3 +up 8 +up 1 +down 6 +forward 8 +forward 7 +up 7 +down 4 +down 4 +down 8 +up 3 +forward 5 +down 4 +forward 9 +forward 6 +forward 6 +forward 7 +down 6 +down 3 +forward 2 +up 8 +forward 1 +down 9 +up 7 +down 3 +down 3 +up 5 +up 4 +up 8 +forward 2 +down 5 +forward 7 +up 8 +up 6 +forward 5 +down 5 +forward 2 +down 6 +forward 7 +down 6 +forward 5 +forward 3 +forward 9 +down 7 +forward 7 +forward 7 +forward 7 +forward 1 +forward 2 +forward 2 +down 6 +down 8 +forward 3 +down 9 +down 5 +up 5 +forward 3 +forward 9 +down 7 +down 9 +down 9 +down 9 +forward 6 +down 1 +forward 6 +forward 8 +forward 8 +down 4 +forward 8 +down 9 +forward 6 +down 2 +down 1 +down 5 +forward 5 +forward 8 +forward 7 +forward 7 +forward 8 +down 5 +forward 3 +up 5 +forward 5 +down 9 +up 8 +down 2 +forward 8 +forward 5 +up 5 +forward 4 +down 3 +forward 4 +up 3 +forward 1 +forward 8 +forward 3 +down 2 +up 8 +forward 2 +down 8 +forward 3 +up 2 +up 1 +down 9 +down 8 +forward 1 +forward 1 +down 8 +forward 9 +up 2 +forward 4 +forward 2 +up 2 +down 8 +down 1 +forward 1 +forward 3 +forward 6 +up 6 +down 2 +up 1 +down 3 +forward 3 +up 5 +down 9 +down 7 +forward 8 +up 5 +up 4 +forward 8 +forward 5 +down 8 +up 8 +forward 7 +up 8 +down 6 +down 2 +down 3 +forward 9 +down 4 +down 2 +down 1 +up 2 +up 4 +forward 3 +forward 1 +forward 9 +forward 1 +down 7 +up 7 +forward 5 +down 3 +forward 3 +down 4 +down 1 +up 5 +down 8 +down 2 +forward 4 +up 9 +down 3 +down 3 +down 2 +forward 7 +down 2 +forward 6 +up 8 +forward 5 +forward 5 +up 9 +forward 8 +forward 9 +down 5 +forward 5 +down 3 +forward 5 +down 1 +forward 6 +forward 6 +forward 1 +forward 2 +up 5 +forward 9