aboutsummaryrefslogtreecommitdiff
path: root/challenge-120/abigail/bash/ch-2.sh
blob: 62540af529e1c3ed4172e6f25ceee537833c0347 (plain)
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
#!/bin/sh

#
# See ../README.md
#

#
# Run as: bash ch-2.sh < input-file
#

set -f

IFS=":"

DIFF_PER_MINUTE=11  # Half degrees
MIN_PER_HOUR=60
FULL_CIRCLE=720     # Half degrees

while read hours minutes
do    #
      # Bash is going to interpret an hour (or minute) of the form "08"
      # or "09" as an illegal octal number. So, we're going to use a
      # trick: we prepend a 1, and subtract 100.
      #
      ((hours   = "1$hours"   - 100))
      ((minutes = "1$minutes" - 100))
      ((angle = (DIFF_PER_MINUTE * (hours * MIN_PER_HOUR + minutes)) %
                                            FULL_CIRCLE))
      if ((2 * angle >= FULL_CIRCLE))
      then ((angle = FULL_CIRCLE - angle))
      fi

      printf "%d" $((angle / 2))
      if ((angle % 2 == 1))
      then printf ".5"
      fi
      echo
done