Day 7 2019 - part 1
[advent-of-code-2019.git] / day7 / part1.sh
1 #!/bin/bash
2
3 set -u
4 set -e
5
6 . "$(dirname $(readlink -f "$BASH_SOURCE"))/computer.sh"
7
8 exec 3<input-full.txt
9
10 OLDIFS=$IFS
11 IFS="," read -u 3 -a orig_data
12 IFS=$OLDIFS
13
14 echo "Part 1:"
15
16 run_amplifier() {
17     sequence=($@)
18     output=0
19     for phase in ${sequence[@]}; do
20         output=$(echo -e "$phase\n$output" | run_program orig_data)
21     done
22     echo "$output"
23 }
24
25 values="01234"
26 biggest_output=0
27 biggest_order="01234"
28 for (( a=0; a<${#values}; a++ )); do
29     values_2="${values:0:$a}${values:$((a+1))}"
30     value_1=${values:$a:1}
31     for (( b=0; b<${#values_2}; b++ )); do
32         values_3="${values_2:0:$b}${values_2:$((b+1))}"
33         value_2=${values_2:$b:1}
34         for (( c=0; c<${#values_3}; c++ )); do
35             values_4="${values_3:0:$c}${values_3:$((c+1))}"
36             value_3=${values_3:$c:1}
37             for (( d=0; d<${#values_4}; d++ )); do
38                 value_5="${values_4:0:$d}${values_4:$((d+1))}"
39                 value_4="${values_4:d:1}"
40                 output=$(run_amplifier $value_1 $value_2 $value_3 $value_4 $value_5)
41                 if [ $output -gt $biggest_output ]; then
42                     biggest_output=$output
43                     biggest_order="${value_1}${value_2}${value_3}${value_4}${value_5}"
44                 fi
45             done
46         done
47     done
48 done
49
50 echo "Got biggest output $biggest_output with phases $biggest_order"
51
52 exit 0
53
54 output=0
55 for val in 4 3 2 1 0; do
56     output=$(echo -e "$val\n$output" | run_program orig_data)
57 done
58 echo $output
59