-
Notifications
You must be signed in to change notification settings - Fork 0
/
part1.sh
executable file
·103 lines (96 loc) · 2.14 KB
/
part1.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#!/bin/bash
file="./input.txt";
# holds all positions where the tail was
index=();
posHx=0;
posHy=0;
posTx=0;
posTy=0;
function isNotTouching {
for (( x = posTx - 1; x < posTx + 2; x++ )); do
for (( y = posTy - 1; y < posTy + 2; y++ )); do
if (( x == posHx && y == posHy )); then
return 1;
fi
done
done
return 0;
}
function addTPos {
if [[ ! " ${index[*]} " =~ " $posTx,$posTy " ]]; then
index+=("$posTx,$posTy");
fi
}
addTPos;
prevDir=N;
while read -r line; do
if [ -z "$line" ]; then
continue;
fi
direction=${line:0:1};
steps=${line:2};
case $direction in
U)
for (( step = 0; step < steps; step++ )); do
((posHy++));
if isNotTouching; then
if (( posTx + 1 == posHx )); then
((posTx++));
elif (( posTx - 1 == posHx )); then
((posTx--));
fi
((posTy++));
addTPos;
fi
done;;
D)
for (( step = 0; step < steps; step++ )); do
((posHy--));
if isNotTouching; then
if (( posTx + 1 == posHx )); then
((posTx++));
elif (( posTx - 1 == posHx )); then
((posTx--));
fi
((posTy--));
addTPos;
fi
done;;
#((posHy-=steps));
#if isNotTouching; then
# posTx=$posHx;
# posTy=$((posHy + steps));
#fi
#while isNotTouching; do
# ((posTy--));
# addTPos;
#done;;
R)
for (( step = 0; step < steps; step++ )); do
((posHx++));
if isNotTouching; then
if (( posTy + 1 == posHy )); then
((posTy++));
elif (( posTy - 1 == posHy )); then
((posTy--));
fi
((posTx++));
addTPos;
fi
done;;
L)
for (( step = 0; step < steps; step++ )); do
((posHx--));
if isNotTouching; then
if (( posTy + 1 == posHy )); then
((posTy++));
elif (( posTy - 1 == posHy )); then
((posTy--));
fi
((posTx--));
addTPos;
fi
done;;
esac
done < $file;
echo "Answer: ${#index[@]}";