Last active
August 29, 2015 14:12
-
-
Save torazuka/5b90cc6ed563a97575f5 to your computer and use it in GitHub Desktop.
調整さん問題
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/ruby | |
# coding: utf-8 | |
require 'date' | |
# INPUT: 1/7-1/16 9:30-17:30 1.5 | |
# OUTPUT: 01/07(水) 09:30-11:00\n .. 1/16(金) 15:30-17:00\n | |
MIN = 60 | |
def get_day(day) | |
Date.strptime(Time.now.year.to_s << "/" << day, "%Y/%m/%d") | |
end | |
def parse(input) | |
if m = input.join(" ").match(/^([01]?[0-2]\/[0-3]?\d)-([01]?[0-2]\/[0-3]?\d)\s([0-2]?\d):([0-5]?\d)-([0-2]?\d):([0-5]?\d)\s(\d)/) then | |
return m[1], m[2], m[3].to_i, m[4].to_i, m[5].to_i, m[6].to_i, m[7].to_f | |
else | |
puts "mm/dd-mm/dd H:M-H:M h形式で日付を入力" | |
exit | |
end | |
end | |
def create_boxes(bh, bm, eh, em, box) | |
t = (((eh * MIN + em) - (bh * MIN + bm)) / (box * MIN)).to_i | |
((0...t).to_a).map{|n| | |
t_eh = (((bh * MIN + bm) + box.to_f * MIN) / MIN).to_i | |
t_em = (((bh * MIN + bm) + box.to_f * MIN) % MIN).to_i | |
t_bh, t_bm = bh, bm | |
bh, bm = t_eh, t_em | |
sprintf("%02d:%02d-%02d:%02d", t_bh, t_bm, t_eh, t_em) | |
} | |
end | |
def format(d) | |
wdays = ["日", "月", "火", "水", "木", "金", "土"] | |
d.strftime("%m/%d(#{wdays[d.wday]})") | |
end | |
def create_lines(day, times) | |
times.map {|time| | |
(format(day) << " " << time) | |
} | |
end | |
bd, ed, bh, bm, eh, em, box = parse(ARGV) | |
days = (get_day(bd) .. get_day(ed)).to_a | |
boxes = create_boxes(bh, bm, eh, em, box) | |
days.map {|day| | |
create_lines(day, boxes) | |
}.flatten.each { |line| puts line} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment