29
\$\begingroup\$

Your challenge is to write a program which, given a year, outputs the number of "Friday 13ths" in it.

Rules & Details:

  • You can take input via STDIN, or as an argument passed to your program.
  • You should output the result to STDOUT.
  • You may assume that input will be a valid year, and does not pre-date the Gregorian calendar (undefined behaviour is permitted in these cases).
  • Calendar/Date libraries are allowed.

This is a , so the shortest code (in bytes) wins.

(Related challenge link)

\$\endgroup\$
5
  • 7
    \$\begingroup\$ What is the required range of input? If it goes much before 1800, what assumptions should be made about switchover from Julian to Gregorian calendar? \$\endgroup\$ Dec 10, 2013 at 22:47
  • \$\begingroup\$ @PeterTaylor I hadn't thought about it. If a date pre-dates gregorian then you can have undefined behaviour. \$\endgroup\$
    – Cruncher
    Dec 11, 2013 at 14:05
  • 1
    \$\begingroup\$ The first countries to adopt the Gregorian calendar did so in October 1582, following the bull of Gregory himself. Countries to adopt the new calendar late did not change until the 20th century, for example Greece introduced it on 1 March 1923. \$\endgroup\$ Dec 11, 2013 at 14:16
  • \$\begingroup\$ @JeppeStigNielsen I don't know much about calendars and such. Whether they adopted them or not doesn't change what the gregorian dates are. Libraries should be able to calculate dates from quite a ways back I presume? \$\endgroup\$
    – Cruncher
    Dec 11, 2013 at 14:17
  • 5
    \$\begingroup\$ I am being offtopic here, I guess. Many libraries written by Anglo-American programmers use September 1752 as the "correct" time of change of calendars. This was when the British Empire changed. The new calendar was kept when U.S.A. was founded, of course. (As a curiosity, some SQL software has 1753 as the minimal year since they don't want to cope with the September 1752 issue.) However, using September 1752 is highly anglocentric. You are right Gregorian dates are the same whether they were used historically or not. That is the so-called proleptic Gregorian calendar. \$\endgroup\$ Dec 11, 2013 at 14:38

37 Answers 37

1
2
0
\$\begingroup\$

Lua, 95 bytes

n=0 for i=1,12 do n=os.date('%w',os.time{month=i,day=13,year=...})=='5'and n+1or n end print(n)

Try it online!

\$\endgroup\$
0
\$\begingroup\$

Scala 3, 107 bytes

Golfed version. Attempt This Online!

y=>(1 to 12).map{m=>if(java.time.LocalDate.of(y,m,6).getDayOfWeek==java.time.DayOfWeek.FRIDAY)1 else 0}.sum

Ungolfed version. Attempt This Online!

import java.time._

object Main {
  def main(args: Array[String]): Unit = {
    println(countFridays(1776))
    println(countFridays(2012))
    println(countFridays(2013))
    println(countFridays(2014))
  }

  def countFridays(year: Int): Int = {
    (1 to 12).map { month =>
      val firstDay = LocalDate.of(year, month, 6).getDayOfWeek
      if (firstDay == DayOfWeek.FRIDAY) 1 else 0
    }.sum
  }
}
\$\endgroup\$
0
\$\begingroup\$

JavaScript, 56 47 bytes

f=(y,m=12)=>m&&f(y,--m)+!new Date(y,m).getDay()

Try It Online!

\$\endgroup\$
1
  • \$\begingroup\$ 54 \$\endgroup\$
    – l4m2
    Nov 20 at 1:38
0
\$\begingroup\$

Japt -x, 10 8 bytes

CÆ!ÐUX e

Try it

CÆ5¥ÐUXD e     :Implicit input of integer U
C              :12
 Æ             :  Map each X in the range [0,C) (months are 0-indexed in JavaScript)
  5¥           :  Check if 5 is equal to
    ÐUXD       :  new Date(U,X,13)
         e     :  0-based index of day of the week
               :Implicitly reduce by addition and output
\$\endgroup\$
0
\$\begingroup\$

JavaScript (Node.js), 47 bytes

f=(y,m=12)=>m&&f(y,--m)+!new Date(y,m).getDay()

Attempt This Online!

-2 bytes thanks to l4m2

\$\endgroup\$
1
0
\$\begingroup\$

Go, 114 bytes

import."time"
func f(y int)(c int){for i:=0;i<12;i++{if Date(y,Month(i+1),1,0,0,0,0,UTC).Weekday()<1{c++}}
return}

Counts months starting with Sundays.

Attempt This Online!

\$\endgroup\$
0
\$\begingroup\$

Rust, 103 bytes

Golfed version. Run it on Rust Playground!

fn c(y:i32)->i32{(1..=12).filter(|m|NaiveDate::from_ymd(y,*m,6).weekday()==Weekday::Fri).count()as i32}

Ungolfed version. Run it on Rust Playground!

use chrono::{Datelike, Weekday, NaiveDate};

fn main() {
    println!("{}", count_fridays(1776));
    println!("{}", count_fridays(2012));
    println!("{}", count_fridays(2013));
    println!("{}", count_fridays(2014));
}

fn count_fridays(year: i32) -> i32 {
    let mut count = 0;
    for month in 1..=12 {
        let date = NaiveDate::from_ymd(year, month, 6);
        if date.weekday() == Weekday::Fri {
            count += 1;
        }
    }
    count
}
\$\endgroup\$
1
2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.