From da5ece1804223a51722105d1e269fb3d78c863c6 Mon Sep 17 00:00:00 2001 From: Abigail Date: Mon, 7 Feb 2022 11:44:50 +0100 Subject: Week 151: Tests --- challenge-151/abigail/t/ctest.ini | 9 +++++++++ challenge-151/abigail/t/input-1-1 | 2 ++ challenge-151/abigail/t/input-2-1 | 2 ++ challenge-151/abigail/t/output-1-1.exp | 2 ++ challenge-151/abigail/t/output-2-1.exp | 2 ++ 5 files changed, 17 insertions(+) create mode 100644 challenge-151/abigail/t/ctest.ini create mode 100644 challenge-151/abigail/t/input-1-1 create mode 100644 challenge-151/abigail/t/input-2-1 create mode 100644 challenge-151/abigail/t/output-1-1.exp create mode 100644 challenge-151/abigail/t/output-2-1.exp diff --git a/challenge-151/abigail/t/ctest.ini b/challenge-151/abigail/t/ctest.ini new file mode 100644 index 0000000000..0a8cb97d18 --- /dev/null +++ b/challenge-151/abigail/t/ctest.ini @@ -0,0 +1,9 @@ +# +# Configuration file for running tests, using ctest. +# See https://github.com/Abigail/Misc/blob/master/ctest +# + +[names] +1-1 = Given Examples +2-1 = Given Examples + diff --git a/challenge-151/abigail/t/input-1-1 b/challenge-151/abigail/t/input-1-1 new file mode 100644 index 0000000000..61fb9ed0dc --- /dev/null +++ b/challenge-151/abigail/t/input-1-1 @@ -0,0 +1,2 @@ +1 | 2 3 | 4 5 +1 | 2 3 | 4 * * 5 | * 6 diff --git a/challenge-151/abigail/t/input-2-1 b/challenge-151/abigail/t/input-2-1 new file mode 100644 index 0000000000..14b0f0f2c6 --- /dev/null +++ b/challenge-151/abigail/t/input-2-1 @@ -0,0 +1,2 @@ +2 4 5 +4 2 3 6 5 3 diff --git a/challenge-151/abigail/t/output-1-1.exp b/challenge-151/abigail/t/output-1-1.exp new file mode 100644 index 0000000000..4792e70f33 --- /dev/null +++ b/challenge-151/abigail/t/output-1-1.exp @@ -0,0 +1,2 @@ +2 +3 diff --git a/challenge-151/abigail/t/output-2-1.exp b/challenge-151/abigail/t/output-2-1.exp new file mode 100644 index 0000000000..dd80f6a91d --- /dev/null +++ b/challenge-151/abigail/t/output-2-1.exp @@ -0,0 +1,2 @@ +7 +13 -- cgit From 03fe3f90231fcea43fb5911dc722d9c788cac92d Mon Sep 17 00:00:00 2001 From: Abigail Date: Mon, 7 Feb 2022 13:28:46 +0100 Subject: Week 151: Perl solutions --- challenge-151/abigail/perl/ch-1.pl | 39 ++++++++++++++++++++++++++++++++++++++ challenge-151/abigail/perl/ch-2.pl | 37 ++++++++++++++++++++++++++++++++++++ 2 files changed, 76 insertions(+) create mode 100644 challenge-151/abigail/perl/ch-1.pl create mode 100644 challenge-151/abigail/perl/ch-2.pl diff --git a/challenge-151/abigail/perl/ch-1.pl b/challenge-151/abigail/perl/ch-1.pl new file mode 100644 index 0000000000..73296d77c3 --- /dev/null +++ b/challenge-151/abigail/perl/ch-1.pl @@ -0,0 +1,39 @@ +#!/opt/perl/bin/perl + +use 5.032; + +use strict; +use warnings; +no warnings 'syntax'; + +use experimental 'signatures'; +use experimental 'lexical_subs'; + +# +# See https://theweeklychallenge.org/blog/perl-weekly-challenge-151 +# + +# +# Run as: perl ch-1.pl < input-file +# + +# +# We'll store the tree in a triangular 2-d array, with +# the children on node on position [$d, $k] on positions +# [$d + 1, 2 * $k] and [$d + 1, 2 * $k + 1]. +# +# Finding the first node without children is trivial. +# +TREE: while (<>) { + chomp; + my @tree = map {[map {$_ ne '*'} /\S+/g]} split /\|/; + foreach my $d (keys @tree) { + foreach my $i (keys @{$tree [$d]}) { + if ($tree [$d] [$i] && !$tree [$d + 1] [2 * $i] + && !$tree [$d + 1] [2 * $i + 1]) { + say $d + 1; + next TREE; + } + } + } +} diff --git a/challenge-151/abigail/perl/ch-2.pl b/challenge-151/abigail/perl/ch-2.pl new file mode 100644 index 0000000000..263bede352 --- /dev/null +++ b/challenge-151/abigail/perl/ch-2.pl @@ -0,0 +1,37 @@ +#!/opt/perl/bin/perl + +use 5.032; + +use strict; +use warnings; +no warnings 'syntax'; + +use experimental 'signatures'; +use experimental 'lexical_subs'; + +# +# See https://theweeklychallenge.org/blog/perl-weekly-challenge-151 +# + +# +# Run as: perl ch-2.pl < input-file +# + +use List::Util qw [max]; + +# +# An algorithm we've seen many times before. For each house, we either +# rob it, or skip it. And we always have to skip the next house. +# Skipping more than two houses in succession is never a good idea, but +# work we'll do extra in the beginning (by skipping more than two house) +# we get back later on, as we're caching all results. +# +sub best; +sub best ($vals, $i = 0) { + state $cache; + $i or $cache = []; + $$cache [$i] ||= $$vals [$i] + max 0, map {best $vals, $_} $i + 2 .. $#$vals +} + + +say best [/[0-9]+/g] while <>; -- cgit From 1a21a2b719818bb32eaa46d14d91379edc7836ce Mon Sep 17 00:00:00 2001 From: Abigail Date: Mon, 7 Feb 2022 14:10:22 +0100 Subject: Week 151: AWK solutions --- challenge-151/abigail/awk/ch-1.awk | 42 ++++++++++++++++++++++++++++++++++++++ challenge-151/abigail/awk/ch-2.awk | 28 +++++++++++++++++++++++++ 2 files changed, 70 insertions(+) create mode 100644 challenge-151/abigail/awk/ch-1.awk create mode 100644 challenge-151/abigail/awk/ch-2.awk diff --git a/challenge-151/abigail/awk/ch-1.awk b/challenge-151/abigail/awk/ch-1.awk new file mode 100644 index 0000000000..314efa9a0a --- /dev/null +++ b/challenge-151/abigail/awk/ch-1.awk @@ -0,0 +1,42 @@ +#!/usr/bin/awk + +# +# See https://theweeklychallenge.org/blog/perl-weekly-challenge-151 +# + +# +# Run as: awk -f ch-1.awk < input-file +# + +{ + # + # Read in tree + # + delete tree + D = 0 + i = 0 + for (k = 0; k < NF; k ++) { + if ($k == "|") { + D ++ + i = 0 + } + else { + tree [D, i ++] = $k == "*" ? 0 : 1 + } + } + + # + # Find first node without children + # + for (d = 0; d <= D; d ++) { + for (i = 0; i < 2 ^ d; i ++) { + if (tree [d, i] && !tree [d + 1, 2 * i] && + !tree [d + 1, 2 * i + 1]) { + print d + 1 + next + } + } + } +} + + diff --git a/challenge-151/abigail/awk/ch-2.awk b/challenge-151/abigail/awk/ch-2.awk new file mode 100644 index 0000000000..3eb76a8e05 --- /dev/null +++ b/challenge-151/abigail/awk/ch-2.awk @@ -0,0 +1,28 @@ +#!/usr/bin/awk + +# +# See https://theweeklychallenge.org/blog/perl-weekly-challenge-151 +# + +# +# Run as: awk -f ch-2.awk < input-file +# + +function best (i, max, sum, k) { + if (!(i in cache)) { + max = 0 + for (k = i + 2; k <= NF; k ++) { + sum = best(k) + if (sum > max) { + max = sum + } + } + cache [i] = $i + max + } + return cache [i] +} + +{ + delete cache + print best(1) +} -- cgit From 98a74265eb245f20dcc038edfca401e67d6cfd4b Mon Sep 17 00:00:00 2001 From: Abigail Date: Mon, 7 Feb 2022 18:10:56 +0100 Subject: Week 151: No recursion for part 2 --- challenge-151/abigail/awk/ch-2.awk | 26 ++++++++++++-------------- challenge-151/abigail/perl/ch-2.pl | 38 ++++++++++++++++++++++++++++---------- 2 files changed, 40 insertions(+), 24 deletions(-) diff --git a/challenge-151/abigail/awk/ch-2.awk b/challenge-151/abigail/awk/ch-2.awk index 3eb76a8e05..73d9b19812 100644 --- a/challenge-151/abigail/awk/ch-2.awk +++ b/challenge-151/abigail/awk/ch-2.awk @@ -8,21 +8,19 @@ # Run as: awk -f ch-2.awk < input-file # -function best (i, max, sum, k) { - if (!(i in cache)) { - max = 0 - for (k = i + 2; k <= NF; k ++) { - sum = best(k) - if (sum > max) { - max = sum - } - } - cache [i] = $i + max - } - return cache [i] +function max (a, b) { + return a < b ? b : a } { - delete cache - print best(1) + delete best + for (i = NF; i > 0; i --) { + best [i] = NF <= 2 ? $i \ + : i == NF ? $i \ + : i == 1 ? $i + best [i + 2] \ + : i == NF - 1 ? max($i, best [i + 1]) \ + : max($i + best [i + 2], best [i + 1]) + } + print best [1] } + diff --git a/challenge-151/abigail/perl/ch-2.pl b/challenge-151/abigail/perl/ch-2.pl index 263bede352..5f5519381f 100644 --- a/challenge-151/abigail/perl/ch-2.pl +++ b/challenge-151/abigail/perl/ch-2.pl @@ -20,17 +20,35 @@ use experimental 'lexical_subs'; use List::Util qw [max]; # -# An algorithm we've seen many times before. For each house, we either -# rob it, or skip it. And we always have to skip the next house. -# Skipping more than two houses in succession is never a good idea, but -# work we'll do extra in the beginning (by skipping more than two house) -# we get back later on, as we're caching all results. +# We'll calculate the best way to rob the houses by working backwards. +# For each house, there are two options: rob the house, or skip it. +# The maximum value we get for robbing the house is the sum of +# the value of the current house, plus the maximum we can get +# if we started two houses down. The maximum value we can get by +# not robbing this house is the best we can get by starting from +# the next house. # -sub best; -sub best ($vals, $i = 0) { - state $cache; - $i or $cache = []; - $$cache [$i] ||= $$vals [$i] + max 0, map {best $vals, $_} $i + 2 .. $#$vals +# We need some fiddling around the edges: +# - Maximizing the value starting from the last house is by robbing it. +# - Maximizing the value starting from the penultimate house is +# robbing the most valuable of the last two houses. +# - If there are only two houses, rob the current house. +# - The first house should always be robbed (a requirement); the +# maximum value which can be achieved we get by starting from the +# third house, adding the value of the first. +# + +sub best ($houses) { + my @best; + foreach my $i (reverse keys @$houses) { + $best [$i] = @$houses < 2 ? $$houses [$i] + : $i == $#$houses ? $$houses [$i] + : $i == 0 ? $$houses [$i] + $best [$i + 2] + : $i == $#$houses - 1 ? max $$houses [$i], $best [$i + 1] + : max $$houses [$i] + $best [$i + 2], + $best [$i + 1]; + } + $best [0]; } -- cgit From b7cd81ad7d65fa804a146fbedb56dbf76b8d3a95 Mon Sep 17 00:00:00 2001 From: Abigail Date: Mon, 7 Feb 2022 18:40:11 +0100 Subject: Week 151: Bash solutions --- challenge-151/abigail/bash/ch-1.sh | 41 +++++++++++++++++++++++++++++++++ challenge-151/abigail/bash/ch-2.sh | 46 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 87 insertions(+) create mode 100644 challenge-151/abigail/bash/ch-1.sh create mode 100644 challenge-151/abigail/bash/ch-2.sh diff --git a/challenge-151/abigail/bash/ch-1.sh b/challenge-151/abigail/bash/ch-1.sh new file mode 100644 index 0000000000..eff4fc05e6 --- /dev/null +++ b/challenge-151/abigail/bash/ch-1.sh @@ -0,0 +1,41 @@ +#!/bin/sh + +# +# See https://theweeklychallenge.org/blog/perl-weekly-challenge-151 +# + +# +# Run as: bash ch-1.sh < input-file +# + +set -f + +declare -A tree + +while read -a tokens +do tree=() + D=0 + i=0 + for token in ${tokens[@]} + do if [ $token == "|" ] + then D=$((D + 1)) + i=0 + else key=$D,$i + if [ $token != "*" ] + then tree[$key]=1 + fi + i=$((i + 1)) + fi + done + + for ((d = 0; d <= D; d ++)) + do for ((i = 0; i < 2 ** d; i ++)) + do if [[ -v tree[$d,$i] ]] && \ + [[ ! -v tree[$((d + 1)),$((2 * $i))] ]] && \ + [[ ! -v tree[$((d + 1)),$((2 * $i + 1))] ]] + then echo $((d + 1)) + break 2 + fi + done + done +done diff --git a/challenge-151/abigail/bash/ch-2.sh b/challenge-151/abigail/bash/ch-2.sh new file mode 100644 index 0000000000..0b8f213fca --- /dev/null +++ b/challenge-151/abigail/bash/ch-2.sh @@ -0,0 +1,46 @@ +#!/bin/sh + +# +# See https://theweeklychallenge.org/blog/perl-weekly-challenge-151 +# + +# +# Run as: bash ch-2.sh < input-file +# + +set -f + +declare -a best + +while read -a houses +do if [[ ${#houses[@]} -lt 2 ]] + then echo ${houses[0]} + continue + fi + + size=${#houses[@]} + for ((i = size - 1; i >= 0; i --)) + do ((i1 = i + 1)) + ((i2 = i + 2)) + if ((i == size - 1)) + then best[$i]=${houses[$i]} + continue + fi + + if ((i == 0)) + then best[$i]=$((${houses[$i]} + ${best[$i2]})) + continue + fi + + if ((i == size - 2)) + then val1=${houses[$i]} + val2=${best[$i1]} + else val1=$((${houses[$i]} + ${best[$i2]})) + val2=${best[$i1]} + fi + + best[$i]=$((val1 < val2 ? val2 : val1)) + done + + echo ${best[0]} +done -- cgit From e270077b999c4129874da382d77addc76460aa17 Mon Sep 17 00:00:00 2001 From: Abigail Date: Mon, 7 Feb 2022 20:33:23 +0100 Subject: Week 151: C solutions --- challenge-151/abigail/c/ch-1.c | 96 ++++++++++++++++++++++++++++++++++++++++++ challenge-151/abigail/c/ch-2.c | 62 +++++++++++++++++++++++++++ 2 files changed, 158 insertions(+) create mode 100644 challenge-151/abigail/c/ch-1.c create mode 100644 challenge-151/abigail/c/ch-2.c diff --git a/challenge-151/abigail/c/ch-1.c b/challenge-151/abigail/c/ch-1.c new file mode 100644 index 0000000000..e4487ddaec --- /dev/null +++ b/challenge-151/abigail/c/ch-1.c @@ -0,0 +1,96 @@ +# include +# include +# include +# include +# include + +/* + * See https://theweeklychallenge.org/blog/perl-weekly-challenge-151 + */ + +/* + * Run as: cc -o ch-1.o ch-1.c; ./ch-1.o < input-file + */ + +int main (void) { + char * line = NULL; + size_t len = 0; + size_t str_len; + + while ((str_len = getline (&line, &len, stdin)) != -1) { + char * line_ptr = line; + /* + * Count the number of '|' characters; this determines + * how many elements to allocate for our tree. + */ + int size = 3; + while (* line_ptr) { + if (* line_ptr ++ == '|') { + size = 2 * size + 1; + } + } + bool * tree; + if ((tree = (bool *) malloc (size * sizeof (bool))) == NULL) { + perror ("Malloc tree failed"); + exit (1); + } + for (size_t i = 0; i < size; i ++) { + tree [i] = false; + } + + int D = 0; + int i = 0; + size_t offset = 0; + line_ptr = line; + /* + * Skip leading spaces + */ + while (* line_ptr && isspace (* line_ptr)) { + line_ptr ++; + } + while (* line_ptr) { + if (* line_ptr == '|') { + D ++; + i = 0; + offset = 2 * offset + 1; + line_ptr ++; + continue; + } + if (* line_ptr == '*') { + i ++; + line_ptr ++; + continue; + } + if (isspace (* line_ptr)) { + while (* line_ptr && isspace (* line_ptr)) { + line_ptr ++; + } + continue; + } + tree [offset + i] = true; + i ++; + while (* line_ptr && !isspace (* line_ptr) && * line_ptr != '|') { + line_ptr ++; + } + } + + int width = 1; + int k = 0; + bool done = false; + for (int d = 0; d <= D && !done; d ++) { + for (int i = 0; i < width && !done; i ++) { + if (tree [k] && !tree [2 * k + 1] && !tree [2 * k + 2]) { + printf ("%d\n", d + 1); + done = true; + } + k ++; + } + width *= 2; + } + + free (tree); + } + free (line); + + return (0); +} diff --git a/challenge-151/abigail/c/ch-2.c b/challenge-151/abigail/c/ch-2.c new file mode 100644 index 0000000000..c2a62f6c92 --- /dev/null +++ b/challenge-151/abigail/c/ch-2.c @@ -0,0 +1,62 @@ +# include +# include +# include + +/* + * See https://theweeklychallenge.org/blog/perl-weekly-challenge-151 + */ + +/* + * Run as: cc -o ch-2.o ch-2.c; ./ch-2.o < input-file + */ + +int max (int a, int b) { + return a < b ? b : a; +} + +int main (void) { + char * line = NULL; + size_t len = 0; + size_t str_len; + + while ((str_len = getline (&line, &len, stdin)) != -1) { + char * line_ptr = line; + int * houses = NULL; + int * best = NULL; + int val; + int offset; + int nr_of_houses = 0; + + while (sscanf (line_ptr, "%d%n", &val, &offset) == 1) { + if ((houses = (int *) + realloc (houses, ++ nr_of_houses * sizeof (int))) == NULL) { + perror ("Recalloc failed"); + exit (1); + } + houses [nr_of_houses - 1] = val; + line_ptr += offset; + } + + if ((best = (int *) malloc (nr_of_houses * sizeof (int))) == NULL) { + perror ("Malloc failed"); + exit (1); + } + + for (int i = nr_of_houses - 1; i >= 0; i --) { + best [i] = nr_of_houses < 2 ? houses [i] + : i == nr_of_houses - 1 ? houses [i] + : i == 0 ? houses [i] + best [i + 2] + : i == nr_of_houses - 2 ? max (houses [i], best [i + 1]) + : max (houses [i] + best [i + 2], + best [i + 1]); + } + + printf ("%d\n", best [0]); + + free (houses); + free (best); + } + free (line); + + return (0); +} -- cgit From f1bc02f8fcc156205b2a8ccabaf0063f32ab8106 Mon Sep 17 00:00:00 2001 From: Abigail Date: Mon, 7 Feb 2022 22:37:05 +0100 Subject: Week 151: Lua solutions --- challenge-151/abigail/lua/ch-1.lua | 45 ++++++++++++++++++++++++++++++++++++++ challenge-151/abigail/lua/ch-2.lua | 33 ++++++++++++++++++++++++++++ 2 files changed, 78 insertions(+) create mode 100644 challenge-151/abigail/lua/ch-1.lua create mode 100644 challenge-151/abigail/lua/ch-2.lua diff --git a/challenge-151/abigail/lua/ch-1.lua b/challenge-151/abigail/lua/ch-1.lua new file mode 100644 index 0000000000..1d8664060f --- /dev/null +++ b/challenge-151/abigail/lua/ch-1.lua @@ -0,0 +1,45 @@ +#!/opt/local/bin/lua + +-- +-- See https://theweeklychallenge.org/blog/perl-weekly-challenge-151 +-- + +-- +-- Run as: lua ch-1.lua < input-file +-- + +for line in io . lines () do + local tree = {} + local d = 1 + local i = 1 + tree [d] = {} + for token in line : gmatch ("(%S+)") do + if token == "|" then + d = d + 1 + i = 1 + tree [d] = {} + goto end_loop + end + if token == "*" then + i = i + 1 + goto end_loop + end + + tree [d] [i] = 1 + i = i + 1 + + ::end_loop:: + end + + for d, row in ipairs (tree) do + for i, _ in pairs (row) do + if not tree [d + 1] or + not tree [d + 1] [2 * i - 1] and not tree [d + 1] [2 * i] then + print (d) + goto end_main + end + end + end + + ::end_main:: +end diff --git a/challenge-151/abigail/lua/ch-2.lua b/challenge-151/abigail/lua/ch-2.lua new file mode 100644 index 0000000000..98df0aae1d --- /dev/null +++ b/challenge-151/abigail/lua/ch-2.lua @@ -0,0 +1,33 @@ +#!/opt/local/bin/lua + +-- +-- See https://theweeklychallenge.org/blog/perl-weekly-challenge-151 +-- + +-- +-- Run as: lua ch-2.lua < input-file +-- + +for line in io . lines () do + local houses = {} + local best = {} + for val in line : gmatch ("%d+") do + houses [#houses + 1] = val + end + + for i = #houses, 1, -1 do + if 2 >= #houses then + best [i] = houses [i] + elseif i == #houses then + best [i] = houses [i] + elseif i == 0 then + best [i] = houses [i] + best [i + 2] + elseif i == #houses - 1 then + best [i] = math . max (houses [i], best [i + 1]) + else + best [i] = math . max (houses [i] + best [i + 2], + best [i + 1]) + end + end + print (best [1]) +end -- cgit From dfe3d0c53a5e090bb2784fb9487463656fa72387 Mon Sep 17 00:00:00 2001 From: Abigail Date: Mon, 7 Feb 2022 23:07:38 +0100 Subject: Week 151: Node.js solutions --- challenge-151/abigail/node/ch-1.js | 33 +++++++++++++++++++++++++++++++++ challenge-151/abigail/node/ch-2.js | 26 ++++++++++++++++++++++++++ 2 files changed, 59 insertions(+) create mode 100644 challenge-151/abigail/node/ch-1.js create mode 100644 challenge-151/abigail/node/ch-2.js diff --git a/challenge-151/abigail/node/ch-1.js b/challenge-151/abigail/node/ch-1.js new file mode 100644 index 0000000000..bf67eda21e --- /dev/null +++ b/challenge-151/abigail/node/ch-1.js @@ -0,0 +1,33 @@ +#!/usr/local/bin/node + +// +// See https://theweeklychallenge.org/blog/perl-weekly-challenge-151 +// + +// +// Run as: node ch-1.js < input-file +// + + require ('readline') +. createInterface ({input: process . stdin}) +. on ('line', line => { + let tree = line . split ("\|") . map (row => row . trim () . split (/ +/)) + + for (let d = 0; d < tree . length; d ++) { + if (d == tree . length - 1) { + return + } + let c_row = tree [d] + let n_row = tree [d + 1] + for (let i = 0; i < c_row . length; i ++) { + let ch1 = 2 * i + let ch2 = 2 * i + 1 + if (c_row [i] != "*" && + (ch1 >= n_row . length || n_row [ch1] == "*") && + (ch2 >= n_row . length || n_row [ch2] == "*")) { + console . log (d + 1) + return; + } + } + } +}) diff --git a/challenge-151/abigail/node/ch-2.js b/challenge-151/abigail/node/ch-2.js new file mode 100644 index 0000000000..f4d698ec49 --- /dev/null +++ b/challenge-151/abigail/node/ch-2.js @@ -0,0 +1,26 @@ +#!/usr/local/bin/node + +// +// See https://theweeklychallenge.org/blog/perl-weekly-challenge-151 +// + +// +// Run as: node ch-2.js < input-file +// + + require ('readline') +. createInterface ({input: process . stdin}) +. on ('line', line => { + let houses = line . trim () . split (/ +/) . map (n => +n) + let best = [] + for (let i = houses . length - 1; i >= 0; i --) { + best [i] = + 2 > houses . length ? houses [i] + : i == houses . length - 1 ? houses [i] + : i == 0 ? houses [i] + best [i + 2] + : i == houses . length - 2 ? Math . max (houses [i], best [i + 1]) + : Math . max (houses [i] + best [i + 2], + best [i + 1]) + } + console . log (best [0]) +}) -- cgit From 5045026f0fe4bf7ba93d00350f8bb6bbde68f5be Mon Sep 17 00:00:00 2001 From: Abigail Date: Tue, 8 Feb 2022 11:51:15 +0100 Subject: Week 151: Simplified algorithm for part 2. By adding two dummy 0's to the "best" array, and not bothering the calculate the best values for the first two houses, we don't have a case study inside the main loop. --- challenge-151/abigail/awk/ch-2.awk | 12 +++++------- challenge-151/abigail/bash/ch-2.sh | 34 +++++++--------------------------- challenge-151/abigail/c/ch-2.c | 17 ++++++++--------- challenge-151/abigail/lua/ch-2.lua | 21 +++++++-------------- challenge-151/abigail/node/ch-2.js | 15 ++++++--------- challenge-151/abigail/perl/ch-2.pl | 27 ++++++++++----------------- 6 files changed, 43 insertions(+), 83 deletions(-) diff --git a/challenge-151/abigail/awk/ch-2.awk b/challenge-151/abigail/awk/ch-2.awk index 73d9b19812..65428f81e8 100644 --- a/challenge-151/abigail/awk/ch-2.awk +++ b/challenge-151/abigail/awk/ch-2.awk @@ -14,13 +14,11 @@ function max (a, b) { { delete best - for (i = NF; i > 0; i --) { - best [i] = NF <= 2 ? $i \ - : i == NF ? $i \ - : i == 1 ? $i + best [i + 2] \ - : i == NF - 1 ? max($i, best [i + 1]) \ - : max($i + best [i + 2], best [i + 1]) + best [NF + 1] = 0 + best [NF + 2] = 0 + for (i = NF; i > 2; i --) { + best [i] = max($i + best [i + 2], best [i + 1]) } - print best [1] + print $1 + best [3] } diff --git a/challenge-151/abigail/bash/ch-2.sh b/challenge-151/abigail/bash/ch-2.sh index 0b8f213fca..6a9eceec29 100644 --- a/challenge-151/abigail/bash/ch-2.sh +++ b/challenge-151/abigail/bash/ch-2.sh @@ -13,34 +13,14 @@ set -f declare -a best while read -a houses -do if [[ ${#houses[@]} -lt 2 ]] - then echo ${houses[0]} - continue - fi - - size=${#houses[@]} - for ((i = size - 1; i >= 0; i --)) - do ((i1 = i + 1)) - ((i2 = i + 2)) - if ((i == size - 1)) - then best[$i]=${houses[$i]} - continue - fi - - if ((i == 0)) - then best[$i]=$((${houses[$i]} + ${best[$i2]})) - continue - fi - - if ((i == size - 2)) - then val1=${houses[$i]} - val2=${best[$i1]} - else val1=$((${houses[$i]} + ${best[$i2]})) - val2=${best[$i1]} - fi - +do size=${#houses[@]} + best[$((size + 0))]=0 + best[$((size + 1))]=0 + for ((i = size - 1; i >= 2; i --)) + do ((val1 = ${houses[$i]} + ${best[$((i + 2))]})) + ((val2 = ${best[$((i + 1))]})) best[$i]=$((val1 < val2 ? val2 : val1)) done - echo ${best[0]} + echo $((${houses[0]} + ${best[2]})) done diff --git a/challenge-151/abigail/c/ch-2.c b/challenge-151/abigail/c/ch-2.c index c2a62f6c92..34e7e24ac1 100644 --- a/challenge-151/abigail/c/ch-2.c +++ b/challenge-151/abigail/c/ch-2.c @@ -37,21 +37,20 @@ int main (void) { line_ptr += offset; } - if ((best = (int *) malloc (nr_of_houses * sizeof (int))) == NULL) { + if ((best = (int *) + malloc ((nr_of_houses + 2) * sizeof (int))) == NULL) { perror ("Malloc failed"); exit (1); } - for (int i = nr_of_houses - 1; i >= 0; i --) { - best [i] = nr_of_houses < 2 ? houses [i] - : i == nr_of_houses - 1 ? houses [i] - : i == 0 ? houses [i] + best [i + 2] - : i == nr_of_houses - 2 ? max (houses [i], best [i + 1]) - : max (houses [i] + best [i + 2], - best [i + 1]); + best [nr_of_houses + 0] = 0; + best [nr_of_houses + 1] = 0; + + for (int i = nr_of_houses - 1; i >= 2; i --) { + best [i] = max (houses [i] + best [i + 2], best [i + 1]); } - printf ("%d\n", best [0]); + printf ("%d\n", houses [0] + best [2]); free (houses); free (best); diff --git a/challenge-151/abigail/lua/ch-2.lua b/challenge-151/abigail/lua/ch-2.lua index 98df0aae1d..0e644d0a6c 100644 --- a/challenge-151/abigail/lua/ch-2.lua +++ b/challenge-151/abigail/lua/ch-2.lua @@ -15,19 +15,12 @@ for line in io . lines () do houses [#houses + 1] = val end - for i = #houses, 1, -1 do - if 2 >= #houses then - best [i] = houses [i] - elseif i == #houses then - best [i] = houses [i] - elseif i == 0 then - best [i] = houses [i] + best [i + 2] - elseif i == #houses - 1 then - best [i] = math . max (houses [i], best [i + 1]) - else - best [i] = math . max (houses [i] + best [i + 2], - best [i + 1]) - end + best [#houses + 1] = 0 + best [#houses + 2] = 0 + + for i = #houses, 3, -1 do + best [i] = math . max (houses [i] + best [i + 2], best [i + 1]) end - print (best [1]) + + print (houses [1] + best [3]) end diff --git a/challenge-151/abigail/node/ch-2.js b/challenge-151/abigail/node/ch-2.js index f4d698ec49..079c61e0ce 100644 --- a/challenge-151/abigail/node/ch-2.js +++ b/challenge-151/abigail/node/ch-2.js @@ -13,14 +13,11 @@ . on ('line', line => { let houses = line . trim () . split (/ +/) . map (n => +n) let best = [] - for (let i = houses . length - 1; i >= 0; i --) { - best [i] = - 2 > houses . length ? houses [i] - : i == houses . length - 1 ? houses [i] - : i == 0 ? houses [i] + best [i + 2] - : i == houses . length - 2 ? Math . max (houses [i], best [i + 1]) - : Math . max (houses [i] + best [i + 2], - best [i + 1]) + let size = houses . length + best [size + 0] = 0 + best [size + 1] = 0 + for (let i = size - 1; i >= 2; i --) { + best [i] = Math . max (houses [i] + best [i + 2], best [i + 1]) } - console . log (best [0]) + console . log (houses [0] + best [2]) }) diff --git a/challenge-151/abigail/perl/ch-2.pl b/challenge-151/abigail/perl/ch-2.pl index 5f5519381f..9c1e1d3b60 100644 --- a/challenge-151/abigail/perl/ch-2.pl +++ b/challenge-151/abigail/perl/ch-2.pl @@ -26,29 +26,22 @@ use List::Util qw [max]; # the value of the current house, plus the maximum we can get # if we started two houses down. The maximum value we can get by # not robbing this house is the best we can get by starting from -# the next house. +# the next house. We'll add two empty houses to make everything work +# out nicely. # -# We need some fiddling around the edges: -# - Maximizing the value starting from the last house is by robbing it. -# - Maximizing the value starting from the penultimate house is -# robbing the most valuable of the last two houses. -# - If there are only two houses, rob the current house. -# - The first house should always be robbed (a requirement); the -# maximum value which can be achieved we get by starting from the -# third house, adding the value of the first. +# Note that we don't have to calculate the best option starting +# from the second house, as we will always skip the second house. +# And we must always pick the first house. # sub best ($houses) { my @best; - foreach my $i (reverse keys @$houses) { - $best [$i] = @$houses < 2 ? $$houses [$i] - : $i == $#$houses ? $$houses [$i] - : $i == 0 ? $$houses [$i] + $best [$i + 2] - : $i == $#$houses - 1 ? max $$houses [$i], $best [$i + 1] - : max $$houses [$i] + $best [$i + 2], - $best [$i + 1]; + $best [$#$houses + 1] = 0; + $best [$#$houses + 2] = 0; + for (my $i = $#$houses; $i >= 2; $i --) { + $best [$i] = max $$houses [$i] + $best [$i + 2], $best [$i + 1]; } - $best [0]; + $$houses [0] + $best [2]; } -- cgit From acbd8f6b2a7471edbe77f9ea08ab82db66e1c304 Mon Sep 17 00:00:00 2001 From: Abigail Date: Tue, 8 Feb 2022 15:12:14 +0100 Subject: Week 151: Python solutions --- challenge-151/abigail/python/ch-1.py | 32 ++++++++++++++++++++++++++++++++ challenge-151/abigail/python/ch-2.py | 18 ++++++++++++++++++ 2 files changed, 50 insertions(+) create mode 100644 challenge-151/abigail/python/ch-1.py create mode 100644 challenge-151/abigail/python/ch-2.py diff --git a/challenge-151/abigail/python/ch-1.py b/challenge-151/abigail/python/ch-1.py new file mode 100644 index 0000000000..12dec88d86 --- /dev/null +++ b/challenge-151/abigail/python/ch-1.py @@ -0,0 +1,32 @@ +#!/usr/local/bin/python3 + +# +# See https://theweeklychallenge.org/blog/perl-weekly-challenge-151 +# + +# +# Run as: python ch-1.py < input-file +# + +import fileinput + +for line in fileinput . input (): + tree = list (map (lambda row: row . strip () . split (), + line . strip () . split ("|"))) + for d in range (len (tree)): + if (d == len (tree) - 1): + print (d + 1) + break + done = False + for i in range (len (tree [d])): + if tree [d] [i] != "*": + ch1 = 2 * i + ch2 = 2 * i + 1 + if ch1 >= len (tree [d + 1]) or ( + (tree [d + 1] [ch1] == "*" and ( + ch2 >= len (tree [d + 1]) or tree [d + 1] [ch2] == "*"))): + print (d + 1) + done = True + break + if done: + break diff --git a/challenge-151/abigail/python/ch-2.py b/challenge-151/abigail/python/ch-2.py new file mode 100644 index 0000000000..a20a18329b --- /dev/null +++ b/challenge-151/abigail/python/ch-2.py @@ -0,0 +1,18 @@ +#!/usr/local/bin/python3 + +# +# See https://theweeklychallenge.org/blog/perl-weekly-challenge-151 +# + +# +# Run as: python ch-2.py < input-file +# + +import fileinput + +for line in fileinput . input (): + houses = list (map (lambda x: int (x), line . strip () . split ())) + best = [0] * (len (houses) + 2) + for i in range (len (houses) - 1, 1, -1): + best [i] = max (houses [i] + best [i + 2], best [i + 1]) + print (houses [0] + best [2]) -- cgit From c6bb015992c65d4248161ca12c22a1af6dd29711 Mon Sep 17 00:00:00 2001 From: Abigail Date: Tue, 8 Feb 2022 18:33:35 +0100 Subject: Week 151: Make the algorithm for part 2 even simpler. No need for an additional array: add two 0's to the array of valuables, and modify in situ. --- challenge-151/abigail/awk/ch-2.awk | 7 ++----- challenge-151/abigail/bash/ch-2.sh | 19 ++++++++----------- challenge-151/abigail/c/ch-2.c | 29 ++++++++++------------------- challenge-151/abigail/lua/ch-2.lua | 15 +++++++-------- challenge-151/abigail/node/ch-2.js | 14 ++++++-------- challenge-151/abigail/perl/ch-2.pl | 15 +++++---------- challenge-151/abigail/python/ch-2.py | 11 ++++++----- 7 files changed, 44 insertions(+), 66 deletions(-) diff --git a/challenge-151/abigail/awk/ch-2.awk b/challenge-151/abigail/awk/ch-2.awk index 65428f81e8..732444708f 100644 --- a/challenge-151/abigail/awk/ch-2.awk +++ b/challenge-151/abigail/awk/ch-2.awk @@ -13,12 +13,9 @@ function max (a, b) { } { - delete best - best [NF + 1] = 0 - best [NF + 2] = 0 for (i = NF; i > 2; i --) { - best [i] = max($i + best [i + 2], best [i + 1]) + $i = max($i + $(i + 2), $(i + 1)) } - print $1 + best [3] + print $1 + $3 } diff --git a/challenge-151/abigail/bash/ch-2.sh b/challenge-151/abigail/bash/ch-2.sh index 6a9eceec29..cb009e0e16 100644 --- a/challenge-151/abigail/bash/ch-2.sh +++ b/challenge-151/abigail/bash/ch-2.sh @@ -10,17 +10,14 @@ set -f -declare -a best - -while read -a houses -do size=${#houses[@]} - best[$((size + 0))]=0 - best[$((size + 1))]=0 - for ((i = size - 1; i >= 2; i --)) - do ((val1 = ${houses[$i]} + ${best[$((i + 2))]})) - ((val2 = ${best[$((i + 1))]})) - best[$i]=$((val1 < val2 ? val2 : val1)) +while read -a h +do h[${#h[@]}]=0 + h[${#h[@]}]=0 + for ((i = ${#h[@]} - 3; i >= 2; i --)) + do ((val1 = ${h[$i]} + ${h[$((i + 2))]})) + ((val2 = ${h[$((i + 1))]})) + h[$i]=$((val1 < val2 ? val2 : val1)) done - echo $((${houses[0]} + ${best[2]})) + echo $((${h[0]} + ${h[2]})) done diff --git a/challenge-151/abigail/c/ch-2.c b/challenge-151/abigail/c/ch-2.c index 34e7e24ac1..9c46968f80 100644 --- a/challenge-151/abigail/c/ch-2.c +++ b/challenge-151/abigail/c/ch-2.c @@ -21,39 +21,30 @@ int main (void) { while ((str_len = getline (&line, &len, stdin)) != -1) { char * line_ptr = line; - int * houses = NULL; - int * best = NULL; + int * h = NULL; int val; int offset; - int nr_of_houses = 0; + int sz = 0; while (sscanf (line_ptr, "%d%n", &val, &offset) == 1) { - if ((houses = (int *) - realloc (houses, ++ nr_of_houses * sizeof (int))) == NULL) { + if ((h = (int *) realloc (h, (2 + ++ sz) * sizeof (int))) == NULL) { perror ("Recalloc failed"); exit (1); } - houses [nr_of_houses - 1] = val; + h [sz - 1] = val; line_ptr += offset; } - if ((best = (int *) - malloc ((nr_of_houses + 2) * sizeof (int))) == NULL) { - perror ("Malloc failed"); - exit (1); - } - - best [nr_of_houses + 0] = 0; - best [nr_of_houses + 1] = 0; + h [sz + 0] = 0; + h [sz + 1] = 0; - for (int i = nr_of_houses - 1; i >= 2; i --) { - best [i] = max (houses [i] + best [i + 2], best [i + 1]); + for (int i = sz - 1; i >= 2; i --) { + h [i] = max (h [i] + h [i + 2], h [i + 1]); } - printf ("%d\n", houses [0] + best [2]); + printf ("%d\n", h [0] + h [2]); - free (houses); - free (best); + free (h); } free (line); diff --git a/challenge-151/abigail/lua/ch-2.lua b/challenge-151/abigail/lua/ch-2.lua index 0e644d0a6c..d8ac15c101 100644 --- a/challenge-151/abigail/lua/ch-2.lua +++ b/challenge-151/abigail/lua/ch-2.lua @@ -9,18 +9,17 @@ -- for line in io . lines () do - local houses = {} - local best = {} + local h = {} for val in line : gmatch ("%d+") do - houses [#houses + 1] = val + h [#h + 1] = val end - best [#houses + 1] = 0 - best [#houses + 2] = 0 + h [#h + 1] = 0 + h [#h + 1] = 0 - for i = #houses, 3, -1 do - best [i] = math . max (houses [i] + best [i + 2], best [i + 1]) + for i = #h - 2, 3, -1 do + h [i] = math . max (h [i] + h [i + 2], h [i + 1]) end - print (houses [1] + best [3]) + print (h [1] + h [3]) end diff --git a/challenge-151/abigail/node/ch-2.js b/challenge-151/abigail/node/ch-2.js index 079c61e0ce..11a88c6bc7 100644 --- a/challenge-151/abigail/node/ch-2.js +++ b/challenge-151/abigail/node/ch-2.js @@ -11,13 +11,11 @@ require ('readline') . createInterface ({input: process . stdin}) . on ('line', line => { - let houses = line . trim () . split (/ +/) . map (n => +n) - let best = [] - let size = houses . length - best [size + 0] = 0 - best [size + 1] = 0 - for (let i = size - 1; i >= 2; i --) { - best [i] = Math . max (houses [i] + best [i + 2], best [i + 1]) + let h = line . trim () . split (/ +/) . map (n => +n) + h [h . length] = 0 + h [h . length] = 0 + for (let i = h . length - 3; i >= 2; i --) { + h [i] = Math . max (h [i] + h [i + 2], h [i + 1]) } - console . log (houses [0] + best [2]) + console . log (h [0] + h [2]) }) diff --git a/challenge-151/abigail/perl/ch-2.pl b/challenge-151/abigail/perl/ch-2.pl index 9c1e1d3b60..df731f4cfe 100644 --- a/challenge-151/abigail/perl/ch-2.pl +++ b/challenge-151/abigail/perl/ch-2.pl @@ -34,15 +34,10 @@ use List::Util qw [max]; # And we must always pick the first house. # -sub best ($houses) { - my @best; - $best [$#$houses + 1] = 0; - $best [$#$houses + 2] = 0; - for (my $i = $#$houses; $i >= 2; $i --) { - $best [$i] = max $$houses [$i] + $best [$i + 2], $best [$i + 1]; - } - $$houses [0] + $best [2]; +while (<>) { + my @h = (/[0-9]+/g, 0, 0); + $h [$_] = max $h [$_] + $h [$_ + 2], $h [$_ + 1] for reverse 2 .. $#h - 2; + say $h [0] + $h [2]; } - -say best [/[0-9]+/g] while <>; +__END__ diff --git a/challenge-151/abigail/python/ch-2.py b/challenge-151/abigail/python/ch-2.py index a20a18329b..52522eff0b 100644 --- a/challenge-151/abigail/python/ch-2.py +++ b/challenge-151/abigail/python/ch-2.py @@ -11,8 +11,9 @@ import fileinput for line in fileinput . input (): - houses = list (map (lambda x: int (x), line . strip () . split ())) - best = [0] * (len (houses) + 2) - for i in range (len (houses) - 1, 1, -1): - best [i] = max (houses [i] + best [i + 2], best [i + 1]) - print (houses [0] + best [2]) + h = list (map (lambda x: int (x), line . strip () . split ())) + h . append (0) + h . append (0) + for i in range (len (h) - 3, 1, -1): + h [i] = max (h [i] + h [i + 2], h [i + 1]) + print (h [0] + h [2]) -- cgit From 922b81eeb7516829d0311fd3fb5b9fa51fe48876 Mon Sep 17 00:00:00 2001 From: Abigail Date: Tue, 8 Feb 2022 19:14:23 +0100 Subject: Week 151: Ruby solutions --- challenge-151/abigail/ruby/ch-1.rb | 31 +++++++++++++++++++++++++++++++ challenge-151/abigail/ruby/ch-2.rb | 17 +++++++++++++++++ 2 files changed, 48 insertions(+) create mode 100644 challenge-151/abigail/ruby/ch-1.rb create mode 100644 challenge-151/abigail/ruby/ch-2.rb diff --git a/challenge-151/abigail/ruby/ch-1.rb b/challenge-151/abigail/ruby/ch-1.rb new file mode 100644 index 0000000000..1164c5bdfc --- /dev/null +++ b/challenge-151/abigail/ruby/ch-1.rb @@ -0,0 +1,31 @@ +#!/usr/bin/ruby + +# +# See https://theweeklychallenge.org/blog/perl-weekly-challenge-151 +# + +# +# Run as: ruby ch-1.rb < input-file +# + +ARGF . each_line do |line| + tree = line . strip() . split(/\|/) . map do |row| + row . strip() . split(/ +/) . map do |x| + x == "*" ? false : true + end + end + done = false + for d in 0 .. tree . length() - 1 do + for i in 0 .. tree [d] . length() - 1 do + if tree [d] [i] && !tree [d + 1] [2 * i] && + !tree [d + 1] [2 * i + 1] then + puts (d + 1) + done = true + break + end + end + if done then + break + end + end +end diff --git a/challenge-151/abigail/ruby/ch-2.rb b/challenge-151/abigail/ruby/ch-2.rb new file mode 100644 index 0000000000..ebdd805af5 --- /dev/null +++ b/challenge-151/abigail/ruby/ch-2.rb @@ -0,0 +1,17 @@ +#!/usr/bin/ruby + +# +# See https://theweeklychallenge.org/blog/perl-weekly-challenge-151 +# + +# +# Run as: ruby ch-2.rb < input-file +# + +ARGF . each_line do |line| + (h = line . split . map do |n| n . to_i end) . push(0, 0) + (h . length - 3) . downto(2) do |i| + h[i] = [h[i] + h[i + 2], h[i + 1]] . max + end + puts (h[0] + h[2]) +end -- cgit From d77e2b954262d6a2299587cc83d572caa879b3de Mon Sep 17 00:00:00 2001 From: Abigail Date: Tue, 8 Feb 2022 20:25:30 +0100 Subject: Week 151: Go solution for part 2 --- challenge-151/abigail/go/ch-2.go | 48 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 challenge-151/abigail/go/ch-2.go diff --git a/challenge-151/abigail/go/ch-2.go b/challenge-151/abigail/go/ch-2.go new file mode 100644 index 0000000000..a8b6725ffa --- /dev/null +++ b/challenge-151/abigail/go/ch-2.go @@ -0,0 +1,48 @@ +package main + +// +// See https://theweeklychallenge.org/blog/perl-weekly-challenge-151 +// + +// +// Run as: go run ch-2.go < input-file +// + +import ( + "fmt" + "bufio" + "os" + "strconv" + "strings" +) + +func max (a int, b int) int { + if a < b { + return b + } + return a +} + +func main () { + var reader = bufio . NewReader (os. Stdin) + for { + var text, err = reader . ReadString ('\n') + if (err != nil) { + break + } + + s := strings . Fields (strings . Trim (text, "\n")) + h := make ([] int, len (s)) + for i, v := range s { + n, _ := strconv . Atoi (v) + h [i] = n + } + h = append (h, 0, 0) + + for i := len (h) - 3; i >= 2; i -- { + h [i] = max (h [i] + h [i + 2], h [i + 1]) + } + + fmt . Println (h [0] + h [2]) + } +} -- cgit From c505ea725b0c148e1c8017b282d7f6b48a8236d8 Mon Sep 17 00:00:00 2001 From: Abigail Date: Tue, 8 Feb 2022 20:41:01 +0100 Subject: Week 151: R solution for part 2 --- challenge-151/abigail/r/ch-2.r | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 challenge-151/abigail/r/ch-2.r diff --git a/challenge-151/abigail/r/ch-2.r b/challenge-151/abigail/r/ch-2.r new file mode 100644 index 0000000000..5d2e166d4a --- /dev/null +++ b/challenge-151/abigail/r/ch-2.r @@ -0,0 +1,22 @@ +#!/usr/local/bin/Rscript + +# +# See https://theweeklychallenge.org/blog/perl-weekly-challenge-151 +# + +# +# Run as: Rscript ch-2.r < input-file +# + +stdin <- file ('stdin', 'r') +repeat { + line <- readLines (stdin, n = 1) + if (length (line) == 0) { + break + } + h <- c (as.numeric (strsplit (line, " ") [[1]]), 0, 0) + for (i in seq (length (h) - 2, 3, -1)) { + h [[i]] <- max (c (h [[i]] + h [[i + 2]], h [[i + 1]])) + } + cat (h [[1]] + h [[3]], "\n") +} -- cgit From 0622db52478d8591387acd183d27d3fb19f5334f Mon Sep 17 00:00:00 2001 From: Abigail Date: Tue, 8 Feb 2022 20:42:39 +0100 Subject: Week 151: README --- challenge-151/abigail/README.md | 27 --------------------------- 1 file changed, 27 deletions(-) diff --git a/challenge-151/abigail/README.md b/challenge-151/abigail/README.md index 352bb54018..f1c6532822 100644 --- a/challenge-151/abigail/README.md +++ b/challenge-151/abigail/README.md @@ -4,50 +4,23 @@ * [AWK](awk/ch-1.awk) * [Bash](bash/ch-1.sh) -* [Bc](bc/ch-1.bc) * [C](c/ch-1.c) -* [Go](go/ch-1.go) -* [Java](java/ch-1.java) * [Lua](lua/ch-1.lua) * [Node.js](node/ch-1.js) -* [Pascal](pascal/ch-1.p) * [Perl](perl/ch-1.pl) * [Python](python/ch-1.py) -* [R](r/ch-1.r) * [Ruby](ruby/ch-1.rb) -* [Scheme](scheme/ch-1.scm) -* [Tcl](tcl/ch-1.tcl) ## Part 2 * [AWK](awk/ch-2.awk) * [Bash](bash/ch-2.sh) -* [Basic](basic/ch-2.bas) -* [Bc](bc/ch-2.bc) -* [Befunge-93](befunge-93/ch-2.bf93) * [C](c/ch-2.c) -* [Cobol](cobol/ch-2.cb) -* [Csh](csh/ch-2.csh) -* [Erlang](lua/ch-2.erl) -* [Forth](lua/ch-2.fs) -* [Fortran](fortran/ch-2.f90) * [Go](go/ch-2.go) -* [Java](java/ch-2.java) * [Lua](lua/ch-2.lua) -* [m4](m4/ch-2.m4) -* [MMIX](mmix/ch-2.mms) * [Node.js](node/ch-2.js) -* [OCaml](ocaml/ch-2.ml) -* [Pascal](pascal/ch-2.p) * [Perl](perl/ch-2.pl) -* [PHP](php/ch-2.php) -* [PostScript](postscript/ch-2.ps) * [Python](python/ch-2.py) * [R](r/ch-2.r) -* [Rexx](rexx/ch-2.rexx) * [Ruby](ruby/ch-2.rb) -* [Scheme](scheme/ch-2.scm) -* [Sed](sed/ch-2.sed) -* [SQL](sql/ch-2.sql) -* [Tcl](tcl/ch-2.tcl) -- cgit From a09d0dd70c22dc386c4280cfae04158098dcc924 Mon Sep 17 00:00:00 2001 From: Abigail Date: Tue, 8 Feb 2022 20:59:09 +0100 Subject: Week 151: Pascal solution for part 2 --- challenge-151/abigail/README.md | 1 + challenge-151/abigail/pascal/ch-2.p | 41 +++++++++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+) create mode 100644 challenge-151/abigail/pascal/ch-2.p diff --git a/challenge-151/abigail/README.md b/challenge-151/abigail/README.md index f1c6532822..870c6804c1 100644 --- a/challenge-151/abigail/README.md +++ b/challenge-151/abigail/README.md @@ -20,6 +20,7 @@ * [Go](go/ch-2.go) * [Lua](lua/ch-2.lua) * [Node.js](node/ch-2.js) +* [Pascal](pascal/ch-2.p) * [Perl](perl/ch-2.pl) * [Python](python/ch-2.py) * [R](r/ch-2.r) diff --git a/challenge-151/abigail/pascal/ch-2.p b/challenge-151/abigail/pascal/ch-2.p new file mode 100644 index 0000000000..18053673e2 --- /dev/null +++ b/challenge-151/abigail/pascal/ch-2.p @@ -0,0 +1,41 @@ +Program ch2; + +(* *) +(* See https://theweeklychallenge.org/blog/perl-weekly-challenge-151 *) +(* *) + +(* *) +(* Run as: fpc -och-2.out ch-2.p; ./ch-2.out < input-file *) +(* *) + +uses + math; + +var + h: array of integer; + i, sz: integer; + +begin + while not eof do begin + sz := 0; + setlength (h, sz); + + while not eoln do begin + inc (sz); + setlength (h, sz); + read (h [sz - 1]); + end; + + setlength (h, sz + 2); + h [sz + 0] := 0; + h [sz + 1] := 0; + + for i := sz - 1 downto 2 do begin + h [i] := max (h [i] + h [i + 2], h [i + 1]); + end; + + writeln (h [0] + h [2]); + + readln; + end +end. -- cgit