Round 1B’s problem A, “Broken Clock”, gives us a clock’s position and asks us for the time.

Catch number 1: it doesn’t tell us which hand is which. Countermeasure number 1: there are only 3 hands, that’s 6 permutations. We can try them all and keep the consistent ones only.

Catch number 2: the clock has no markings, so all hand positions are given with an arbitrary offset. Countermeasure 2: that offset is eliminated by pairwise subtraction.

So let’s get the math out. The clock positions are given in nanoseconds, \(360×12×10^{10}\) per revolution. For obvious reasons, I’ll call that constant \(2\pi\). For a time \(t\) in nanoseconds and hand positions \(h\), \(m\) and \(s\), we’d have:

\[ \left\{ \begin{array}{cclc} h & = & \theta + t & [\bmod 2\pi] \\ m & = & \theta + 12t & [\bmod 2\pi] \\ s & = & \theta + 720t & [\bmod 2\pi] \end{array} \right. \]

Subtracting pairwise,

\[ \left\{ \begin{array}{cclc} m - h & = & 11t & [\bmod 2\pi] \\ s - m & = & 708t & [\bmod 2\pi] \\ s - h & = & 719t & [\bmod 2\pi] \end{array} \right. \]

Three equations, four unknowns. But \(t\)’s range is bounded, so there’s probably a way to get a MIP solver on the task. Luckily for us, that won’t be needed.

The variables are all integers.^{1} So we’re really looking at a modular inverse. The 708 may be a bit of a composite, but 11 and 719 are *bona-fide* primes, we can invert them without a second thought using any conventional means: product enumeration, exponentiation, WolframAlpha^{2}, extended Euclid/Bézout.^{3}

I suppose 128-bit integers would be ok, but they’re a bit of a pain to get working in Haskell. Especially when we’ve got BigInt baked right in.

```
eleven_ :: Integer
twoPi,= 360 * 12 * 10^10
twoPi = 15709090909091 eleven_
```

Now given \(h\), \(m\) and \(s\), we can obtain \(t\) directy from just a single difference (the hours-minutes one), there is always one that fits. Then we can trivially verify the seconds hand is indeed in the correct position relative to the others for that time. Any equation involving \(s\) and \(t\) will do, though testing both for acute decision fatigue also works.

```
verify :: [Integer] -> Maybe Integer
= do
verify [h,m,s] let t = (m - h) * eleven_ `mod` twoPi
$ ((720-1) * t - (s - h)) `mod` twoPi == 0
guard $ ((720-12) * t - (s - m)) `mod` twoPi == 0
guard pure t
```

Solving from \(A\), \(B\), \(C\) is a simple matter of trying the permutations until a fit is found. The judge accepts any solution if there are multiple.^{4}

```
solve :: Integer -> Integer -> Integer -> (Integer,Integer,Integer,Integer)
= (h,m,s,n) where
solve a b c = t `divMod` (10^9)
(t',n) = t' `divMod` 60
(t'',s) = t'' `divMod` 60
(h,m) = head $ mapMaybe verify $ permutations [a,b,c] t
```

Here’s a fairly standard CodeJam wrapper for completeness.

```
main :: IO ()
= do
main <- readLn
t 1..t] $ \i -> do
forM_ [<- map read . words <$> getLine
[a,b,c] let (h,m,s,n) = solve a b c
putStrLn $ "Case #" ++ show i ++ ": " ++ unwords (map show [h,m,s,n])
```

This concludes this puzzle’s solution. As usual, the full code is on GitHub. See you soon for the round’s other problems!