The Sage and Trelawney
Problem description:
“You must cast your spell,” said the Sage, “at the Golden Hour in Christmas Day.”
“But how shall I know the Golden Hour?” asked Trelawney.
The Sage rolled his eyes and quoted:
“The Golden Hour of the day is the Silver Hour with the day as bronze and the day as copper. When copper is one, the Silver Hour is one no matter what is bronze. When bronze and copper are the same, the Silver Hour is one hour later than the Silver Hour with bronze the same and copper one less. When copper is more than bronze, the Silver Hour is the Golden Hour with bronze as the day. When bronze is more than copper, the Silver Hour is the Silver Hour with copper the same and bronze less copper as bronze, together with the Silver Hour with bronze the same and copper one less.”
“This I know by heart,” said Trelawney, “but there is much I do not understand. What number is the day?”
“The day is the number of days from the start of the year,” said the Sage. “The first of January is day one and Christmas is, therefore, 360 this year. It is 359 in a non-leap year.”
“Then how,” said Trelawney, “can I take two Silver Hours together? If I add seven and nine, for example, I get sixteen. That is not an hour.”
“No!” said the Sage. “The Golden Hour is always a number from one to twelve. Golden spells can be cast only from one to midnight. They are never morning spells. If you calculate a number greater than twelve, reduce it by twelve to get a valid hour.”
“I see,” said Trelawney doubtfully. “The Golden Hour of two is the Silver Hour of two and two. This is one hour later than the Silver Hour of two and one, by the third rule, and that is one by the second rule, so the answer is two.” He picked up the quill and tried to think of a simpler way to write all this.
Eventually he produced the following:
Golden (Day of 3) by Rule 1 is:
Silver (Bronze of 3, Copper of 3) by Rule 3 is:
Silver (Bronze of 3, Copper of 2) + 1 by Rule 5 is:
Silver (Bronze of 3-2, Copper of 2) +
Silver (Bronze of 3, Copper of 1) + 1 by Rule 2 is:
Silver (Bronze of 3-2, Copper of 2) + 1 + 1 by common arithmetic is:
Silver (Bronze of 1, Copper of 2) + 2 by Rule 4 is:
Golden (Day of 1) + 2
“Are you sleeping?” asked the Sage.
“It’s alright,” said Trelawney. “I have calculated the Golden Hours for the first week. I am sure I can finish by Christmas.”
“Let us hope so,” said the Sage.
Task:
By computer program, or otherwise, find the Golden Hour of Christmas Day for normal years and leap years.
My Solution:
You have hopefully realised by now that this is a recursive problem. Unfortunately, since the level of recursion is high, if you try to just write the program recursively it’ll just run itself into the ground pretty quickly, no matter how powerful your computer is.
So, you use a Lookup Table, which stores the values of each recursive iteration. Rather than continually ‘recursing’, on each iteration, the program refers to the correct value in the lookup table.
Download my Java solution here, using a lookup table.
This is my original solution, not using a lookup table. It is theoretically sound but won’t work due to the number of computations required to produce the answer.
From memory, the actual answer is ten, I think.
