-
Notifications
You must be signed in to change notification settings - Fork 0
/
doomsday.rb
90 lines (78 loc) · 1.75 KB
/
doomsday.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# Step 0 - get date you want to discover
print 'Insert day (1-31): '
day = gets.to_i
print 'Insert month (1-12): '
month = gets.to_i
print 'Insert year (yyyy): '
year = gets
# Debug
# day = 21
# month = 6
# year = "1926"
# Step 1 - compute the base day of century
baseDay = (((5 * (year[0, 2].to_i + 1)) + (year[0, 2].to_i / 4).ceil % 7) + 4) % 7
# puts baseDay
# Step 2 - compute doomsday of year
doomsday = (((year[2, 4].to_i / 12).ceil + (year[2, 4].to_i % 12) + ((year[2, 4].to_i % 12) / 4).ceil) % 7) + baseDay
# brief formula
doomsday = ((year[2, 4].to_i + (year[2, 4].to_i / 4).ceil) % 7) + baseDay
# puts doomsday
# Step 3 - find the day of the week
nearest = 9999999999
case month
when 1
if ((year % 4 == 0) and (year % 100 != 0)) or (year % 400 == 0)
nearest = 4
else
[3, 31].each {
|val|
if (day-val).abs < nearest
nearest = val
end
}
end
when 2
ddays = [1, 29]
if ((year % 4 == 0) and (year % 100 != 0)) or (year % 400 == 0)
ddays = [7, 14, 21, 28]
end
ddays.each {
|val|
if (day-val).abs < nearest
nearest = val
end
}
when 3
[7, 14, 21, 28].each {
|val|
if (day-val).abs < nearest
nearest = val
end
}
when 4
nearest = 4
when 5
nearest = 9
when 6
nearest = 6
when 7
nearest = 11
when 8
nearest = 8
when 9
nearest = 5
when 10
nearest = 10
when 11
nearest = 7
when 12
nearest = 12
else
nearest = 9999
end
weekday = (day - nearest + doomsday) % 7
puts "~~~~~~~~~~~~~~~~~~~~~~~"
puts ['January', 'February', 'March',
'April', 'May', 'June', 'July',
'August', 'September', 'October',
'November', 'December'][month-1]+" "+day.to_s+", "+year+" was "+['Monday', 'Tuesday', 'Wednesday', 'Thusday', 'Friday', 'Saturday', 'Sunday'][weekday-1]