-
Notifications
You must be signed in to change notification settings - Fork 0
/
doomsday.go
74 lines (62 loc) · 1.44 KB
/
doomsday.go
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
package main
import (
"flag"
"fmt"
"math/rand"
"time"
"os"
)
var yearMin, yearMax int
var helpFlag = flag.Bool("d", false, "print doomsdays")
func init() {
flag.IntVar(&yearMin, "min", 1800, "min year")
flag.IntVar(&yearMax, "max", 2100, "max year")
rand.Seed(time.Now().Unix())
}
func date(year int, month time.Month, day int) time.Time {
return time.Date(year, month, day, 12, 0, 0, 0, time.Local)
}
func doomsday(year int) time.Weekday {
return date(year, time.March, 14).Weekday()
}
func help() {
for i := yearMin - yearMin%100; i <= yearMax; i += 100 {
fmt.Printf("%d - %s\n", i, doomsday(i))
}
prevMonth := time.December
for t := date(1999, 1, 1); t.Before(date(2000, 1, 1)); t = t.AddDate(0, 0, 1) {
if t.Weekday() == doomsday(1999) {
if prevMonth != t.Month() {
fmt.Println()
fmt.Printf("%10s", t.Month())
prevMonth = t.Month()
}
fmt.Printf("%3d", t.Day())
}
}
fmt.Println()
}
func main() {
flag.Parse()
if *helpFlag {
help()
return
}
t := date(yearMin+rand.Intn(yearMax-yearMin),
time.January,
1+rand.Intn(365))
fmt.Printf("%d\n", t.Year())
guessWeekday(doomsday(t.Year()))
fmt.Printf("%d-%d-%d\n", t.Year(), t.Month(), t.Day())
guessWeekday(t.Weekday())
}
func guessWeekday(goal time.Weekday) {
var guess int
fmt.Printf("Sun-Sat [0-6]? ")
fmt.Scanf("%d", &guess)
if guess != int(goal) {
fmt.Printf("Wrong, it was a %s\n", goal)
os.Exit(1)
}
fmt.Printf("Correct, it was a %s\n", goal)
}