aboutsummaryrefslogtreecommitdiff
path: root/challenge-103/abigail/python
diff options
context:
space:
mode:
Diffstat (limited to 'challenge-103/abigail/python')
-rw-r--r--challenge-103/abigail/python/ch-1.py45
-rw-r--r--challenge-103/abigail/python/ch-2.py71
2 files changed, 116 insertions, 0 deletions
diff --git a/challenge-103/abigail/python/ch-1.py b/challenge-103/abigail/python/ch-1.py
new file mode 100644
index 0000000000..ebe2362239
--- /dev/null
+++ b/challenge-103/abigail/python/ch-1.py
@@ -0,0 +1,45 @@
+#!/opt/local/bin/python
+
+#
+# See ../README.md
+#
+
+#
+# Run as python ch-1.py < input-file
+#
+
+#
+# We're reading years from standard input, one year per line, outputting
+# years from the sexagenary cycle [1]. This is slightly more than what
+# the challenge ask; the challenge asks to output the heavenly stem [2],
+# and the earthly branch [3]. But we also output its Yin/Yang.
+#
+# [1] https://en.wikipedia.org/wiki/Sexagenary_cycle
+# [2] https://en.wikipedia.org/wiki/Heavenly_Stems
+# [3] https://en.wikipedia.org/wiki/Earthly_Branches
+#
+
+#
+# Each of the cycles have been rotated so the first entry corresponds to
+# the year 0 in the Proleptic Gregorian calendar. (We're using the
+# convention of having a year 0, as per ISO 8601).
+# That way, we can just mod the year with the number of entries, without
+# first having to subtract something from the year.
+#
+# The heavenly stems last for 2 years, so we just duplicate the entries.
+#
+
+yin_yang = ["Yang", "Yin"]
+heavenly_stems = ["Metal", "Metal", "Water", "Water", "Wood", "Wood",
+ "Fire", "Fire", "Earth", "Earth"]
+earthly_branches = ["Monkey", "Rooster", "Dog", "Pig", "Rat", "Ox",
+ "Tiger", "Rabbit", "Dragon", "Snake", "Horse", "Goat"]
+
+
+import fileinput
+
+for year in fileinput . input ():
+ year = int (year)
+ print (yin_yang [year % len (yin_yang)],
+ heavenly_stems [year % len (heavenly_stems)],
+ earthly_branches [year % len (earthly_branches)])
diff --git a/challenge-103/abigail/python/ch-2.py b/challenge-103/abigail/python/ch-2.py
new file mode 100644
index 0000000000..56f708220f
--- /dev/null
+++ b/challenge-103/abigail/python/ch-2.py
@@ -0,0 +1,71 @@
+#!/opt/local/bin/python
+
+#
+# See ../README.md
+#
+
+#
+# Run as python ch-2.py < input-file
+#
+
+#
+# We're reading a start time, the current time, and a file name
+# from STDIN, separated by white space. The file contains the
+# tracks played.
+#
+
+import fileinput
+
+for line in fileinput . input ():
+ start_time, current_time, file_name = line . split ()
+
+ #
+ # You need a 64 bit integer for this; 32 bits of holds
+ # a five week vacation in milliseconds, but the author
+ # of the puzzle lives somewhere were people take 14 week
+ # holidays...
+ #
+ time_diff = (int (current_time) - int (start_time)) * 1000
+
+ #
+ # Parse the media file; store the track into an array tracks.
+ #
+ tracks = []
+ total_time = 0
+ for track in open (file_name, "r"):
+ play_time, title = track . strip () . split (",", 2)
+ play_time = int (play_time)
+ tracks . append ([play_time, title])
+ total_time = total_time + play_time
+
+ #
+ # We're not interested in playing full loops.
+ #
+ time_diff = time_diff % total_time
+
+ #
+ # Find the current track playing. If $time_diff is less than the
+ # play time of the track, this is the track currently playing. If
+ # so, print the track name (with quotes, as given in the example --
+ # although they don't belong there, as they're from the CSV encoding;
+ # for that reason, we won't deescape anything else either). Otherwise
+ # (track length is shorter than $time_diff), we subtrack the length
+ # of the track from $time_diff, and continue checking the next track.
+ #
+ for track in tracks:
+ if time_diff - track [0] < 0:
+ time_diff = int (time_diff / 1000)
+ hours = int (time_diff / 3600)
+ minutes = int ((time_diff % 3600) / 60)
+ seconds = time_diff % 60
+
+ if hours > 0:
+ format_str = "{0}\n{1:02d}:{2:02d}:{3:02d}"
+ else:
+ format_str = "{0}\n{2:02d}:{3:02d}"
+
+ print (format_str . format (track [1], hours, minutes, seconds))
+ break
+ else:
+ time_diff = time_diff - track [0]
+