diff options
| author | 冯昶 <seaker@qq.com> | 2021-10-09 17:07:32 +0800 |
|---|---|---|
| committer | 冯昶 <seaker@qq.com> | 2021-10-09 17:07:32 +0800 |
| commit | 2bd4f4d95ef7c33d45b5a80e69fce40287039986 (patch) | |
| tree | fe4e1606401037346acc4695c91cf2b3dedb2d18 /challenge-132 | |
| parent | 036fe13fa017e7127944ac86f3bbb47c8d822973 (diff) | |
| parent | 4cd8d62dd835b0e16e39aae2169896ec58796b51 (diff) | |
| download | perlweeklychallenge-club-2bd4f4d95ef7c33d45b5a80e69fce40287039986.tar.gz perlweeklychallenge-club-2bd4f4d95ef7c33d45b5a80e69fce40287039986.tar.bz2 perlweeklychallenge-club-2bd4f4d95ef7c33d45b5a80e69fce40287039986.zip | |
Merge remote-tracking branch 'upstream/master'
Diffstat (limited to 'challenge-132')
95 files changed, 4644 insertions, 62 deletions
diff --git a/challenge-132/abigail/README.md b/challenge-132/abigail/README.md index 4c27485f4c..5eef101325 100644 --- a/challenge-132/abigail/README.md +++ b/challenge-132/abigail/README.md @@ -3,10 +3,14 @@ ## Part 1 * [AWK](awk/ch-1.awk) +* [Bash](bash/ch-1.sh) * [C](c/ch-1.c) +* [Lua](lua/ch-1.lua) +* [Node.js](node/ch-1.js) * [Perl](perl/ch-1.pl) +* [Python](python/ch-1.py) +* [Ruby](ruby/ch-1.rb) ## Part 2 -* [AWK](awk/ch-2.awk) * [Perl](perl/ch-2.pl) diff --git a/challenge-132/abigail/awk/ch-1.awk b/challenge-132/abigail/awk/ch-1.awk new file mode 100644 index 0000000000..59a0d20f40 --- /dev/null +++ b/challenge-132/abigail/awk/ch-1.awk @@ -0,0 +1,43 @@ +#!/usr/bin/awk + +# +# See ../README.md +# + +# +# Run as: awk -f ch-1.awk < input-file +# + +# +# Conversion functions from Wikipedia +# + +function g2j (Y, M, D) { + return (int ((1461 * (Y + 4800 + int ((M - 14) / 12))) / 4) + \ + int ((367 * (M - 2 - 12 * int ((M - 14) / 12))) / 12) - \ + int ((3 * int (((Y + 4900 + int ((M - 14) / 12)) / 100))) / 4) + \ + D - 32075) +} + +function j2g (J) { + e = 4 * (J + 1401 + int (int ((4 * J + 274277) / 146097) * 3 / 4) - 38) + 3 + D = int (((5 * (int ((e % 1461) / 4)) + 2) % 153) / 5) + 1 + M = ((int ((5 * (int ((e % 1461) / 4)) + 2) / 153) + 2) % 12) + 1 + Y = int (e / 1461) - 4716 + int ((12 + 2 - M) / 12) + return sprintf ("%04d/%02d/%02d", Y, M, D) +} + +BEGIN { + FS = "/" + TODAY_Y = 2021 + TODAY_M = 9 + TODAY_D = 22 + + julian_today = g2j(TODAY_Y, TODAY_M, TODAY_D) +} + +{ + julian_then = g2j($1, $2, $3) + print j2g(2 * julian_then - julian_today) ", " \ + j2g(2 * julian_today - julian_then) +} diff --git a/challenge-132/abigail/bash/ch-1.sh b/challenge-132/abigail/bash/ch-1.sh new file mode 100644 index 0000000000..eb99113be0 --- /dev/null +++ b/challenge-132/abigail/bash/ch-1.sh @@ -0,0 +1,41 @@ +#!/bin/sh + +# +# See ../README.md +# + +# +# Run as: bash ch-1.sh < input-file +# + +set -f + +IFS="/" + +function g2j () { + local Y=$1 + local M=$2 + local D=$3 + J=$(( ((1461 * (Y + 4800 + (M - 14) / 12)) / 4 + + (367 * (M - 2 - 12 * ((M - 14) / 12))) / 12 - + (3 * ((Y + 4900 + (M - 14) / 12) / 100)) / 4 + D - 32075) )) +} + +function j2g () { + local J=$1 + local e=$(( 4 * (J + 1401 + + (((4 * J + 274277) / 146097) * 3) / 4 - 38) + 3 )) + D=$(( ((5 * ((e % 1461) / 4) + 2) % 153) / 5 + 1 )) + M=$(( (((5 * ((e % 1461) / 4) + 2) / 153 + 2) % 12) + 1 )) + Y=$(( (e / 1461) - 4716 + (12 + 2 - M) / 12 )) +} + +g2j 2021 9 22; julian_today=$J + +while read y m d +do g2j ${y/#0/} ${m/#0/} ${d/#0/}; julian_then=$J + j2g $(( 2 * julian_then - julian_today )) + printf "%04d/%02d/%02d, " $Y $M $D + j2g $(( 2 * julian_today - julian_then )) + printf "%04d/%02d/%02d\n" $Y $M $D +done diff --git a/challenge-132/abigail/blog.txt b/challenge-132/abigail/blog.txt new file mode 100644 index 0000000000..5e79dc06ef --- /dev/null +++ b/challenge-132/abigail/blog.txt @@ -0,0 +1 @@ +https://abigail.github.io/HTML/Perl-Weekly-Challenge/week-132-1.html diff --git a/challenge-132/abigail/blog2.txt b/challenge-132/abigail/blog2.txt new file mode 100644 index 0000000000..9593e5f199 --- /dev/null +++ b/challenge-132/abigail/blog2.txt @@ -0,0 +1 @@ +https://abigail.github.io/HTML/Perl-Weekly-Challenge/week-132-2.html diff --git a/challenge-132/abigail/c/ch-1.c b/challenge-132/abigail/c/ch-1.c new file mode 100644 index 0000000000..1a50d2b8df --- /dev/null +++ b/challenge-132/abigail/c/ch-1.c @@ -0,0 +1,62 @@ +# 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 + */ + +# define idx_Y 0 +# define idx_M 1 +# define idx_D 2 + +typedef unsigned short date_type; + +long g2j (date_type Y, date_type M, date_type D) { + return ((1461 * (Y + 4800 + (M - 14) / 12)) / 4 + + (367 * (M - 2 - 12 * ((M - 14) / 12))) / 12 - + (3 * ((Y + 4900 + (M - 14) / 12) / 100)) / 4 + D - 32075); +} + +void j2g (long J, date_type * date) { + long e = 4 * (J + 1401 + (((4 * J + 274277) / 146097) * 3) / 4 - 38) + 3; + date [idx_D] = ((5 * ((e % 1461) / 4) + 2) % 153) / 5 + 1; + date [idx_M] = (((5 * ((e % 1461) / 4) + 2) / 153 + 2) % 12) + 1; + date [idx_Y] = (e / 1461) - 4716 + (12 + 2 - date [idx_M]) / 12; +} + +unsigned short TODAY [] = {2021, 9, 22}; + +int main (void) { + date_type Y, M, D; + date_type * date_early; + date_type * date_late; + + if ((date_early = (date_type *) malloc (3 * sizeof (date_type))) == NULL) { + perror ("Malloc failed"); + exit (1); + } + if ((date_late = (date_type *) malloc (3 * sizeof (date_type))) == NULL) { + perror ("Malloc failed"); + exit (1); + } + + long julian_today = g2j (TODAY [idx_Y], TODAY [idx_M], TODAY [idx_D]); + + while (scanf ("%hu/%hu/%hu", &Y, &M, &D) == 3) { + long julian_then = g2j (Y, M, D); + j2g (2 * julian_then - julian_today, date_early); + j2g (2 * julian_today - julian_then, date_late); + printf ("%04d/%02d/%02d, %04d/%02d/%02d\n", + date_early [idx_Y], date_early [idx_M], date_early [idx_D], + date_late [idx_Y], date_late [idx_M], date_late [idx_D]); + } + free (date_early); + free (date_late); + + return (0); +} diff --git a/challenge-132/abigail/lua/ch-1.lua b/challenge-132/abigail/lua/ch-1.lua new file mode 100644 index 0000000000..a13cd4cf89 --- /dev/null +++ b/challenge-132/abigail/lua/ch-1.lua @@ -0,0 +1,45 @@ +#!/opt/local/bin/lua + +-- +-- See ../README.md +-- + +-- +-- Run as: lua ch-1.lua < input-file +-- + +function int (x) + if x > 0 then + return math . floor (x) + else + return math . ceil (x) + end +end + +function g2j (Y, M, D) + return (int ((1461 * (Y + 4800 + int ((M - 14) / 12))) / 4) + + int ((367 * (M - 2 - 12 * int ((M - 14) / 12))) / 12) - + int ((3 * int (((Y + 4900 + int ((M - 14) / 12)) / 100))) / 4) + + D - 32075) +end + +function j2g (J) + local e = 4 * (J + 1401 + + int (int ((4 * J + 274277) / 146097) * 3 / 4) - 38) + 3 + local D = int (((5 * (int ((e % 1461) / 4)) + 2) % 153) / 5) + 1 + local M = ((int ((5 * (int ((e % 1461) / 4)) + 2) / 153) + 2) % 12) + 1 + local Y = int (e / 1461) - 4716 + int ((12 + 2 - M) / 12) + return Y, M, D +end + +local julian_today = g2j (2021, 9, 22) +local output_format = "%04d/%02d/%02d, %04d/%02d/%02d\n" + +for line in io . lines () do + local _, _, Y, M, D = line : find ("([0-9]+)/([0-9]+)/([0-9]+)") + local julian_then = g2j (Y, M, D) + local Y1, M1, D1 = j2g (2 * julian_then - julian_today) + local Y2, M2, D2 = j2g (2 * julian_today - julian_then) + + io . write (output_format : format (Y1, M1, D1, Y2, M2, D2)) +end diff --git a/challenge-132/abigail/node/ch-1.js b/challenge-132/abigail/node/ch-1.js new file mode 100644 index 0000000000..69f4a996b9 --- /dev/null +++ b/challenge-132/abigail/node/ch-1.js @@ -0,0 +1,68 @@ +#!/usr/local/bin/node + +// +// See ../README.md +// + +// +// Run as: node ch-1.js < input |
