-
Notifications
You must be signed in to change notification settings - Fork 0
/
day2.sh
46 lines (45 loc) · 1.07 KB
/
day2.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#!/bin/bash
IFS=$'\n'
dayNumber=2
dayLabel="DAY${dayNumber}"
scriptDir="$(dirname "$0")"
inputName="input${dayNumber}"
inputArray=($(cat ${scriptDir}/${inputName}))
depth=0
horizontalPosition=0
for line in ${inputArray[@]} ;do
action=${line%% *}
value=${line##* }
case ${action} in
"forward")
horizontalPosition=$((horizontalPosition+value))
;;
"down")
depth=$((depth+value))
;;
"up")
depth=$((depth-value))
;;
esac
done
printf "%s%s\n" "${dayLabel}-puzzle1 --> Results: $((depth*horizontalPosition)) (horizontalPosition=${horizontalPosition}|depth=${depth})"
depth=0
horizontalPosition=0
aim=0
for line in ${inputArray[@]} ;do
action=${line%% *}
value=${line##* }
case ${action} in
"forward")
horizontalPosition=$((horizontalPosition+value))
depth=$((depth+(aim*value)))
;;
"down")
aim=$((aim+value))
;;
"up")
aim=$((aim-value))
;;
esac
done
printf "%s%s\n" "${dayLabel}-puzzle2 --> Results: $((depth*horizontalPosition)) (horizontalPosition=${horizontalPosition}|depth=${depth}|aim=${aim})"