19
\$\begingroup\$

(Inspired by last week's Riddler on FiveThirtyEight.com. Sandbox post.)

Given a year between 2001 and 2099, calculate and return the number of days during that calendar year where mm * dd = yy (where yy is the 2-digit year).

2018, for example, has 5:

  • January 18th (1 * 18 = 18)
  • February 9th (2 * 9 = 18)
  • March 6th (3 * 6 = 18)
  • June 3rd (6 * 3 = 18)
  • September 2nd (9 * 2 = 18)

Input can be a 2 or 4-digit numeric year.

Output should be an integer. Optional trailing space or return is fine.

Complete input/output list:

Input = Output
 2001 = 1     2021 = 3     2041 = 0     2061 = 0     2081 = 2
 2002 = 2     2022 = 3     2042 = 4     2062 = 0     2082 = 0
 2003 = 2     2023 = 1     2043 = 0     2063 = 3     2083 = 0
 2004 = 3     2024 = 7     2044 = 3     2064 = 2     2084 = 5
 2005 = 2     2025 = 2     2045 = 3     2065 = 1     2085 = 1
 2006 = 4     2026 = 2     2046 = 1     2066 = 3     2086 = 0
 2007 = 2     2027 = 3     2047 = 0     2067 = 0     2087 = 1
 2008 = 4     2028 = 4     2048 = 6     2068 = 1     2088 = 3
 2009 = 3     2029 = 1     2049 = 1     2069 = 1     2089 = 0
 2010 = 4     2030 = 6     2050 = 3     2070 = 3     2090 = 5
 2011 = 2     2031 = 1     2051 = 1     2071 = 0     2091 = 1
 2012 = 6     2032 = 3     2052 = 2     2072 = 6     2092 = 1
 2013 = 1     2033 = 2     2053 = 0     2073 = 0     2093 = 1
 2014 = 3     2034 = 1     2054 = 4     2074 = 0     2094 = 0
 2015 = 3     2035 = 2     2055 = 2     2075 = 2     2095 = 1
 2016 = 4     2036 = 6     2056 = 4     2076 = 1     2096 = 4
 2017 = 1     2037 = 0     2057 = 1     2077 = 2     2097 = 0
 2018 = 5     2038 = 1     2058 = 0     2078 = 2     2098 = 1
 2019 = 1     2039 = 1     2059 = 0     2079 = 0     2099 = 2
 2020 = 5     2040 = 5     2060 = 6     2080 = 4

This is a challenge, lowest byte count in each language wins.

Pre-calculating and simply looking up the answers is normally excluded per our loophole rules, but I'm explicitly allowing it for this challenge. It allows for some interesting alternate strategies, although its not likely a 98 99-item lookup list is going to be shortest.

\$\endgroup\$
  • \$\begingroup\$ If it makes it any easier in your language, the answer will be the same regardless of century; 1924 and 2124 have the same number of days as 2024. \$\endgroup\$ – BradC Apr 13 '18 at 14:41
  • \$\begingroup\$ if the the result of mm*dd is bigger than 100 it is automatically filtered? \$\endgroup\$ – DanielIndie Apr 13 '18 at 16:03
  • \$\begingroup\$ @DanielIndie Correct, no "wraparound" dates should be counted. In other words, Dec 12, 2044 doesn't count, even though 12 * 12 = 144. \$\endgroup\$ – BradC Apr 13 '18 at 16:13
  • \$\begingroup\$ As we need only handle a limited number of inputs, I've edited them all in. Feel free to rollback or reformat. \$\endgroup\$ – Shaggy Apr 13 '18 at 17:20
  • 1
    \$\begingroup\$ @gwaugh Just that you can decide which to accept as valid input (so you don't have to spend extra characters converting between the two). \$\endgroup\$ – BradC Feb 4 at 20:06

31 Answers 31

0
\$\begingroup\$

Japt -x, 18 bytes

Takes input as an integer in the range 1-99.

346Ç=ÐUTZ)f *ÒZÎ¥U

Try it

As with my JS solution, takes advantage of the fact that JavaScript's Date will rollover to the next month, and continue doing so, if you pass it a day value that exceeds the number of days in the month you pass to it so, on the first iteration, ÐUTZ tries to construct the date yyyy-01-345 which becomes yyyy-12-11, or yyyy-12-10 on leap years. We don't need to check dates after day 345 that as 12*11+ results in a 3-digit number.

346Ç=ÐUTZ)f *ÒZÎ¥U     :Implicit input of integer U
346Ç                   :Map each Z in the range [0,346)
    =                  :  Reassign to Z
     ÐUTZ              :    new Date(U,0,Z) - months are 0-indexed in JS
         )             :  End reassignment
          f            :  Get the day of the month
            *Ò         :  Multiply by the negation of bitwise NOT of
              ZÎ       :  Get 0-based month of Z
                ¥U     :  Test for equality with U
                       :Implicitly reduce by adition and output
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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