Skip to content

Commit

Permalink
Solve p19 in c, c++
Browse files Browse the repository at this point in the history
  • Loading branch information
LivInTheLookingGlass committed Aug 16, 2024
1 parent 090899e commit d74682b
Show file tree
Hide file tree
Showing 12 changed files with 175 additions and 5 deletions.
4 changes: 2 additions & 2 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,12 @@ Olivia's Project Euler Solutions
+------------+----------------------------+--------+-------------------+
| Language | Version | Solved | Status |
+============+============================+========+===================+
| C | C99+ in: |clang|, |br| | 24 | |Ci| |br| |
| C | C99+ in: |clang|, |br| | 25 | |Ci| |br| |
| | |gcc|, |pcc|, & |tcc| |br| | | |C-Cov| |br| |
| | C11+ in: |msvc| [1]_ | | |CodeQL| |br| |
| | | | |C-lint| |
+------------+----------------------------+--------+-------------------+
| C++ | C++98+ in: |br| |clang|, & | 17 | |Cpi| |br| |
| C++ | C++98+ in: |br| |clang|, & | 18 | |Cpi| |br| |
| | |gcc| |br| | | |Cp-Cov| |br| |
| | C++14+ in: |msvc| [1]_ | | |CodeQL| |br| |
| | | | |Cp-lint| |
Expand Down
2 changes: 2 additions & 0 deletions c/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,8 @@ Problems Solved
- ☒ `14 <./src/p0014.c>`__
- ☒ `15 <./src/p0015.c>`__
- ☒ `16 <./src/p0016.c>`__
- ☒ `17 <./src/p0017.c>`__
- ☒ `19 <./src/p0019.c>`__
- ☒ `20 <./src/p0020.c>`__
- ☒ `22 <./src/p0022.c>`__
- ☒ `34 <./src/p0034.c>`__
Expand Down
61 changes: 61 additions & 0 deletions c/src/p0019.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
Project Euler Problem 19
This one ended up being very easy thanks to the time library
Problem:
You are given the following information, but you may prefer to do some research
for yourself.
1 Jan 1900 was a Monday.
Thirty days has September,
April, June and November.
All the rest have thirty-one,
Saving February alone,
Which has twenty-eight, rain or shine.
And on leap years, twenty-nine.
A leap year occurs on any year evenly divisible by 4, but not on a century
unless it is divisible by 400.
How many Sundays fell on the first of the month during the twentieth century
(1 Jan 1901 to 31 Dec 2000)?
*/
#ifndef EULER_P0019
#define EULER_P0019
#include <time.h>
#include <stdio.h>
#include <stdint.h>
#include <inttypes.h>

uint16_t p0019() {
uint16_t answer = 0;
struct tm date = {0};

for (int year = 1901; year <= 2000; ++year) {
for (int month = 0; month < 12; ++month) {
date.tm_year = year - 1900;
date.tm_mon = month;
date.tm_mday = 1;

if (mktime(&date) == -1) {
fprintf(stderr, "mktime failed to normalize the date.\n");
return -1;
}

if (date.tm_wday == 0) {
++answer;
}
}
}
return answer;
}


