patternjavaModerate
PUT (Parallel Universe Time) generator
Viewed 0 times
puttimegeneratoruniverseparallel
Problem
I don't like to post programming contest code in here, but I really like the question and want to share it with you all.
It has been years since Superman started his enmity with the
super-genius Lex Luthor. After years of intense battle, Luthor finally
decides to end it all by going into the past and capturing Superman
the moment he lands on Earth. Using the time warp machine that he
built, Luthor goes back in time. Superman, wanting to thwart his evil
intentions, asks The Flash to push him back into time by breaking the
time barrier.
The Flash obliges and throws Superman back in time. But Superman
senses that something is fishy and the Earth doesn't feel the same.
After an hour of intense detective work (which he isn’t used to) he
finds out that he has been sent into a parallel anti-universe where
everything is reverse to that of our Universe. Looking at his watch,
Superman realizes that he'll first need to correct the time on his
watch in order to reach Kansas, the place where his ship landed, on
time.
Superman, not being as smart as you, asks for your help in fixing the
time on his watch. You know that the actual time on the other
anti-Universe is the mirror image of the time on Superman's watch,
considering the mirror to be a vertical line running from 12 to 6 on
the watch.
INPUT
The first line contains an integer T, denoting the number of test
cases. T lines follow with the time given in the 12-hour format: HH:MM
am/pm
OUTPUT
The output should consist of T lines with the mirrored time of each
test case in the 12-hour format: HH:MM am/pm
SAMPLE I/O
Input
Output
TL;DR
You will be given a time as
A picture worth thousands words
```
package foo.competition;
import j
It has been years since Superman started his enmity with the
super-genius Lex Luthor. After years of intense battle, Luthor finally
decides to end it all by going into the past and capturing Superman
the moment he lands on Earth. Using the time warp machine that he
built, Luthor goes back in time. Superman, wanting to thwart his evil
intentions, asks The Flash to push him back into time by breaking the
time barrier.
The Flash obliges and throws Superman back in time. But Superman
senses that something is fishy and the Earth doesn't feel the same.
After an hour of intense detective work (which he isn’t used to) he
finds out that he has been sent into a parallel anti-universe where
everything is reverse to that of our Universe. Looking at his watch,
Superman realizes that he'll first need to correct the time on his
watch in order to reach Kansas, the place where his ship landed, on
time.
Superman, not being as smart as you, asks for your help in fixing the
time on his watch. You know that the actual time on the other
anti-Universe is the mirror image of the time on Superman's watch,
considering the mirror to be a vertical line running from 12 to 6 on
the watch.
INPUT
The first line contains an integer T, denoting the number of test
cases. T lines follow with the time given in the 12-hour format: HH:MM
am/pm
OUTPUT
The output should consist of T lines with the mirrored time of each
test case in the 12-hour format: HH:MM am/pm
SAMPLE I/O
Input
3
05:00 am
06:00 pm
03:15 pmOutput
07:00 pm
06:00 am
08:45 amTL;DR
You will be given a time as
XX:XX am/pm (12 hour format) and you have to print the time that you will see if you hold the analog clock beside the mirror.A picture worth thousands words
```
package foo.competition;
import j
Solution
The challenge states that Parallel Universe Time is Normal Time, mirrored on the vertical axis of an analog clock; the examples suggest that AM/PM should be swapped. I initially found it ambiguous how noon and midnight should be treated, and assumed that 12:00 am should map to 12:00 pm and vice versa, consistent with @tintinmj's original code.
However, on further thought, I believe @abuzittingillifirca is right, that noon should map to noon and midnight should map to midnight. Consider the following diagram of the mapping. The blue lines are undisputed (for example, 1 am ⟷ 11 pm). By a continuity argument, it's more plausible that noon and midnight should be fixed points of the transformation (red lines).
Here is the revised code to implement that.
However, on further thought, I believe @abuzittingillifirca is right, that noon should map to noon and midnight should map to midnight. Consider the following diagram of the mapping. The blue lines are undisputed (for example, 1 am ⟷ 11 pm). By a continuity argument, it's more plausible that noon and midnight should be fixed points of the transformation (red lines).
Here is the revised code to implement that.
private static final Pattern TIME_FMT = Pattern.compile("(0?[1-9]|1[0-2]):([0-5]\\d)\\s*([ap]m)");
private static final int MINUTES_PER_DAY = 24 * 60;
/**
* Parses time as minutes since midnight.
*/
private static int parse(String time) {
Matcher m = TIME_FMT.matcher(time);
if (!m.matches()) {
throw new IllegalArgumentException(time);
}
int hour = Integer.parseInt(m.group(1)) % 12;
if ("pm".equals(m.group(3))) hour += 12;
int minute = Integer.parseInt(m.group(2));
return 60 * hour + minute;
}
/**
* Formats time, in minutes since midnight.
*/
private static String format(int minutes) {
String ampm = (minutes % MINUTES_PER_DAY < MINUTES_PER_DAY / 2) ? "am" : "pm";
int hour = (minutes / 60) % 12;
if (hour == 0) hour = 12;
int minute = minutes % 60;
return String.format("%02d:%02d %s", hour, minute, ampm);
}
public static String parallelUniverseTime(String time) {
return format(MINUTES_PER_DAY - parse(time));
}Code Snippets
private static final Pattern TIME_FMT = Pattern.compile("(0?[1-9]|1[0-2]):([0-5]\\d)\\s*([ap]m)");
private static final int MINUTES_PER_DAY = 24 * 60;
/**
* Parses time as minutes since midnight.
*/
private static int parse(String time) {
Matcher m = TIME_FMT.matcher(time);
if (!m.matches()) {
throw new IllegalArgumentException(time);
}
int hour = Integer.parseInt(m.group(1)) % 12;
if ("pm".equals(m.group(3))) hour += 12;
int minute = Integer.parseInt(m.group(2));
return 60 * hour + minute;
}
/**
* Formats time, in minutes since midnight.
*/
private static String format(int minutes) {
String ampm = (minutes % MINUTES_PER_DAY < MINUTES_PER_DAY / 2) ? "am" : "pm";
int hour = (minutes / 60) % 12;
if (hour == 0) hour = 12;
int minute = minutes % 60;
return String.format("%02d:%02d %s", hour, minute, ampm);
}
public static String parallelUniverseTime(String time) {
return format(MINUTES_PER_DAY - parse(time));
}Context
StackExchange Code Review Q#38044, answer score: 14
Revisions (0)
No revisions yet.