diff options
| author | Paulo Custodio <pauloscustodio@gmail.com> | 2023-04-01 23:59:32 +0100 |
|---|---|---|
| committer | Paulo Custodio <pauloscustodio@gmail.com> | 2023-04-01 23:59:32 +0100 |
| commit | 2619919a5a726a4dffc15357813c15bdbd2acb73 (patch) | |
| tree | d7e037d85a9b4073d3543982e2f1e952e298e08d | |
| parent | 634867957bd351d613e82e4cc75beb409c1fd96a (diff) | |
| download | perlweeklychallenge-club-2619919a5a726a4dffc15357813c15bdbd2acb73.tar.gz perlweeklychallenge-club-2619919a5a726a4dffc15357813c15bdbd2acb73.tar.bz2 perlweeklychallenge-club-2619919a5a726a4dffc15357813c15bdbd2acb73.zip | |
Add C solution
| -rw-r--r-- | challenge-132/paulo-custodio/Makefile | 2 | ||||
| -rw-r--r-- | challenge-132/paulo-custodio/c/ch-1.c | 88 | ||||
| -rw-r--r-- | challenge-132/paulo-custodio/test.pl | 4 |
3 files changed, 90 insertions, 4 deletions
diff --git a/challenge-132/paulo-custodio/Makefile b/challenge-132/paulo-custodio/Makefile new file mode 100644 index 0000000000..c3c762d746 --- /dev/null +++ b/challenge-132/paulo-custodio/Makefile @@ -0,0 +1,2 @@ +all: + perl ../../challenge-001/paulo-custodio/test.pl diff --git a/challenge-132/paulo-custodio/c/ch-1.c b/challenge-132/paulo-custodio/c/ch-1.c new file mode 100644 index 0000000000..91d89db0ed --- /dev/null +++ b/challenge-132/paulo-custodio/c/ch-1.c @@ -0,0 +1,88 @@ +/* +Challenge 132 + +TASK #1 > Mirror Dates +Submitted by: Mark Anderson +You are given a date (yyyy/mm/dd). + +Assuming, the given date is your date of birth. Write a script to find the +mirror dates of the given date. + +Dave Cross has built cool site that does something similar. + +Assuming today is 2021/09/22. +Example 1: +Input: 2021/09/18 +Output: 2021/09/14, 2021/09/26 + +On the date you were born, someone who was your current age, would have +been born on 2021/09/14. +Someone born today will be your current age on 2021/09/26. +Example 2: +Input: 1975/10/10 +Output: 1929/10/27, 2067/09/05 + +On the date you were born, someone who was your current age, would have +been born on 1929/10/27. +Someone born today will be your current age on 2067/09/05. +Example 3: +Input: 1967/02/14 +Output: 1912/07/08, 2076/04/30 + +On the date you were born, someone who was your current age, would have +been born on 1912/07/08. +Someone born today will be your current age on 2076/04/30. +*/ + +#include <stdio.h> +#include <stdlib.h> + +// https://pdc.ro.nu/jd-code.html +long gregorian_date_to_jd(int y, int m, int d) { + y += 8000; + if (m < 3) { y--; m += 12; } + return (y * 365) + (y / 4) - (y / 100) + (y / 400) - 1200820 + + (m * 153 + 3) / 5 - 92 + d - 1; +} + +void jd_to_gregorian_date(long jd, int* yp, int* mp, int* dp) { + int y, m, d; + for (y = jd / 366 - 4715; gregorian_date_to_jd(y + 1, 1, 1) <= jd; y++); + for (m = 1; gregorian_date_to_jd(y, m + 1, 1) <= jd; m++); + for (d = 1; gregorian_date_to_jd(y, m, d + 1) <= jd; d++); + *yp = y; *mp = m; *dp = d; +} + + +int main(int argc, char* argv[]) { + if (argc != 3) { + fputs("usage: ch-1 birth_date today", stderr); + return EXIT_FAILURE; + } + + int by, bm, bd; + if (sscanf(argv[1], "%d/%d/%d", &by, &bm, &bd) != 3) { + fputs("invalid birth date", stderr); + return EXIT_FAILURE; + } + + int ty, tm, td; + if (sscanf(argv[2], "%d/%d/%d", &ty, &tm, &td) != 3) { + fputs("invalid birth date", stderr); + return EXIT_FAILURE; + } + + long bjd = gregorian_date_to_jd(by, bm, bd); + long tjd = gregorian_date_to_jd(ty, tm, td); + long days = tjd - bjd; + long mirror1_jd = bjd - days; + long mirror2_jd = tjd + days; + + int m1y, m1m, m1d; + jd_to_gregorian_date(mirror1_jd, &m1y, &m1m, &m1d); + + int m2y, m2m, m2d; + jd_to_gregorian_date(mirror2_jd, &m2y, &m2m, &m2d); + + printf("%04d/%02d/%02d, %04d/%02d/%02d\n", m1y, m1m, m1d, m2y, m2m, m2d); +} diff --git a/challenge-132/paulo-custodio/test.pl b/challenge-132/paulo-custodio/test.pl deleted file mode 100644 index ba6c37260b..0000000000 --- a/challenge-132/paulo-custodio/test.pl +++ /dev/null @@ -1,4 +0,0 @@ -#!/usr/bin/env perl -use Modern::Perl; -use Test::More; -require '../../challenge-001/paulo-custodio/test.pl'; |
