aboutsummaryrefslogtreecommitdiff
path: root/challenge-138/java/WorkDays.java
blob: 8d98f71adf66377e04152e90cc8951bd3dfdcd8b (plain)
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
// The Weekly Challenge - 138
// Task 1 Work Days
// Usage: java WorkDays YYYY

import java.time.LocalDate;

public class WorkDays
{
    public static void main(String[] args)
    {
        int year = Integer.parseInt(args[0]);
        System.out.println(numOfWorkDays(year));

        testing();
        benchmark();

    }

    public static void testing()
    {
        for (int i = 1900; i <= 2400; i++)
        {
            if (numOfWorkDays(i) != simple_days_count(i))
                System.out.println("big event in " + i);
        }
    }

    public static int numOfWorkDays (int year) {
        LocalDate MondayJan = LocalDate.of(year, 1, 1);
        int ans = 0;
        int ga = gd(MondayJan);
        while (ga != 1) 
        {
            if (ga <= 5 && ga >= 2) 
                ans++;
            MondayJan = MondayJan.plusDays(1);
            ga = gd(MondayJan);
        }
        LocalDate lastDay = MondayJan.plusWeeks(51);
        ans += 51*5;
        int gb = gd(lastDay);
        while (lastDay.getDayOfYear() < lastDay.lengthOfYear()) 
        {
            if (gb <= 5 && gb >= 1)
                ans++;
            lastDay = lastDay.plusDays(1);
            gb = gd(lastDay);
        }
        ans += gb <= 5 ? 1 : 0;
        return ans;
    }


    public static int simple_days_count(int year)
    {
        int i = 0;
        LocalDate my_day = LocalDate.of(year, 1, 1);
        while (my_day.getYear() == year)
        {
            if (gd(my_day) != 6 && gd(my_day) != 7)
            {
                i++;
            }
            my_day = my_day.plusDays(1);
        }
        return i;
    }


    public static int gd(LocalDate date)
    {
        var a = date.getDayOfWeek();
        switch(a)
        {
            case MONDAY: return 1;
            case TUESDAY: return 2;
            case WEDNESDAY: return 3;
            case THURSDAY: return 4;
            case FRIDAY: return 5;
            case SATURDAY: return 6;
            case SUNDAY: return 7;
        }
        return 0;
    }

    public static void benchmark()
    {
        long time_0 = System.nanoTime();
        for (int i = 1975; i < 2400; i++) {
            simple_days_count(i);
        }
        long time_1 = System.nanoTime();
        for (int i = 1975; i < 2400; i++) {
            numOfWorkDays(i);
        }
        long time_2 = System.nanoTime();
        System.out.println("simple_days_count " + (time_1 - time_0));
        System.out.println("numOfWorkDays " + (time_2 - time_1));

        // Output:
        // simple_days_count  10398009
        // numOfWorkDays        344198

    }

}