import java.util.Scanner; /* * To change this template, choose Tools | Templates * and open the template in the editor. */ //package javaapplication1; /** * * @author cteam38 */ public class bus { public static void main(String[] args) { Scanner sc = new Scanner(System.in); String S, R; int Y; Bus b; while (true){ if (!sc.hasNext()) break; S = sc.next(); R = sc.next(); Y = sc.nextInt(); b = new Bus(S, Y); b.proccess(R); } } static class Bus{ static int [][] holidaysDays = {{1,1},{1,5},{8,5},{5,7},{6,7},{28,9},{28,10},{17,11},{24,12},{25,12},{26,12}}; static int [][] holidaysAfter = {{2,1},{2,5},{9,5},{7,7},{29,9},{29,10},{18,11},{27,12}}; static int [] daysNoLeap = {0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334}; static int [] daysWithLeap = {0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335}; static int[] countInMonth = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; private int dayOfWeek = 5; private int year = 2010; boolean[] daysIn = new boolean[7]; boolean holidays, worksdays, worksAfterHolidays, isLeapFinnaly; int easterDay, easterMonth, easterDayA, easterMonthA;; public Bus(String S, int Y){ if (year < Y){ while (year != Y){ if (isLeap(year)) dayOfWeek += 2; else dayOfWeek ++; year++; } } else{ while (year != Y){ year --; if (isLeap(year)) dayOfWeek -= 2; else dayOfWeek --; } } isLeapFinnaly = isLeap(year); //Easter monday insertEasterMonday(year); char c; int x; for(int i = 0; i < S.length(); i++){ c = S.charAt(i); if(c == 't') holidays = true; else if(c=='w') worksdays = true; else if(c == 'a') worksAfterHolidays = true; else { x = Integer.parseInt(c+""); daysIn[x - 1] = true; } /*for (boolean a : daysIn){ System.out.println(a); }*/ } } private int dayOfWeek(int day, int month) { int x = (isLeapFinnaly) ? daysWithLeap[month - 1] : daysNoLeap[month - 1]; x = x +day; x = (x%7 + dayOfWeek - 1) % 7; return (x==0) ? 7 : x; } private boolean isHoliday(int day, int month) { for (int [] hol : holidaysDays){ if (hol[0] == day && hol[1] == month) return true; } if (day == easterDay && month == easterMonth) return true; if(dayOfWeek(day, month) == 7) return true; return false; } private boolean isHolidayAfter(int day, int month) { for (int [] hol : holidaysAfter){ if (hol[0] == day && hol[1] == month) {//System.out.println("a"); return true; } } if (day == easterDayA && month == easterMonthA) {//System.out.println("b"); return true; } if(dayOfWeek(day, month) == 1) {//System.out.println("c"); return true; } return false; } private boolean isRight(int day, int month){ //System.out.println(day + "." + month + "."); if(holidays && isHoliday(day, month)){//System.out.println("a"); return true; } for(int i = 1; i <= 7; i++ ){ if(daysIn[i-1] && dayOfWeek(day, month) == i){//System.out.println("b"); return true; } } if (worksdays && isWorkDay(day, month) && !isHoliday(day, month)) {//System.out.println("c"); return true; } if(worksAfterHolidays && isHolidayAfter(day, month) && isWorkDay(day, month)) {//System.out.println("d"); return true; } return false; } private boolean isLeap(int year2) { if(year2 % 4 != 0) return false; if ( year2 % 100 == 0 && year2 % 400 != 0) return false; return true; } private boolean isWorkDay(int day, int month) { return (dayOfWeek(day, month) <= 5); } private int daysInMonth(int month){ if (month == 2 && isLeapFinnaly) return 29; return countInMonth[month - 1]; } public void proccess(String R) { String [] dates = R.split(","); String[] d; String[] a; int d1, m1, d2, m2, countDaysInMonths, count = 0; for (String date : dates){ if(date.length() > 6){ a = date.split("-"); d = a[0].split("\\."); // System.out.println(a[0]); d1 = Integer.parseInt(d[0]); m1 = Integer.parseInt(d[1]); d = a[1].split("\\."); d2 = Integer.parseInt(d[0]); m2 = Integer.parseInt(d[1]); for (int i = m1; i <= m2; i++){ countDaysInMonths = daysInMonth(i); if (i == m2) countDaysInMonths = d2 - 1; if(countDaysInMonths == 0){ i--; countDaysInMonths = daysInMonth(i); } for (int j = d1; j <= countDaysInMonths; j++){ if (isRight(j, i)) { count++; // if(j==6 && i ==4){ //System.out.println("--------------"+j + "." + i + "."); //} } } } } else{ d = date.split("\\."); d1 = Integer.parseInt(d[0]); m1 = Integer.parseInt(d[1]); if (isRight(d1, m1)) count++; } } System.out.println(count); } private void insertEasterMonday(int y){ int golden, solar, lunar, p; golden = (y % 19) + 1; solar = (y - 1600) / 100 - (y - 1600) / 400; lunar = (((y - 1400) / 100) * 8) / 25; p = (3003 - (11 * golden) + solar - lunar) % 30; if (p == 29 || (p == 28 && golden > 11))p--; int day = 21 + p; int month = 3; if (day > 31){ day = day -31; month++; } int d = dayOfWeek(day, month); if(d != 7) day = (7-d) + day + 1; else day++; if (day > 31){ day = day -31; month++; } easterDay = day; easterMonth = month; day++; if (day > 31){ day = day -31; month++; } easterDayA = day; easterMonthA = month; } } }