-
Notifications
You must be signed in to change notification settings - Fork 0
/
spiral-dist.sh
executable file
·69 lines (55 loc) · 1.19 KB
/
spiral-dist.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
#!/bin/bash
while read NUMBER; do
RADIUS=1
SQUARE=1
D_X=0
D_Y=0
# Find out which square are we in
while [ $SQUARE -lt $NUMBER ]; do
((D_X = D_X + 1))
((D_Y = D_Y + 1))
((RADIUS = RADIUS + 2))
((SQUARE = RADIUS * RADIUS))
done
# Go left until number matches
STEP=1
while [ $SQUARE -ne $NUMBER ] && [ $STEP -lt $RADIUS ]; do
((SQUARE = SQUARE - 1))
((D_X = D_X - 1))
((STEP = STEP + 1))
done
# Go up until number matches
STEP=1
while [ $SQUARE -ne $NUMBER ] && [ $STEP -lt $RADIUS ]; do
((SQUARE = SQUARE - 1))
((D_Y = D_Y - 1))
((STEP = STEP + 1))
done
# Go right until number matches
STEP=1
while [ $SQUARE -ne $NUMBER ] && [ $STEP -lt $RADIUS ]; do
((SQUARE = SQUARE - 1))
((D_X = D_X + 1))
((STEP = STEP + 1))
done
# Go down until number matches
STEP=1
while [ $SQUARE -ne $NUMBER ] && [ $STEP -lt $RADIUS ]; do
((SQUARE = SQUARE - 1))
((D_Y = D_Y + 1))
((STEP = STEP + 1))
done
# Calculate distance
D_SUM=0
if [ $D_X -gt 0 ]; then
((D_SUM = D_SUM + D_X))
else
((D_SUM = D_SUM - D_X))
fi
if [ $D_Y -gt 0 ]; then
((D_SUM = D_SUM + D_Y))
else
((D_SUM = D_SUM - D_Y))
fi
echo "> $NUMBER: $D_X:$D_Y = $D_SUM"
done