diff options
| author | Abigail <abigail@abigail.be> | 2021-03-08 17:40:58 +0100 |
|---|---|---|
| committer | Abigail <abigail@abigail.be> | 2021-03-14 19:59:45 +0100 |
| commit | 30b59df5174d391d0becb21538fe3c2ea298346c (patch) | |
| tree | 52d47a8022c661ca9e49ab803060ce6b8c795ed7 /challenge-103 | |
| parent | 292e33319493e09adfee8f35a1b67b07e493e75a (diff) | |
| download | perlweeklychallenge-club-30b59df5174d391d0becb21538fe3c2ea298346c.tar.gz perlweeklychallenge-club-30b59df5174d391d0becb21538fe3c2ea298346c.tar.bz2 perlweeklychallenge-club-30b59df5174d391d0becb21538fe3c2ea298346c.zip | |
C solution for week 103, part 1
Diffstat (limited to 'challenge-103')
| -rw-r--r-- | challenge-103/abigail/README.md | 1 | ||||
| -rw-r--r-- | challenge-103/abigail/c/ch-1.c | 57 |
2 files changed, 58 insertions, 0 deletions
diff --git a/challenge-103/abigail/README.md b/challenge-103/abigail/README.md index ee507a9b99..e59555b95d 100644 --- a/challenge-103/abigail/README.md +++ b/challenge-103/abigail/README.md @@ -51,6 +51,7 @@ output. ### Solutions * [AWK](awk/ch-1.awk) * [Bash](bash/ch-1.sh) +* [C](c/ch-1.c) * [Perl](perl/ch-1.pl) ### Blog diff --git a/challenge-103/abigail/c/ch-1.c b/challenge-103/abigail/c/ch-1.c new file mode 100644 index 0000000000..f4974ced54 --- /dev/null +++ b/challenge-103/abigail/c/ch-1.c @@ -0,0 +1,57 @@ +# include <stdlib.h> +# include <stdio.h> +# include <string.h> + +/* + * See ../README.md + */ + +/* + * Run as: cc -o ch-1.o ch-1.c; ./ch-1.o < input-file + */ + +/* + * We're reading years from standard input, one year per line, outputting + * years from the sexagenary cycle [1]. This is slightly more than what + * the challenge ask; the challenge asks to output the heavenly stem [2], + * and the earthly branch [3]. But we also output its Yin/Yang. + * + * [1] https://en.wikipedia.org/wiki/Sexagenary_cycle + * [2] https://en.wikipedia.org/wiki/Heavenly_Stems + * [3] https://en.wikipedia.org/wiki/Earthly_Branches + * + + * + * Each of the cycles have been rotated so the first entry corresponds to + * the year 0 in the Proleptic Gregorian calendar. (We're using the + * convention of having a year 0, as per ISO 8601). + * That way, we can just mod the year with the number of entries, without + * first having to subtract something from the year. + * + * The heavenly stems last for 2 years, so we just duplicate the entries. + */ + +char * yin_yang [ 2] = {"Yang", "Yin"}; +size_t yin_yang_size = 2; +char * heavenly_stems [10] = {"Metal", "Metal", "Water", "Water", + "Wood", "Wood", "Fire", "Fire", + "Earth", "Earth"}; +size_t heavenly_stems_size = 10; +char * earthly_branches [12] = {"Monkey", "Rooster", "Dog", "Pig", + "Rat", "Ox", "Tiger", "Rabbit", + "Dragon", "Snake", "Horse", "Goat"}; +size_t earthly_branches_size = 12; + + + +int main (void) { + int year; + + while (scanf ("%d", &year) == 1) { + printf ("%s %s %s\n", yin_yang [year % yin_yang_size], + heavenly_stems [year % heavenly_stems_size], + earthly_branches [year % earthly_branches_size]); + } + + return (0); +} |
