diff options
| author | Abigail <abigail@abigail.be> | 2021-07-24 23:11:35 +0200 |
|---|---|---|
| committer | Abigail <abigail@abigail.be> | 2021-07-24 23:11:35 +0200 |
| commit | 62818508c81af33a19e400cce9145e681920eb82 (patch) | |
| tree | f11e3508db11b31712703cf4d5d02640cadc4a3d | |
| parent | 52d0e0851f79fead3bdad2caed25e012a124a8eb (diff) | |
| download | perlweeklychallenge-club-62818508c81af33a19e400cce9145e681920eb82.tar.gz perlweeklychallenge-club-62818508c81af33a19e400cce9145e681920eb82.tar.bz2 perlweeklychallenge-club-62818508c81af33a19e400cce9145e681920eb82.zip | |
Solutions for week 122, part 2, in 8 different languages.
| -rw-r--r-- | challenge-122/abigail/awk/ch-2.awk | 30 | ||||
| -rw-r--r-- | challenge-122/abigail/bash/ch-2.sh | 25 | ||||
| -rw-r--r-- | challenge-122/abigail/c/ch-2.c | 140 | ||||
| -rw-r--r-- | challenge-122/abigail/lua/ch-2.lua | 30 | ||||
| -rw-r--r-- | challenge-122/abigail/node/ch-2.js | 29 | ||||
| -rw-r--r-- | challenge-122/abigail/python/ch-2.py | 25 | ||||
| -rw-r--r-- | challenge-122/abigail/ruby/ch-2.rb | 27 |
7 files changed, 306 insertions, 0 deletions
diff --git a/challenge-122/abigail/awk/ch-2.awk b/challenge-122/abigail/awk/ch-2.awk new file mode 100644 index 0000000000..32ed981320 --- /dev/null +++ b/challenge-122/abigail/awk/ch-2.awk @@ -0,0 +1,30 @@ +#!/usr/bin/awk + +# +# See ../README.md +# + +# +# Run as: awk -f ch-2.awk < input-file +# + +{ + c [0] = 0 + c [1] = 0 + c [2] = 1 + s [2, 0] = "" + + for (i = 3; i < $1 + 3; i ++) { + c [i] = 0 + for (j = 1; j <= 3; j ++) { + for (k = 0; k < c [i - j]; k ++) { + s [i, c [i]] = j " " s [i - j, k] + c [i] ++ + } + } + } + + for (k = 0; k < c [$1 + 2]; k ++) { + print s [$1 + 2, k] + } +} diff --git a/challenge-122/abigail/bash/ch-2.sh b/challenge-122/abigail/bash/ch-2.sh new file mode 100644 index 0000000000..d523f918bf --- /dev/null +++ b/challenge-122/abigail/bash/ch-2.sh @@ -0,0 +1,25 @@ +#!/bin/sh + +# +# See ../README.md +# + +# +# Run as: bash ch-2.sh < input-file +# + +set -f + +declare scores +l=$'\n' +scores[2]=$l + +read n + +for ((i = 3; i < n + 3; i ++)) +do for ((j = 1; j <= 3; j ++)) + do scores[$i]=${scores[$i]}${scores[$((i - j))]//$l/$l$j } + done +done + +echo "${scores[$((n + 2))]/$l/}" diff --git a/challenge-122/abigail/c/ch-2.c b/challenge-122/abigail/c/ch-2.c new file mode 100644 index 0000000000..21fd1dfebb --- /dev/null +++ b/challenge-122/abigail/c/ch-2.c @@ -0,0 +1,140 @@ +# include <stdlib.h> +# include <stdio.h> +# include <string.h> + +/* + * See ../README.md + */ + +/* + * Run as: cc -o ch-2.o ch-2.c; ./ch-2.o < input-file + */ + +typedef long long number; + +int main (void) { + int n; + char *** scores; + number * count; /* Counts the number of possibilities for each + entry in scores */ + size_t ** lengths; /* Length of each entry in scores */ + if (scanf ("%d", &n) != 1) { + perror ("Unexpected input"); + exit (1); + } + + if ((scores = (char ***) malloc ((n + 3) * sizeof (char **))) == NULL) { + perror ("Malloc scores failed"); + exit (1); + } + if ((count = (number *) malloc ((n + 3) * sizeof (number))) == NULL) { + perror ("Malloc count failed"); + exit (1); + } + if ((lengths = (size_t **) malloc ((n + 3) * sizeof (size_t *))) == NULL) { + perror ("Malloc lengths failed"); + exit (1); + } + + /* + * Initialize + */ + count [0] = 0; + count [1] = 0; + count [2] = 1; + + if ((scores [2] = (char **) malloc (sizeof (char *))) == NULL) { + perror ("Malloc failed"); + exit (1); + } + if ((scores [2] [0] = (char *) malloc (sizeof (char))) == NULL) { + perror ("Malloc failed"); + exit (1); + } + scores [2] [0] [0] = '\0'; + + if ((lengths [2] = (size_t *) malloc (sizeof (size_t))) == NULL) { + perror ("Malloc failed"); + exit (1); + } + scores [2] [0] [0] = '\0'; + lengths [2] [0] = 0; + + /* + * Main loop + */ + for (int i = 3; i < n + 3; i ++) { + /* + * First, allocate the right amount of for scores [i] and + * lengths [i] which is going to be + * count [i] = count [i - 1] + count [i - 2] + count [i - 3] + */ + count [i] = count [i - 1] + count [i - 2] + count [i - 3]; + if ((scores [i] = (char **) malloc (count [i] * sizeof (char *))) + == NULL) { + perror ("Malloc failed"); + exit (1); + } + if ((lengths [i] = (size_t *) malloc (count [i] * sizeof (size_t))) + == NULL) { + perror ("Malloc failed"); + exit (1); + } + /* + * Copy strings, with single scores prepended. + */ + number l = 0; + for (int j = 1; j <= 3; j ++) { + for (int k = 0; k < count [i - j]; k ++) { + lengths [i] [l] = 2 + lengths [i - j] [k]; + if ((scores [i] [l] = (char *) malloc + ((lengths [i] [l] + 1) * sizeof (char))) == NULL) { + perror ("Malloc failed"); + exit (1); + } + scores [i] [l] [0] = j + '0'; + scores [i] [l] [1] = ' '; + strncpy (scores [i] [l] + 2, scores [i - j] [k], + lengths [i - j] [k]); + scores [i] [l] [lengths [i] [l]] = '\0'; + + l ++; + } + } + /* + * We now don't need lengths [i - 3] or scores [i - 3] anymore, + * so we can free() up the memory it uses. + */ + if (i - 3 > 1) { + for (int k = 0; k < count [i - 3]; k ++) { + free (scores [i - 3] [k]); + } + free (scores [i - 3]); + free (lengths [i - 3]); + } + } + + /* + * Print the wanted scores + */ + for (int i = 0; i < count [n + 2]; i ++) { + printf ("%s\n", scores [n + 2] [i]); + } + + /* + * free() memory we have not released yet. + */ + for (int i = n; i <= n + 2; i ++) { + for (int k = 0; k < count [i]; k ++) { + free (scores [i] [k]); + } + free (scores [i]); + free (lengths [i]); + } + + free (scores); + free (lengths); + free (count); + + return (0); +} diff --git a/challenge-122/abigail/lua/ch-2.lua b/challenge-122/abigail/lua/ch-2.lua new file mode 100644 index 0000000000..949ff29456 --- /dev/null +++ b/challenge-122/abigail/lua/ch-2.lua @@ -0,0 +1,30 @@ +#!/opt/local/bin/lua + +-- +-- See ../README.md +-- + +-- +-- Run as: lua ch-2.lua < input-file +-- + +local n = io . read ("*number") +local scores = {} + +scores [1] = {} +scores [2] = {} +scores [3] = {} +scores [3] [1] = "" + +for i = 4, n + 3 do + scores [i] = {} + for j = 1, 3 do + for _, v in ipairs (scores [i - j]) do + scores [i] [1 + #scores [i]] = j .. " " .. v + end + end +end + +for i, v in ipairs (scores [n + 3]) do + print (v) +end diff --git a/challenge-122/abigail/node/ch-2.js b/challenge-122/abigail/node/ch-2.js new file mode 100644 index 0000000000..d1e0cd8b0b --- /dev/null +++ b/challenge-122/abigail/node/ch-2.js @@ -0,0 +1,29 @@ +#!/usr/local/bin/node + +// +// See ../README.md +// + +// +// Run as: node ch-2.js < input-file +// + + require ('readline') +. createInterface ({input: process . stdin}) +. on ('line', n => { + let scores = [] + scores [0] = [] + scores [1] = [] + scores [2] = [""] + + n =+ n + for (i = 3; i < n + 3; i ++) { + scores [i] = [] + for (j = 1; j <= 3; j ++) { + scores [i] . push (... scores [i - j] . map (s => j + " " + s)) + } + } + + scores [n + 2] . forEach (s => console . log (s)) +}) + diff --git a/challenge-122/abigail/python/ch-2.py b/challenge-122/abigail/python/ch-2.py new file mode 100644 index 0000000000..2c10b39f25 --- /dev/null +++ b/challenge-122/abigail/python/ch-2.py @@ -0,0 +1,25 @@ +#!/opt/local/bin/python + +# +# See ../README.md +# + +# +# Run as: python ch-2.py < input-file +# + +import sys + +n = int (sys . stdin . readline ()) + +scores = [[], [], [""]] + +for i in range (n): + new = [] + for j in range (3): + for k in scores [len (scores) - 1 - j]: + new . append (str (j + 1) + " " + k) + scores . append (new) + +for score in scores [-1]: + print (score) diff --git a/challenge-122/abigail/ruby/ch-2.rb b/challenge-122/abigail/ruby/ch-2.rb new file mode 100644 index 0000000000..8627a9ae8a --- /dev/null +++ b/challenge-122/abigail/ruby/ch-2.rb @@ -0,0 +1,27 @@ +#!/usr/bin/ruby + +# +# See ../README.md +# + +# +# Run as: ruby ch-2.rb < input-file +# + +n = gets . to_i + +scores = [[], [], [""]] + +for i in 1 .. n do + new = [] + for j in 1 .. 3 do + for k in scores [-j] do + new . push (j . to_s + " " + k) + end + end + scores . push (new) +end + +for score in scores [-1] do + puts (score) +end |