#ifndef UNITY_END
int main(int argc, char const *argv[]) {
printf("%" PRIu16 "\n", p0019());
return 0;
}
#endif
#endif
2 changes: 2 additions & 0 deletions c/test.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include "src/p0015.c"
#include "src/p0016.c"
#include "src/p0017.c"
#include "src/p0019.c"
#include "src/p0020.c"
#include "src/p0022.c"
#include "src/p0025.c"
Expand Down Expand Up @@ -48,6 +49,7 @@ const Answer answers[] = {
{15, 137846528820, p0015},
{16, 1366, p0016},
{17, 21124, p0017},
{19, 171, (uint64_t (*)()) p0019},
{20, 648, p0020},
{22, 871198282, p0022},
{25, 4782, p0025},
Expand Down
2 changes: 1 addition & 1 deletion c/test_euler.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
answers = {
x: get_answer(x) for x in (
*range(1, 18),
20,
*range(19, 21),
22,
25,
30,
Expand Down
59 changes: 59 additions & 0 deletions cplusplus/src/p0019.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
Project Euler Problem 19
This one ended up being very easy thanks to the ctime library
Problem:
You are given the following information, but you may prefer to do some research
for yourself.
1 Jan 1900 was a Monday.
Thirty days has September,
April, June and November.
All the rest have thirty-one,
Saving February alone,
Which has twenty-eight, rain or shine.
And on leap years, twenty-nine.
A leap year occurs on any year evenly divisible by 4, but not on a century
unless it is divisible by 400.
How many Sundays fell on the first of the month during the twentieth century
(1 Jan 1901 to 31 Dec 2000)?
*/
#ifndef EULER_P0019
#define EULER_P0019
#include <ctime>
#include <iostream>
#include <cstdint>
#include <stdexcept>

uint16_t p0019() {
uint16_t answer = 0;
struct tm date = {0};

for (int year = 1901; year <= 2000; ++year) {
for (int month = 0; month < 12; ++month) {
date.tm_year = year - 1900;
date.tm_mon = month;
date.tm_mday = 1;

if (mktime(&date) == -1) {
throw std::runtime_error("mkdtime failed to normalize the date.");
}
if (date.tm_wday == 0) {
++answer;
}
}
}
return answer;
}


#ifndef UNITY_END
int main(int argc, char const *argv[]) {
std::cout << p0019() << std::endl;
return 0;
}
#endif
#endif
2 changes: 2 additions & 0 deletions cplusplus/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include "src/p0015.cpp"
#include "src/p0016.cpp"
#include "src/p0017.cpp"
#include "src/p0019.cpp"
#include "src/p0020.cpp"
#include "src/p0022.cpp"
#include "src/p0034.cpp"
Expand Down Expand Up @@ -47,6 +48,7 @@ static const Answer answers[] = {
{15, 137846528820, p0015},
{16, 1366, p0016},
{17, 21124, p0017},
{19, 171, (uint64_t (*)()) p0019},
{20, 648, p0020},
{22, 871198282, p0022},
{34, 40730, p0034},
Expand Down
2 changes: 1 addition & 1 deletion cplusplus/test_euler.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
9,
11,
*range(13, 18),
20,
*range(19, 21),
22,
34,
76,
Expand Down
2 changes: 1 addition & 1 deletion docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ Problems Solved
+-----------+------------+------------+------------+------------+------------+------------+------------+
|:prob:`18` | | | | | |:py-d:`0018`|:rs-d:`0018`|
+-----------+------------+------------+------------+------------+------------+------------+------------+
|:prob:`19` | | | | |:js-d:`0019`|:py-d:`0019`|:rs-d:`0019`|
|:prob:`19` |:c-d:`0019` |:cp-d:`0019`| | |:js-d:`0019`|:py-d:`0019`|:rs-d:`0019`|
+-----------+------------+------------+------------+------------+------------+------------+------------+
|:prob:`20` |:c-d:`0020` |:cp-d:`0020`|:cs-d:`0020`|:ja-d:`0020`|:js-d:`0020`|:py-d:`0020`|:rs-d:`0020`|
+-----------+------------+------------+------------+------------+------------+------------+------------+
Expand Down
22 changes: 22 additions & 0 deletions docs/src/c/p0019.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
C Implementation of Problem 19
==============================

View source code :source:`c/src/p0019.c`

Solution
--------

.. c:function:: uint16_t p0019()
.. c:function:: int main(int argc, char const *argv[])
.. note::
This function is only present in the Python test runner, or when compiling as a standalone program.
It is not present when compiling for the Unity test runner.
.. literalinclude:: ../../../c/src/p0019.c
:language: C
:linenos:
.. tags:: calendar, combinatorics
21 changes: 21 additions & 0 deletions docs/src/cplusplus/p0019.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
C++ Implementation of Problem 19
================================

View source code :source:`cplusplus/src/p0019.cpp`

Solution
--------

.. cpp:function:: uint16_t p0019()

.. cpp:function:: int main(int argc, char const *argv[])

.. note::

This function is only present in the Python test runner, or when compiling as a standalone program.

.. literalinclude:: ../../../cplusplus/src/p0019.cpp
:language: C++
:linenos:

.. tags:: calendar, combinatorics
1 change: 1 addition & 0 deletions javascript/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ Problems Solved
- ☒ `15 <./src/p0015.js>`__
- ☒ `16 <./src/p0016.js>`__
- ☒ `17 <./src/p0017.js>`__
- ☒ `19 <./src/p0019.js>`__
- ☒ `20 <./src/p0020.js>`__
- ☒ `22 <./src/p0022.js>`__
- ☒ `27 <./src/p0027.js>`__
Expand Down

0 comments on commit d74682b

Please sign in to comment.