From 36df052252a4d1b6d145e4a44dc5360091c3878b Mon Sep 17 00:00:00 2001 From: Abigail Date: Mon, 5 Jul 2021 12:35:46 +0200 Subject: README for week 120 --- challenge-120/abigail/README.md | 62 ++++++++++++++++++++++++----------------- 1 file changed, 37 insertions(+), 25 deletions(-) diff --git a/challenge-120/abigail/README.md b/challenge-120/abigail/README.md index 2fc20d5e68..c7dfc7d883 100644 --- a/challenge-120/abigail/README.md +++ b/challenge-120/abigail/README.md @@ -1,34 +1,29 @@ # Solutions by Abigail -## [Swap Nibbles](https://perlweeklychallenge.org/blog/perl-weekly-challenge-119/#TASK1) +## [Swap Odd/Even Bits](https://perlweeklychallenge.org/blog/perl-weekly-challenge-120/#TASK1) -> You are given a positive integer `$N`. -> -> Write a script to swap the two nibbles of the binary representation of -> the given number and print the decimal number of the new binary -> representation. -> -> > A nibble is a four-bit aggregation, or half an octet. -> -> To keep the task simple, we only allow integer less than or equal to `255`. +> You are given a positive integer `$N` less than or equal to 255. +> +> Write a script to swap the odd positioned bit with even positioned +> bit and print the decimal equivalent of the new binary representation. ### Examples ~~~~ Input: $N = 101 -Output: 86 +Output: 154 ~~~~ -Binary representation of decimal `101` is `1100101` or as 2 nibbles -`(0110)(0101)`. The swapped nibbles would be `(0101)(0110)` same as -decimal `86`. +Binary representation of the given number is `01 10 01 01`. +The new binary representation after the odd/even swap is `10 01 10 10`. +The decimal equivalent of `10011010` is `154`. ~~~~ Input: $N = 18 Output: 33 ~~~~ -Binary representation of decimal `18` is `10010` or as 2 nibbles -`(0001)(0010)`. The swapped nibbles would be `(0010)(0001)` same as -decimal `33`. +Binary representation of the given number is `00 01 00 10`. +The new binary representation after the odd/even swap is `00 10 00 01`. +The decimal equivalent of `100001` is `33`. ### Solutions * [AWK](awk/ch-1.awk) @@ -49,16 +44,33 @@ decimal `33`. * [Tcl](tcl/ch-1.tcl) ### Blog -[Swap Nibbles](https://abigail.github.io/HTML/Perl-Weekly-Challenge/week-119-1.html) +[Swap Odd/Even Bits](https://abigail.github.io/HTML/Perl-Weekly-Challenge/week-120-1.html) -## [Sequence without 1-on-1](https://perlweeklychallenge.org/blog/perl-weekly-challenge-119/#TASK2) +## [Clock Angle](https://perlweeklychallenge.org/blog/perl-weekly-challenge-119/#TASK2) -> Write a script to generate sequence starting at `1`. Consider the -> increasing sequence of integers which contain only `1`s, `2`s, and -> `3`s, and do not have any doublets of `1`s > like below. Please accept -> a positive integer `$N` and print the `$N`th term in the generated sequence. +> You are given time `$T` in the format `hh:mm`. > -> > 1, 2, 3, 12, 13, 21, 22, 23, 31, 32, 33, 121, 122, 123, 131, ... +> Write a script to find the smaller angle formed by the hands of an +> analog clock at a given time.
+> **HINT: A analog clock is divided up into `12` sectors. One sector +> represents `30` degree (`360/12 = 30`).** + +### Examples +~~~~ +Input: $T = '03:10' +Output: 35 degree +~~~~ + +The distance between the `2` and the `3` on the clock is `30` degree. +For the `10` minutes i.e. `1/6` of an hour that have passed. The hour +hand has also moved `1/6` of the distance between the `3` and the `4`, +which adds `5` degree (`1/6` of `30`). The total measure of the angle +is `35` degree. + +~~~~ +Input: $T = '04:00' +Output: 120 degree +~~~~ ### Solutions * [AWK](awk/ch-2.awk) @@ -71,4 +83,4 @@ decimal `33`. * [Ruby](ruby/ch-2.rb) ### Blog -[Sequence without 1-on-1](https://abigail.github.io/HTML/Perl-Weekly-Challenge/week-119-2.html) +[Clock Angle](https://abigail.github.io/HTML/Perl-Weekly-Challenge/week-120-2.html) -- cgit From c79281808d6b8968f5bd105536df88b345bded7d Mon Sep 17 00:00:00 2001 From: Abigail Date: Mon, 5 Jul 2021 14:37:17 +0200 Subject: Tests for week 120 --- challenge-120/abigail/t/ctest.ini | 12 ++ challenge-120/abigail/t/input-1-1 | 2 + challenge-120/abigail/t/input-1-2 | 255 +++++++++++++++++++++++++++++++++ challenge-120/abigail/t/input-2-1 | 2 + challenge-120/abigail/t/output-1-1.exp | 2 + challenge-120/abigail/t/output-1-2.exp | 255 +++++++++++++++++++++++++++++++++ challenge-120/abigail/t/output-2-1.exp | 2 + 7 files changed, 530 insertions(+) create mode 100644 challenge-120/abigail/t/ctest.ini create mode 100644 challenge-120/abigail/t/input-1-1 create mode 100644 challenge-120/abigail/t/input-1-2 create mode 100644 challenge-120/abigail/t/input-2-1 create mode 100644 challenge-120/abigail/t/output-1-1.exp create mode 100644 challenge-120/abigail/t/output-1-2.exp create mode 100644 challenge-120/abigail/t/output-2-1.exp diff --git a/challenge-120/abigail/t/ctest.ini b/challenge-120/abigail/t/ctest.ini new file mode 100644 index 0000000000..4f84a004d3 --- /dev/null +++ b/challenge-120/abigail/t/ctest.ini @@ -0,0 +1,12 @@ +# +# Configuration file for running tests, using ctest. +# See https://github.com/Abigail/Misc/blob/master/ctest +# + +[names] +1-1 = Given Examples +1-2 = Numbers 1 to 255 +2-1 = Given Examples + +[challenges/1/bc] +add_to_input = 0 diff --git a/challenge-120/abigail/t/input-1-1 b/challenge-120/abigail/t/input-1-1 new file mode 100644 index 0000000000..1229000578 --- /dev/null +++ b/challenge-120/abigail/t/input-1-1 @@ -0,0 +1,2 @@ +101 +18 diff --git a/challenge-120/abigail/t/input-1-2 b/challenge-120/abigail/t/input-1-2 new file mode 100644 index 0000000000..99f18623a6 --- /dev/null +++ b/challenge-120/abigail/t/input-1-2 @@ -0,0 +1,255 @@ +1 +2 +3 +4 +5 +6 +7 +8 +9 +10 +11 +12 +13 +14 +15 +16 +17 +18 +19 +20 +21 +22 +23 +24 +25 +26 +27 +28 +29 +30 +31 +32 +33 +34 +35 +36 +37 +38 +39 +40 +41 +42 +43 +44 +45 +46 +47 +48 +49 +50 +51 +52 +53 +54 +55 +56 +57 +58 +59 +60 +61 +62 +63 +64 +65 +66 +67 +68 +69 +70 +71 +72 +73 +74 +75 +76 +77 +78 +79 +80 +81 +82 +83 +84 +85 +86 +87 +88 +89 +90 +91 +92 +93 +94 +95 +96 +97 +98 +99 +100 +101 +102 +103 +104 +105 +106 +107 +108 +109 +110 +111 +112 +113 +114 +115 +116 +117 +118 +119 +120 +121 +122 +123 +124 +125 +126 +127 +128 +129 +130 +131 +132 +133 +134 +135 +136 +137 +138 +139 +140 +141 +142 +143 +144 +145 +146 +147 +148 +149 +150 +151 +152 +153 +154 +155 +156 +157 +158 +159 +160 +161 +162 +163 +164 +165 +166 +167 +168 +169 +170 +171 +172 +173 +174 +175 +176 +177 +178 +179 +180 +181 +182 +183 +184 +185 +186 +187 +188 +189 +190 +191 +192 +193 +194 +195 +196 +197 +198 +199 +200 +201 +202 +203 +204 +205 +206 +207 +208 +209 +210 +211 +212 +213 +214 +215 +216 +217 +218 +219 +220 +221 +222 +223 +224 +225 +226 +227 +228 +229 +230 +231 +232 +233 +234 +235 +236 +237 +238 +239 +240 +241 +242 +243 +244 +245 +246 +247 +248 +249 +250 +251 +252 +253 +254 +255 diff --git a/challenge-120/abigail/t/input-2-1 b/challenge-120/abigail/t/input-2-1 new file mode 100644 index 0000000000..2fc5888d95 --- /dev/null +++ b/challenge-120/abigail/t/input-2-1 @@ -0,0 +1,2 @@ +03:10 +04:00 diff --git a/challenge-120/abigail/t/output-1-1.exp b/challenge-120/abigail/t/output-1-1.exp new file mode 100644 index 0000000000..1eaeeb42db --- /dev/null +++ b/challenge-120/abigail/t/output-1-1.exp @@ -0,0 +1,2 @@ +154 +33 diff --git a/challenge-120/abigail/t/output-1-2.exp b/challenge-120/abigail/t/output-1-2.exp new file mode 100644 index 0000000000..e6bb766d0d --- /dev/null +++ b/challenge-120/abigail/t/output-1-2.exp @@ -0,0 +1,255 @@ +2 +1 +3 +8 +10 +9 +11 +4 +6 +5 +7 +12 +14 +13 +15 +32 +34 +33 +35 +40 +42 +41 +43 +36 +38 +37 +39 +44 +46 +45 +47 +16 +18 +17 +19 +24 +26 +25 +27 +20 +22 +21 +23 +28 +30 +29 +31 +48 +50 +49 +51 +56 +58 +57 +59 +52 +54 +53 +55 +60 +62 +61 +63 +128 +130 +129 +131 +136 +138 +137 +139 +132 +134 +133 +135 +140 +142 +141 +143 +160 +162 +161 +163 +168 +170 +169 +171 +164 +166 +165 +167 +172 +174 +173 +175 +144 +146 +145 +147 +152 +154 +153 +155 +148 +150 +149 +151 +156 +158 +157 +159 +176 +178 +177 +179 +184 +186 +185 +187 +180 +182 +181 +183 +188 +190 +189 +191 +64 +66 +65 +67 +72 +74 +73 +75 +68 +70 +69 +71 +76 +78 +77 +79 +96 +98 +97 +99 +104 +106 +105 +107 +100 +102 +101 +103 +108 +110 +109 +111 +80 +82 +81 +83 +88 +90 +89 +91 +84 +86 +85 +87 +92 +94 +93 +95 +112 +114 +113 +115 +120 +122 +121 +123 +116 +118 +117 +119 +124 +126 +125 +127 +192 +194 +193 +195 +200 +202 +201 +203 +196 +198 +197 +199 +204 +206 +205 +207 +224 +226 +225 +227 +232 +234 +233 +235 +228 +230 +229 +231 +236 +238 +237 +239 +208 +210 +209 +211 +216 +218 +217 +219 +212 +214 +213 +215 +220 +222 +221 +223 +240 +242 +241 +243 +248 +250 +249 +251 +244 +246 +245 +247 +252 +254 +253 +255 diff --git a/challenge-120/abigail/t/output-2-1.exp b/challenge-120/abigail/t/output-2-1.exp new file mode 100644 index 0000000000..7b477e88f2 --- /dev/null +++ b/challenge-120/abigail/t/output-2-1.exp @@ -0,0 +1,2 @@ +35 +120 -- cgit From 78c841faea1060a3b7ea63a4316e461e490d275e Mon Sep 17 00:00:00 2001 From: Abigail Date: Mon, 5 Jul 2021 14:38:06 +0200 Subject: Solutions in 15 languages for week 120, part 1. This is basically the same as last weeks challenge... --- challenge-120/abigail/README.md | 1 - challenge-120/abigail/awk/ch-1.awk | 21 +++++++++++++++++++++ challenge-120/abigail/bash/ch-1.sh | 16 ++++++++++++++++ challenge-120/abigail/bc/ch-1.bc | 27 +++++++++++++++++++++++++++ challenge-120/abigail/c/ch-1.c | 22 ++++++++++++++++++++++ challenge-120/abigail/go/ch-1.go | 25 +++++++++++++++++++++++++ challenge-120/abigail/java/ch-1.java | 27 +++++++++++++++++++++++++++ challenge-120/abigail/lua/ch-1.lua | 27 +++++++++++++++++++++++++++ challenge-120/abigail/node/ch-1.js | 16 ++++++++++++++++ challenge-120/abigail/pascal/ch-1.p | 22 ++++++++++++++++++++++ challenge-120/abigail/perl/ch-1.pl | 29 +++++++++++++++++++++++++++++ challenge-120/abigail/python/ch-1.py | 16 ++++++++++++++++ challenge-120/abigail/r/ch-1.r | 19 +++++++++++++++++++ challenge-120/abigail/ruby/ch-1.rb | 16 ++++++++++++++++ challenge-120/abigail/scheme/ch-1.scm | 23 +++++++++++++++++++++++ challenge-120/abigail/tcl/ch-1.tcl | 12 ++++++++++++ 16 files changed, 318 insertions(+), 1 deletion(-) create mode 100644 challenge-120/abigail/awk/ch-1.awk create mode 100644 challenge-120/abigail/bash/ch-1.sh create mode 100644 challenge-120/abigail/bc/ch-1.bc create mode 100644 challenge-120/abigail/c/ch-1.c create mode 100644 challenge-120/abigail/go/ch-1.go create mode 100644 challenge-120/abigail/java/ch-1.java create mode 100644 challenge-120/abigail/lua/ch-1.lua create mode 100644 challenge-120/abigail/node/ch-1.js create mode 100644 challenge-120/abigail/pascal/ch-1.p create mode 100644 challenge-120/abigail/perl/ch-1.pl create mode 100644 challenge-120/abigail/python/ch-1.py create mode 100644 challenge-120/abigail/r/ch-1.r create mode 100644 challenge-120/abigail/ruby/ch-1.rb create mode 100644 challenge-120/abigail/scheme/ch-1.scm create mode 100644 challenge-120/abigail/tcl/ch-1.tcl diff --git a/challenge-120/abigail/README.md b/challenge-120/abigail/README.md index c7dfc7d883..f2ea164db7 100644 --- a/challenge-120/abigail/README.md +++ b/challenge-120/abigail/README.md @@ -29,7 +29,6 @@ The decimal equivalent of `100001` is `33`. * [AWK](awk/ch-1.awk) * [Bash](bash/ch-1.sh) * [bc](bc/ch-1.bc) -* [Befunge-93](befunge-93/ch-1.bf93) * [C](c/ch-1.c) * [Go](go/ch-1.go) * [Java](java/ch-1.java) diff --git a/challenge-120/abigail/awk/ch-1.awk b/challenge-120/abigail/awk/ch-1.awk new file mode 100644 index 0000000000..2f7e4310d2 --- /dev/null +++ b/challenge-120/abigail/awk/ch-1.awk @@ -0,0 +1,21 @@ +#!/usr/bin/awk + +# +# See ../README.md +# + +# +# Run as: awk -f ch-1.awk < input-file +# + +{ + out = 0 + num = $1 + for (i = 0; i < 8; i ++) { + bit = int ((num - (num % 2 ^ i)) / 2 ^ i) % 2; + if (bit) { + out += 2 ^ (i + (i % 2 ? -1 : 1)) + } + } + print out +} diff --git a/challenge-120/abigail/bash/ch-1.sh b/challenge-120/abigail/bash/ch-1.sh new file mode 100644 index 0000000000..8ecc50d5dc --- /dev/null +++ b/challenge-120/abigail/bash/ch-1.sh @@ -0,0 +1,16 @@ +#!/bin/sh + +# +# See ../README.md +# + +# +# Run as: bash ch-1.sh < input-file +# + +set -f + +while read num +do echo $(( (num & 0x55) << 1 + | (num & 0xAA) >> 1 )) +done diff --git a/challenge-120/abigail/bc/ch-1.bc b/challenge-120/abigail/bc/ch-1.bc new file mode 100644 index 0000000000..8a1f52c022 --- /dev/null +++ b/challenge-120/abigail/bc/ch-1.bc @@ -0,0 +1,27 @@ +# +# See ../README.md +# + +# +# Run as: bc ch-1.bc < input-file +# + +while (1) { + num = read () + if (num == 0) { + break + } + out = 0 + for (i = 0; i < 8; i ++) { + bit = ((num - (num % 2 ^ i)) / 2 ^ i) % 2 + if (bit == 1) { + if (i % 2 == 1) { + out += 2 ^ (i - 1) + } + if (i % 2 == 0) { + out += 2 ^ (i + 1) + } + } + } + out +} diff --git a/challenge-120/abigail/c/ch-1.c b/challenge-120/abigail/c/ch-1.c new file mode 100644 index 0000000000..ae21fa18d5 --- /dev/null +++ b/challenge-120/abigail/c/ch-1.c @@ -0,0 +1,22 @@ +# include +# include +# include + +/* + * See ../README.md + */ + +/* + * Run as: cc -o ch-1.o ch-1.c; ./ch-1.o < input-file + */ + +int main (void) { + int num; + + while (scanf ("%d", &num) == 1) { + printf ("%d\n", (num & 0x55) << 1 + | (num & 0xAA) >> 1); + } + + return (0); +} diff --git a/challenge-120/abigail/go/ch-1.go b/challenge-120/abigail/go/ch-1.go new file mode 100644 index 0000000000..e5ebd26ebb --- /dev/null +++ b/challenge-120/abigail/go/ch-1.go @@ -0,0 +1,25 @@ +package main + +// +// See ../README.md +// + +// +// Run as: go run ch-1.go +// + +import ( + "fmt" +) + +func main () { + for { + var n, num int + n, _ = fmt . Scanf ("%d", &num) + if (n != 1) { + break + } + fmt . Printf ("%d\n", (num & 0x55) << 1 | + (num & 0xAA) >> 1) + } +} diff --git a/challenge-120/abigail/java/ch-1.java b/challenge-120/abigail/java/ch-1.java new file mode 100644 index 0000000000..66cf3ee4e7 --- /dev/null +++ b/challenge-120/abigail/java/ch-1.java @@ -0,0 +1,27 @@ +// +// See ../README.md +// + +// +// Run as: ln ch-1.java ch1.java; javac ch1.java; java ch1 < input-file +// + +import java.util.*; + +public class ch1 { + public static void main (String [] args) { + Scanner scanner = new Scanner (System . in); + try { + while (true) { + int num = scanner . nextInt (); + System . out . println ( (num & 0x55) << 1 + | (num & 0xAA) >> 1); + } + } + catch (Exception e) { + // + // EOF + // + } + } +} diff --git a/challenge-120/abigail/lua/ch-1.lua b/challenge-120/abigail/lua/ch-1.lua new file mode 100644 index 0000000000..0d5896f59c --- /dev/null +++ b/challenge-120/abigail/lua/ch-1.lua @@ -0,0 +1,27 @@ +#!/opt/local/bin/lua + +-- +-- See ../README.md +-- + +-- +-- Run as: lua ch-1.lua < input-file +-- + +for line in io . lines () do + num = tonumber (line) + out = 0 + for i = 0, 7 do + bit = math . floor ((num - (num % (2 ^ i))) / (2 ^ i)) % 2 + if bit == 1 + then + if i % 2 == 1 + then + out = out + 2 ^ (i - 1) + else + out = out + 2 ^ (i + 1) + end + end + end + print (out) +end diff --git a/challenge-120/abigail/node/ch-1.js b/challenge-120/abigail/node/ch-1.js new file mode 100644 index 0000000000..b40fd0d3ac --- /dev/null +++ b/challenge-120/abigail/node/ch-1.js @@ -0,0 +1,16 @@ +#!/usr/local/bin/node + +// +// See ../README.md +// + +// +// Run as: node ch-1.js < input-file +// + + require ('readline') +. createInterface ({input: process . stdin}) +. on ('line', (num) => { + console . log ( (+num & 0x55) << 1 + | (+num & 0xAA) >> 1) +}) diff --git a/challenge-120/abigail/pascal/ch-1.p b/challenge-120/abigail/pascal/ch-1.p new file mode 100644 index 0000000000..b16ebde46e --- /dev/null +++ b/challenge-120/abigail/pascal/ch-1.p @@ -0,0 +1,22 @@ +Program XXX; + +(* *) +(* See ../README.md *) +(* *) + +(* *) +(* Run as: fpc -och-1.out ch-1.p; ./ch-1.out < input-file *) +(* *) + +var num: integer; + +begin + while true do begin + readln (num); + if num = 0 then begin + break; + end; + writeln ((num and $55) shl 1 or + (num and $AA) shr 1); + end +end. diff --git a/challenge-120/abigail/perl/ch-1.pl b/challenge-120/abigail/perl/ch-1.pl new file mode 100644 index 0000000000..b5aa1b4b9d --- /dev/null +++ b/challenge-120/abigail/perl/ch-1.pl @@ -0,0 +1,29 @@ +#!/opt/perl/bin/perl + +use 5.032; + +use strict; +use warnings; +no warnings 'syntax'; + +use experimental 'signatures'; +use experimental 'lexical_subs'; + +# +# See ../README.md +# + +# +# Run as: perl ch-1.pl < input-file +# + +# +# This is just week 119 with different constants +# + + +while (<>) { + say + ($_ & 0x55) << 1 # Odd bits shifted one the right + | ($_ & 0xAA) >> 1 # Even bits shifted one to the left +} + diff --git a/challenge-120/abigail/python/ch-1.py b/challenge-120/abigail/python/ch-1.py new file mode 100644 index 0000000000..510ce33d10 --- /dev/null +++ b/challenge-120/abigail/python/ch-1.py @@ -0,0 +1,16 @@ +#!/opt/local/bin/python + +# +# See ../README.md +# + +# +# Run as: python ch-1.py < input-file +# + +import fileinput + +for line in fileinput . input (): + num = int (line) + print ( (num & 0x55) << 1 + | (num & 0xAA) >> 1) diff --git a/challenge-120/abigail/r/ch-1.r b/challenge-120/abigail/r/ch-1.r new file mode 100644 index 0000000000..b276380d71 --- /dev/null +++ b/challenge-120/abigail/r/ch-1.r @@ -0,0 +1,19 @@ +# +# See ../README.md +# + +# +# Run as: Rscript ch-1.r < input-file +# + +stdin <- file ('stdin', 'r') +repeat { + n <- readLines (stdin, n = 1) + if (length (n) == 0) { + break + } + n = as.integer (n) + + cat (bitwOr (bitwShiftL (bitwAnd (n, 0x55), 1), + bitwShiftR (bitwAnd (n, 0xAA), 1)), "\n") +} diff --git a/challenge-120/abigail/ruby/ch-1.rb b/challenge-120/abigail/ruby/ch-1.rb new file mode 100644 index 0000000000..51fc9f6e50 --- /dev/null +++ b/challenge-120/abigail/ruby/ch-1.rb @@ -0,0 +1,16 @@ +#!/usr/bin/ruby + +# +# See ../README.md +# + +# +# Run as: ruby ch-1.rb < input-file +# + +ARGF . each_line do + |line| + num = line . to_i + puts ( (num & 0x55) << 1 \ + | (num & 0xAA) >> 1) +end diff --git a/challenge-120/abigail/scheme/ch-1.scm b/challenge-120/abigail/scheme/ch-1.scm new file mode 100644 index 0000000000..d28e2f668e --- /dev/null +++ b/challenge-120/abigail/scheme/ch-1.scm @@ -0,0 +1,23 @@ +;;; +;;; See ../README.md +;;; + +;;; +;;; Run as: guile --no-auto-compile ch-1.scm < input-file +;;; + + +(use-modules (ice-9 format)) + +(define (main) + (define num (read)) + (if (not (eof-object? num)) + (begin + (format #t "~d\n" (logior (ash (logand num #x55) 1) + (ash (logand num #xAA) -1))) + (main) + ) + ) +) + +(main) diff --git a/challenge-120/abigail/tcl/ch-1.tcl b/challenge-120/abigail/tcl/ch-1.tcl new file mode 100644 index 0000000000..5e08e4c702 --- /dev/null +++ b/challenge-120/abigail/tcl/ch-1.tcl @@ -0,0 +1,12 @@ +# +# See ../README.md +# + +# +# Run as: tclsh ch-1.tcl < input-file +# + +while {[gets stdin num] >= 0} { + puts [expr ($num & 0x55) << 1 \ + | ($num & 0xAA) >> 1] +} -- cgit From a781fb0ab337d2da7233100cc3414694dec59f40 Mon Sep 17 00:00:00 2001 From: Abigail Date: Mon, 5 Jul 2021 19:02:43 +0200 Subject: More tests for week 120, part 2 --- challenge-120/abigail/t/ctest.ini | 1 + challenge-120/abigail/t/input-2-2 | 720 +++++++++++++++++++++++++++++++++ challenge-120/abigail/t/output-2-2.exp | 720 +++++++++++++++++++++++++++++++++ 3 files changed, 1441 insertions(+) create mode 100644 challenge-120/abigail/t/input-2-2 create mode 100644 challenge-120/abigail/t/output-2-2.exp diff --git a/challenge-120/abigail/t/ctest.ini b/challenge-120/abigail/t/ctest.ini index 4f84a004d3..4bfae653d8 100644 --- a/challenge-120/abigail/t/ctest.ini +++ b/challenge-120/abigail/t/ctest.ini @@ -7,6 +7,7 @@ 1-1 = Given Examples 1-2 = Numbers 1 to 255 2-1 = Given Examples +2-2 = All hh:mm possible [challenges/1/bc] add_to_input = 0 diff --git a/challenge-120/abigail/t/input-2-2 b/challenge-120/abigail/t/input-2-2 new file mode 100644 index 0000000000..f76e1bcc13 --- /dev/null +++ b/challenge-120/abigail/t/input-2-2 @@ -0,0 +1,720 @@ +00:00 +00:01 +00:02 +00:03 +00:04 +00:05 +00:06 +00:07 +00:08 +00:09 +00:10 +00:11 +00:12 +00:13 +00:14 +00:15 +00:16 +00:17 +00:18 +00:19 +00:20 +00:21 +00:22 +00:23 +00:24 +00:25 +00:26 +00:27 +00:28 +00:29 +00:30 +00:31 +00:32 +00:33 +00:34 +00:35 +00:36 +00:37 +00:38 +00:39 +00:40 +00:41 +00:42 +00:43 +00:44 +00:45 +00:46 +00:47 +00:48 +00:49 +00:50 +00:51 +00:52 +00:53 +00:54 +00:55 +00:56 +00:57 +00:58 +00:59 +01:00 +01:01 +01:02 +01:03 +01:04 +01:05 +01:06 +01:07 +01:08 +01:09 +01:10 +01:11 +01:12 +01:13 +01:14 +01:15 +01:16 +01:17 +01:18 +01:19 +01:20 +01:21 +01:22 +01:23 +01:24 +01:25 +01:26 +01:27 +01:28 +01:29 +01:30 +01:31 +01:32 +01:33 +01:34 +01:35 +01:36 +01:37 +01:38 +01:39 +01:40 +01:41 +01:42 +01:43 +01:44 +01:45 +01:46 +01:47 +01:48 +01:49 +01:50 +01:51 +01:52 +01:53 +01:54 +01:55 +01:56 +01:57 +01:58 +01:59 +02:00 +02:01 +02:02 +02:03 +02:04 +02:05 +02:06 +02:07 +02:08 +02:09 +02:10 +02:11 +02:12 +02:13 +02:14 +02:15 +02:16 +02:17 +02:18 +02:19 +02:20 +02:21 +02:22 +02:23 +02:24 +02:25 +02:26 +02:27 +02:28 +02:29 +02:30 +02:31 +02:32 +02:33 +02:34 +02:35 +02:36 +02:37 +02:38 +02:39 +02:40 +02:41 +02:42 +02:43 +02:44 +02:45 +02:46 +02:47 +02:48 +02:49 +02:50 +02:51 +02:52 +02:53 +02:54 +02:55 +02:56 +02:57 +02:58 +02:59 +03:00 +03:01 +03:02 +03:03 +03:04 +03:05 +03:06 +03:07 +03:08 +03:09 +03:10 +03:11 +03:12 +03:13 +03:14 +03:15 +03:16 +03:17 +03:18 +03:19 +03:20 +03:21 +03:22 +03:23 +03:24 +03:25 +03:26 +03:27 +03:28 +03:29 +03:30 +03:31 +03:32 +03:33 +03:34 +03:35 +03:36 +03:37 +03:38 +03:39 +03:40 +03:41 +03:42 +03:43 +03:44 +03:45 +03:46 +03:47 +03:48 +03:49 +03:50 +03:51 +03:52 +03:53 +03:54 +03:55 +03:56 +03:57 +03:58 +03:59 +04:00 +04:01 +04:02 +04:03 +04:04 +04:05 +04:06 +04:07 +04:08 +04:09 +04:10 +04:11 +04:12 +04:13 +04:14 +04:15 +04:16 +04:17 +04:18 +04:19 +04:20 +04:21 +04:22 +04:23 +04:24 +04:25 +04:26 +04:27 +04:28 +04:29 +04:30 +04:31 +04:32 +04:33 +04:34 +04:35 +04:36 +04:37 +04:38 +04:39 +04:40 +04:41 +04:42 +04:43 +04:44 +04:45 +04:46 +04:47 +04:48 +04:49 +04:50 +04:51 +04:52 +04:53 +04:54 +04:55 +04:56 +04:57 +04:58 +04:59 +05:00 +05:01 +05:02 +05:03 +05:04 +05:05 +05:06 +05:07 +05:08 +05:09 +05:10 +05:11 +05:12 +05:13 +05:14 +05:15 +05:16 +05:17 +05:18 +05:19 +05:20 +05:21 +05:22 +05:23 +05:24 +05:25 +05:26 +05:27 +05:28 +05:29 +05:30 +05:31 +05:32 +05:33 +05:34 +05:35 +05:36 +05:37 +05:38 +05:39 +05:40 +05:41 +05:42 +05:43 +05:44 +05:45 +05:46 +05:47 +05:48 +05:49 +05:50 +05:51 +05:52 +05:53 +05:54 +05:55 +05:56 +05:57 +05:58 +05:59 +06:00 +06:01 +06:02 +06:03 +06:04 +06:05 +06:06 +06:07 +06:08 +06:09 +06:10 +06:11 +06:12 +06:13 +06:14 +06:15 +06:16 +06:17 +06:18 +06:19 +06:20 +06:21 +06:22 +06:23 +06:24 +06:25 +06:26 +06:27 +06:28 +06:29 +06:30 +06:31 +06:32 +06:33 +06:34 +06:35 +06:36 +06:37 +06:38 +06:39 +06:40 +06:41 +06:42 +06:43 +06:44 +06:45 +06:46 +06:47 +06:48 +06:49 +06:50 +06:51 +06:52 +06:53 +06:54 +06:55 +06:56 +06:57 +06:58 +06:59 +07:00 +07:01 +07:02 +07:03 +07:04 +07:05 +07:06 +07:07 +07:08 +07:09 +07:10 +07:11 +07:12 +07:13 +07:14 +07:15 +07:16 +07:17 +07:18 +07:19 +07:20 +07:21 +07:22 +07:23 +07:24 +07:25 +07:26 +07:27 +07:28 +07:29 +07:30 +07:31 +07:32 +07:33 +07:34 +07:35 +07:36 +07:37 +07:38 +07:39 +07:40 +07:41 +07:42 +07:43 +07:44 +07:45 +07:46 +07:47 +07:48 +07:49 +07:50 +07:51 +07:52 +07:53 +07:54 +07:55 +07:56 +07:57 +07:58 +07:59 +08:00 +08:01 +08:02 +08:03 +08:04 +08:05 +08:06 +08:07 +08:08 +08:09 +08:10 +08:11 +08:12 +08:13 +08:14 +08:15 +08:16 +08:17 +08:18 +08:19 +08:20 +08:21 +08:22 +08:23 +08:24 +08:25 +08:26 +08:27 +08:28 +08:29 +08:30 +08:31 +08:32 +08:33 +08:34 +08:35 +08:36 +08:37 +08:38 +08:39 +08:40 +08:41 +08:42 +08:43 +08:44 +08:45 +08:46 +08:47 +08:48 +08:49 +08:50 +08:51 +08:52 +08:53 +08:54 +08:55 +08:56 +08:57 +08:58 +08:59 +09:00 +09:01 +09:02 +09:03 +09:04 +09:05 +09:06 +09:07 +09:08 +09:09 +09:10 +09:11 +09:12 +09:13 +09:14 +09:15 +09:16 +09:17 +09:18 +09:19 +09:20 +09:21 +09:22 +09:23 +09:24 +09:25 +09:26 +09:27 +09:28 +09:29 +09:30 +09:31 +09:32 +09:33 +09:34 +09:35 +09:36 +09:37 +09:38 +09:39 +09:40 +09:41 +09:42 +09:43 +09:44 +09:45 +09:46 +09:47 +09:48 +09:49 +09:50 +09:51 +09:52 +09:53 +09:54 +09:55 +09:56 +09:57 +09:58 +09:59 +10:00 +10:01 +10:02 +10:03 +10:04 +10:05 +10:06 +10:07 +10:08 +10:09 +10:10 +10:11 +10:12 +10:13 +10:14 +10:15 +10:16 +10:17 +10:18 +10:19 +10:20 +10:21 +10:22 +10:23 +10:24 +10:25 +10:26 +10:27 +10:28 +10:29 +10:30 +10:31 +10:32 +10:33 +10:34 +10:35 +10:36 +10:37 +10:38 +10:39 +10:40 +10:41 +10:42 +10:43 +10:44 +10:45 +10:46 +10:47 +10:48 +10:49 +10:50 +10:51 +10:52 +10:53 +10:54 +10:55 +10:56 +10:57 +10:58 +10:59 +11:00 +11:01 +11:02 +11:03 +11:04 +11:05 +11:06 +11:07 +11:08 +11:09 +11:10 +11:11 +11:12 +11:13 +11:14 +11:15 +11:16 +11:17 +11:18 +11:19 +11:20 +11:21 +11:22 +11:23 +11:24 +11:25 +11:26 +11:27 +11:28 +11:29 +11:30 +11:31 +11:32 +11:33 +11:34 +11:35 +11:36 +11:37 +11:38 +11:39 +11:40 +11:41 +11:42 +11:43 +11:44 +11:45 +11:46 +11:47 +11:48 +11:49 +11:50 +11:51 +11:52 +11:53 +11:54 +11:55 +11:56 +11:57 +11:58 +11:59 diff --git a/challenge-120/abigail/t/output-2-2.exp b/challenge-120/abigail/t/output-2-2.exp new file mode 100644 index 0000000000..46eb8814b3 --- /dev/null +++ b/challenge-120/abigail/t/output-2-2.exp @@ -0,0 +1,720 @@ +0 +5.5 +11 +16.5 +22 +27.5 +33 +38.5 +44 +49.5 +55 +60.5 +66 +71.5 +77 +82.5 +88 +93.5 +99 +104.5 +110 +115.5 +121 +126.5 +132 +137.5 +143 +148.5 +154 +159.5 +165 +170.5 +176 +178.5 +173 +167.5 +162 +156.5 +151 +145.5 +140 +134.5 +129 +123.5 +118 +112.5 +107 +101.5 +96 +90.5 +85 +79.5 +74 +68.5 +63 +57.5 +52 +46.5 +41 +35.5 +30 +24.5 +19 +13.5 +8 +2.5 +3 +8.5 +14 +19.5 +25 +30.5 +36 +41.5 +47 +52.5 +58 +63.5 +69 +74.5 +80 +85.5 +91 +96.5 +102 +107.5 +113 +118.5 +124 +129.5 +135 +140.5 +146 +151.5 +157 +162.5 +168 +173.5 +179 +175.5 +170 +164.5 +159 +153.5 +148 +142.5 +137 +131.5 +126 +120.5 +115 +109.5 +104 +98.5 +93 +87.5 +82 +76.5 +71 +65.5 +60 +54.5 +49 +43.5 +38 +32.5 +27 +21.5 +16 +10.5 +5 +0.5 +6 +11.5 +17 +22.5 +28 +33.5 +39 +44.5 +50 +55.5 +61 +66.5 +72 +77.5 +83 +88.5 +94 +99.5 +105 +110.5 +116 +121.5 +127 +132.5 +138 +143.5 +149 +154.5 +160 +165.5 +171 +176.5 +178 +172.5 +167 +161.5 +156 +150.5 +145 +139.5 +134 +128.5 +123 +117.5 +112 +106.5 +101 +95.5 +90 +84.5 +79 +73.5 +68 +62.5 +57 +51.5 +46 +40.5 +35 +29.5 +24 +18.5 +13 +7.5 +2 +3.5 +9 +14.5 +20 +25.5 +31 +36.5 +42 +47.5 +53 +58.5 +64 +69.5 +75 +80.5 +86 +91.5 +97 +102.5 +108 +113.5 +119 +124.5 +130 +135.5 +141 +146.5 +152 +157.5 +163 +168.5 +174 +179.5 +175 +169.5 +164 +158.5 +153 +147.5 +142 +136.5 +131 +125.5 +120 +114.5 +109 +103.5 +98 +92.5 +87 +81.5 +76 +70.5 +65 +59.5 +54 +48.5 +43 +37.5 +32 +26.5 +21 +15.5 +10 +4.5 +1 +6.5 +12 +17.5 +23 +28.5 +34 +39.5 +45 +50.5 +56 +61.5 +67 +72.5 +78 +83.5 +89 +94.5 +100 +105.5 +111 +116.5 +122 +127.5 +133 +138.5 +144 +149.5 +155 +160.5 +166 +171.5 +177 +177.5 +172 +166.5 +161 +155.5 +150 +144.5 +139 +133.5 +128 +122.5 +117 +111.5 +106 +100.5 +95 +89.5 +84 +78.5 +73 +67.5 +62 +56.5 +51 +45.5 +40 +34.5 +29 +23.5 +18 +12.5 +7 +1.5 +4 +9.5 +15 +20.5 +26 +31.5 +37 +42.5 +48 +53.5 +59 +64.5 +70 +75.5 +81 +86.5 +92 +97.5 +103 +108.5 +114 +119.5 +125 +130.5 +136 +141.5 +147 +152.5 +158 +163.5 +169 +174.5 +180 +174.5 +169 +163.5 +158 +152.5 +147 +141.5 +136 +130.5 +125 +119.5 +114 +108.5 +103 +97.5 +92 +86.5 +81 +75.5 +70 +64.5 +59 +53.5 +48 +42.5 +37 +31.5 +26 +20.5 +15 +9.5 +4 +1.5 +7 +12.5 +18 +23.5 +29 +34.5 +40 +45.5 +51 +56.5 +62 +67.5 +73 +78.5 +84 +89.5 +95 +100.5 +106 +111.5 +117 +122.5 +128 +133.5 +139 +144.5 +150 +155.5 +161 +166.5 +172 +177.5 +177 +171.5 +166 +160.5 +155 +149.5 +144 +138.5 +133 +127.5 +122 +116.5 +111 +105.5 +100 +94.5 +89 +83.5 +78 +72.5 +67 +61.5 +56 +50.5 +45 +39.5 +34 +28.5 +23 +17.5 +12 +6.5 +1 +4.5 +10 +15.5 +21 +26.5 +32 +37.5 +43 +48.5 +54 +59.5 +65 +70.5 +76 +81.5 +87 +92.5 +98 +103.5 +109 +114.5 +120 +125.5 +131 +136.5 +142 +147.5 +153 +158.5 +164 +169.5 +175 +179.5 +174 +168.5 +163 +157.5 +152 +146.5 +141 +135.5 +130 +124.5 +119 +113.5 +108 +102.5 +97 +91.5 +86 +80.5 +75 +69.5 +64 +58.5 +53 +47.5 +42 +36.5 +31 +25.5 +20 +14.5 +9 +3.5 +2 +7.5 +13 +18.5 +24 +29.5 +35 +40.5 +46 +51.5 +57 +62.5 +68 +73.5 +79 +84.5 +90 +95.5 +101 +106.5 +112 +117.5 +123 +128.5 +134 +139.5 +145 +150.5 +156 +161.5 +167 +172.5 +178 +176.5 +171 +165.5 +160 +154.5 +149 +143.5 +138 +132.5 +127 +121.5 +116 +110.5 +105 +99.5 +94 +88.5 +83 +77.5 +72 +66.5 +61 +55.5 +50 +44.5 +39 +33.5 +28 +22.5 +17 +11.5 +6 +0.5 +5 +10.5 +16 +21.5 +27 +32.5 +38 +43.5 +49 +54.5 +60 +65.5 +71 +76.5 +82 +87.5 +93 +98.5 +104 +109.5 +115 +120.5 +126 +131.5 +137 +142.5 +148 +153.5 +159 +164.5 +170 +175.5 +179 +173.5 +168 +162.5 +157 +151.5 +146 +140.5 +135 +129.5 +124 +118.5 +113 +107.5 +102 +96.5 +91 +85.5 +80 +74.5 +69 +63.5 +58 +52.5 +47 +41.5 +36 +30.5 +25 +19.5 +14 +8.5 +3 +2.5 +8 +13.5 +19 +24.5 +30 +35.5 +41 +46.5 +52 +57.5 +63 +68.5 +74 +79.5 +85 +90.5 +96 +101.5 +107 +112.5 +118 +123.5 +129 +134.5 +140 +145.5 +151 +156.5 +162 +167.5 +173 +178.5 +176 +170.5 +165 +159.5 +154 +148.5 +143 +137.5 +132 +126.5 +121 +115.5 +110 +104.5 +99 +93.5 +88 +82.5 +77 +71.5 +66 +60.5 +55 +49.5 +44 +38.5 +33 +27.5 +22 +16.5 +11 +5.5 -- cgit From 0242bf8e30c825fb37e1a19fb70e8d8b6a47957f Mon Sep 17 00:00:00 2001 From: Abigail Date: Mon, 5 Jul 2021 18:40:41 +0200 Subject: Solutions in 14 languages for week 120, part 2 --- challenge-120/abigail/README.md | 6 ++++ challenge-120/abigail/awk/ch-2.awk | 25 +++++++++++++++ challenge-120/abigail/bash/ch-2.sh | 38 ++++++++++++++++++++++ challenge-120/abigail/bc/ch-2.bc | 44 ++++++++++++++++++++++++++ challenge-120/abigail/befunge-93/ch-2.bf93 | 12 +++++++ challenge-120/abigail/c/ch-2.c | 33 +++++++++++++++++++ challenge-120/abigail/go/ch-2.go | 38 ++++++++++++++++++++++ challenge-120/abigail/java/ch-2.java | 45 ++++++++++++++++++++++++++ challenge-120/abigail/lua/ch-2.lua | 26 +++++++++++++++ challenge-120/abigail/node/ch-2.js | 26 +++++++++++++++ challenge-120/abigail/perl/ch-2.pl | 51 ++++++++++++++++++++++++++++++ challenge-120/abigail/python/ch-2.py | 27 ++++++++++++++++ challenge-120/abigail/r/ch-2.r | 28 ++++++++++++++++ challenge-120/abigail/ruby/ch-2.rb | 25 +++++++++++++++ challenge-120/abigail/t/ctest.ini | 3 ++ challenge-120/abigail/tcl/ch-2.tcl | 28 ++++++++++++++++ 16 files changed, 455 insertions(+) create mode 100644 challenge-120/abigail/awk/ch-2.awk create mode 100644 challenge-120/abigail/bash/ch-2.sh create mode 100644 challenge-120/abigail/bc/ch-2.bc create mode 100644 challenge-120/abigail/befunge-93/ch-2.bf93 create mode 100644 challenge-120/abigail/c/ch-2.c create mode 100644 challenge-120/abigail/go/ch-2.go create mode 100644 challenge-120/abigail/java/ch-2.java create mode 100644 challenge-120/abigail/lua/ch-2.lua create mode 100644 challenge-120/abigail/node/ch-2.js create mode 100644 challenge-120/abigail/perl/ch-2.pl create mode 100644 challenge-120/abigail/python/ch-2.py create mode 100644 challenge-120/abigail/r/ch-2.r create mode 100644 challenge-120/abigail/ruby/ch-2.rb create mode 100644 challenge-120/abigail/tcl/ch-2.tcl diff --git a/challenge-120/abigail/README.md b/challenge-120/abigail/README.md index f2ea164db7..84e672283a 100644 --- a/challenge-120/abigail/README.md +++ b/challenge-120/abigail/README.md @@ -74,12 +74,18 @@ Output: 120 degree ### Solutions * [AWK](awk/ch-2.awk) * [Bash](bash/ch-2.sh) +* [bc](bc/ch-2.bc) +* [Befunge-93](befunge-93/ch-2.bf93) * [C](c/ch-2.c) +* [Go](go/ch-2.go) +* [Java](java/ch-2.java) * [Lua](lua/ch-2.lua) * [Node.js](node/ch-2.js) * [Perl](perl/ch-2.pl) * [Python](python/ch-2.py) +* [R](r/ch-2.r) * [Ruby](ruby/ch-2.rb) +* [Tcl](tcl/ch-2.tcl) ### Blog [Clock Angle](https://abigail.github.io/HTML/Perl-Weekly-Challenge/week-120-2.html) diff --git a/challenge-120/abigail/awk/ch-2.awk b/challenge-120/abigail/awk/ch-2.awk new file mode 100644 index 0000000000..f7a32c7ee0 --- /dev/null +++ b/challenge-120/abigail/awk/ch-2.awk @@ -0,0 +1,25 @@ +#!/usr/bin/awk + +# +# See ../README.md +# + +# +# Run as: awk -f ch-2.awk < input-file +# + +BEGIN { + FS = ":" + DIFF_PER_MINUTE = 11 + MIN_PER_HOUR = 60 + FULL_CIRCLE = 720 +} + +{ + angle = (DIFF_PER_MINUTE * ($1 * MIN_PER_HOUR + $2)) % FULL_CIRCLE + if (2 * angle >= FULL_CIRCLE) { + angle = FULL_CIRCLE - angle + } + + print (angle / 2) +} diff --git a/challenge-120/abigail/bash/ch-2.sh b/challenge-120/abigail/bash/ch-2.sh new file mode 100644 index 0000000000..62540af529 --- /dev/null +++ b/challenge-120/abigail/bash/ch-2.sh @@ -0,0 +1,38 @@ +#!/bin/sh + +# +# See ../README.md +# + +# +# Run as: bash ch-2.sh < input-file +# + +set -f + +IFS=":" + +DIFF_PER_MINUTE=11 # Half degrees +MIN_PER_HOUR=60 +FULL_CIRCLE=720 # Half degrees + +while read hours minutes +do # + # Bash is going to interpret an hour (or minute) of the form "08" + # or "09" as an illegal octal number. So, we're going to use a + # trick: we prepend a 1, and subtract 100. + # + ((hours = "1$hours" - 100)) + ((minutes = "1$minutes" - 100)) + ((angle = (DIFF_PER_MINUTE * (hours * MIN_PER_HOUR + minutes)) % + FULL_CIRCLE)) + if ((2 * angle >= FULL_CIRCLE)) + then ((angle = FULL_CIRCLE - angle)) + fi + + printf "%d" $((angle / 2)) + if ((angle % 2 == 1)) + then printf ".5" + fi + echo +done diff --git a/challenge-120/abigail/bc/ch-2.bc b/challenge-120/abigail/bc/ch-2.bc new file mode 100644 index 0000000000..6897ea94a8 --- /dev/null +++ b/challenge-120/abigail/bc/ch-2.bc @@ -0,0 +1,44 @@ +# +# See ../README.md +# + +# +# Run as: bc ch-2.bc < input-file +# + +diff_per_minute = 11 +min_per_hour = 60 +full_circle = 720 + +while (1) { + hours = read () + if (hours == -1) { + break + } + minutes = read () + + # + # Result scale to 0, otherwise % acts weirdly. + # + scale = 0 + angle = (diff_per_minute * (hours * min_per_hour + minutes)) % full_circle + if (2 * angle >= full_circle) { + angle = full_circle - angle + } + + # + # If the angle is an odd number of half degrees, we want + # a result ending in ".5", but if it's an even number of + # half degrees, we don't want to end in ".0" + # + scale = angle % 2 + + # + # Little hack to print a half as "0.5" + # + if (angle == 1) { + "0" + } + + angle / 2 +} diff --git a/challenge-120/abigail/befunge-93/ch-2.bf93 b/challenge-120/abigail/befunge-93/ch-2.bf93 new file mode 100644 index 0000000000..103f2830bf --- /dev/null +++ b/challenge-120/abigail/befunge-93/ch-2.bf93 @@ -0,0 +1,12 @@ +> & :1+!#@_ ~$ 543*** &+ 65+ * 65432**** % : 6543*** `#v_v +^ v v +^ v<<<<<<<<<<<<<<<<<<<<<<<<<<< : 56+9* `!#v_ : 554** / "0"+, 554** % > : 55+ / "0"+, > 55+% "0"+, v +^ v ^ ^ v +^ >>>>>>>>>>>>>>>>>>> : 9 `#^_ ^ v +^ v +^ v ,,".5" < v +^ v ^ v +^<<<<<<<<<<<<<<<<<<<<<<<<<< , +55 <<<<<<<<<<<<<<<<<<<< _^# !%2 <<< diff --git a/challenge-120/abigail/c/ch-2.c b/challenge-120/abigail/c/ch-2.c new file mode 100644 index 0000000000..56e1872d0d --- /dev/null +++ b/challenge-120/abigail/c/ch-2.c @@ -0,0 +1,33 @@ +# include +# include +# include + +/* + * See ../README.md + */ + +/* + * Run as: cc -o ch-2.o ch-2.c; ./ch-2.o < input-file + */ + +# define DIFF_PER_MINUTE 11 /* Half degrees */ +# define MIN_PER_HOUR 60 +# define FULL_CIRCLE 720 /* Half degrees */ + +int main (void) { + int hours, minutes; + + while (scanf ("%d:%d", &hours, &minutes) == 2) { + int angle = (DIFF_PER_MINUTE * (hours * MIN_PER_HOUR + minutes)) % + FULL_CIRCLE; + if (2 * angle >= FULL_CIRCLE) { + angle = FULL_CIRCLE - angle; + } + printf ("%d", angle / 2); + if (angle % 2) { + printf (".5"); + } + printf ("\n"); + } + return (0); +} diff --git a/challenge-120/abigail/go/ch-2.go b/challenge-120/abigail/go/ch-2.go new file mode 100644 index 0000000000..a4a6f4ce95 --- /dev/null +++ b/challenge-120/abigail/go/ch-2.go @@ -0,0 +1,38 @@ +package main + +// +// See ../README.md +// + +// +// Run as: go run ch-2.go < input-file +// + +import ( + "fmt" +) + +var DIFF_PER_MINUTE = 11; +var MIN_PER_HOUR = 60; +var FULL_CIRCLE = 720; + +func main () { + var hours, minutes int; + for { + var n, err = fmt . Scanf ("%d:%d", &hours, &minutes) + if (err != nil || n != 2) { + break; + } + var angle = (DIFF_PER_MINUTE * (hours * MIN_PER_HOUR + minutes)) % + FULL_CIRCLE; + if (2 * angle >= FULL_CIRCLE) { + angle = FULL_CIRCLE - angle; + } + + fmt . Print (angle / 2); + if (angle % 2 == 1) { + fmt . Print (".5") + } + fmt . Print ("\n") + } +} diff --git a/challenge-120/abigail/java/ch-2.java b/challenge-120/abigail/java/ch-2.java new file mode 100644 index 0000000000..9ffeb322ee --- /dev/null +++ b/challenge-120/abigail/java/ch-2.java @@ -0,0 +1,45 @@ +// +// See ../README.md +// + +// +// Run as: ln ch-2.java ch2.java; javac ch2.java; java ch2 < input-file +// + +import java.util.*; + + +public class ch2 { + public static void main (String [] args) { + int DIFF_PER_MINUTE = 11; // Half degrees + int MIN_PER_HOUR = 60; + int FULL_CIRCLE = 720; // Half degrees + Scanner scanner = new Scanner (System . in); + try { + while (true) { + String line = scanner . nextLine (); + String [] parts = line . split (":"); + int hours = Integer . parseInt (parts [0]); + int minutes = Integer . parseInt (parts [1]); + + int angle = (DIFF_PER_MINUTE * + (hours * MIN_PER_HOUR + minutes)) % FULL_CIRCLE; + + if (2 * angle >= FULL_CIRCLE) { + angle = FULL_CIRCLE - angle; + } + + System . out . print (angle / 2); + if (angle % 2 == 1) { + System . out . print (".5"); + } + System . out . print ("\n"); + } + } + catch (Exception e) { + // + // EOF + // + } + } +} diff --git a/challenge-120/abigail/lua/ch-2.lua b/challenge-120/abigail/lua/ch-2.lua new file mode 100644 index 0000000000..31c18d0b5e --- /dev/null +++ b/challenge-120/abigail/lua/ch-2.lua @@ -0,0 +1,26 @@ +#!/opt/local/bin/lua + +-- +-- See ../README.md +-- + +-- +-- Run as: lua ch-2.lua < input-file +-- + +local DIFF_PER_MINUTE = 11 +local MIN_PER_HOUR = 60 +local FULL_CIRCLE = 720 + +for line in io . lines () do + local _, _, hours, minutes = line : find ("([0-9][0-9]):([0-9][0-9])") + hours = tonumber (hours) + minutes = tonumber (minutes) + local angle = (DIFF_PER_MINUTE * (hours * MIN_PER_HOUR + minutes)) % + FULL_CIRCLE + if 2 * angle >= FULL_CIRCLE + then angle = FULL_CIRCLE - angle + end + + print (angle / 2) +end diff --git a/challenge-120/abigail/node/ch-2.js b/challenge-120/abigail/node/ch-2.js new file mode 100644 index 0000000000..4159eafa6d --- /dev/null +++ b/challenge-120/abigail/node/ch-2.js @@ -0,0 +1,26 @@ +#!/usr/local/bin/node + +// +// See ../README.md +// + +// +// Run as: node ch-2.js < input-file +// + +let DIFF_PER_MINUTE = 11 +let MIN_PER_HOUR = 60 +let FULL_CIRCLE = 720 + + require ('readline') +. createInterface ({input: process . stdin}) +. on ('line', line => { + let [hours, minutes] = line . trim () . split (":") + angle = (DIFF_PER_MINUTE * (+hours * MIN_PER_HOUR + +minutes)) % + FULL_CIRCLE + if (2 * angle >= FULL_CIRCLE) { + angle = FULL_CIRCLE - angle + } + + console . log (angle / 2) +}) diff --git a/challenge-120/abigail/perl/ch-2.pl b/challenge-120/abigail/perl/ch-2.pl new file mode 100644 index 0000000000..b533e7d8a6 --- /dev/null +++ b/challenge-120/abigail/perl/ch-2.pl @@ -0,0 +1,51 @@ +#!/opt/perl/bin/perl + +use 5.032; + +use strict; +use warnings; +no warnings 'syntax'; + +use experimental 'signatures'; +use experimental 'lexical_subs'; + +# +# See ../README.md +# + +# +# Run as: perl ch-2.pl < input-file +# + +my $MIN_PER_HOUR = 60; +my $DIFF_PER_MINUTE = 11; # Half degrees +my $FULL_CIRCLE = 720; # Half degrees + +while (<>) { + my ($hours, $minutes) = /[0-9]+/g; + # + # Every minute, the angle between the hour and minute hand + # increases by 5.5 degrees. So, we will calculate how many + # minutes have passed since 00:00, multiply this by 11, giving + # us the number of half degrees between two hands. + # We normalize the angle by modding it by 720. To get the smaller + # angle, if the angle is more than 360, we subtract the angle + # from 720 (the full circle). + # + # Finally, we divide by 2, to get the answer in degrees. + # + + # + # Note that this is going to work regardless whether the + # time is given in 12 hour format, a 24 hour format, or + # the silly format Americans use. + # + + my $angle = ($DIFF_PER_MINUTE * ($hours * $MIN_PER_HOUR + $minutes)) % + $FULL_CIRCLE; + $angle = $FULL_CIRCLE - $angle if 2 * $angle >= $FULL_CIRCLE; + + say $angle / 2; +} + +__END__ diff --git a/challenge-120/abigail/python/ch-2.py b/challenge-120/abigail/python/ch-2.py new file mode 100644 index 0000000000..5c01644600 --- /dev/null +++ b/challenge-120/abigail/python/ch-2.py @@ -0,0 +1,27 @@ +#!/opt/local/bin/python + +# +# See ../README.md +# + +# +# Run as: python ch-2.py < input-file +# + +import fileinput + +DIFF_PER_MINUTE = 11 +MIN_PER_HOUR = 60 +FULL_CIRCLE = 720 + +for line in fileinput . input (): + hours, minutes = line . strip () . split (":") + angle = (DIFF_PER_MINUTE * (int (hours) * MIN_PER_HOUR + int (minutes))) \ + % FULL_CIRCLE + if 2 * angle >= FULL_CIRCLE: + angle = FULL_CIRCLE - angle + + print ("{}" . format (int (angle / 2)), end = '') + if angle % 2: + print (".5", end = '') + print ("") diff --git a/challenge-120/abigail/r/ch-2.r b/challenge-120/abigail/r/ch-2.r new file mode 100644 index 0000000000..8fd4497435 --- /dev/null +++ b/challenge-120/abigail/r/ch-2.r @@ -0,0 +1,28 @@ +# +# See ../README.md +# + +# +# Run as: Rscript ch-2.r < input-file +# + +DIFF_PER_MINUTE <- 11 +MIN_PER_HOUR <- 60 +FULL_CIRCLE <- 720 + +stdin <- file ('stdin', 'r') +repeat { + time <- readLines (stdin, n = 1) + if (length (time) == 0) { + break + } + parts <- strsplit (time, ":") + hours <- as.numeric (parts [[1]] [[1]]) + minutes <- as.numeric (parts [[1]] [[2]]) + angle <- (DIFF_PER_MINUTE * (hours * MIN_PER_HOUR + minutes)) %% + FULL_CIRCLE + if (2 * angle >= FULL_CIRCLE) { + angle <- FULL_CIRCLE - angle + } + cat (angle / 2, "\n") +} diff --git a/challenge-120/abigail/ruby/ch-2.rb b/challenge-120/abigail/ruby/ch-2.rb new file mode 100644 index 0000000000..ad72cfa1fa --- /dev/null +++ b/challenge-120/abigail/ruby/ch-2.rb @@ -0,0 +1,25 @@ +#!/usr/bin/ruby + +# +# See ../README.md +# + +# +# Run as: ruby ch-2.rb < input-file +# + +diff_per_minute = 11 +min_per_hour = 60 +full_circle = 720 + +ARGF . each_line do + |time| + hours, minutes = time . split (/:/) + angle = (diff_per_minute * (hours . to_i * min_per_hour + minutes . to_i))\ + % full_circle + angle = full_circle - angle if 2 * angle >= full_circle + + print (angle / 2) + print (".5") if angle % 2 == 1 + print ("\n") +end diff --git a/challenge-120/abigail/t/ctest.ini b/challenge-120/abigail/t/ctest.ini index 4bfae653d8..4cd56690f3 100644 --- a/challenge-120/abigail/t/ctest.ini +++ b/challenge-120/abigail/t/ctest.ini @@ -11,3 +11,6 @@ [challenges/1/bc] add_to_input = 0 + +[challenges/2/bc] +add_to_input = -1 diff --git a/challenge-120/abigail/tcl/ch-2.tcl b/challenge-120/abigail/tcl/ch-2.tcl new file mode 100644 index 0000000000..44989af0e1 --- /dev/null +++ b/challenge-120/abigail/tcl/ch-2.tcl @@ -0,0 +1,28 @@ +# +# See ../README.md +# + +# +# Run as: tclsh ch-2.tcl < input-file +# + +set DIFF_PER_MINUTE 11 +set MIN_PER_HOUR 60 +set FULL_CIRCLE 720 + +while {[gets stdin line] >= 0} { + set parts [split $line :] + set hours [expr 1[lindex $parts 0] - 100] + set minutes [expr 1[lindex $parts 1] - 100] + set angle [expr (($DIFF_PER_MINUTE * \ + ($hours * $MIN_PER_HOUR + $minutes))) % $FULL_CIRCLE] + if {2 * $angle >= $FULL_CIRCLE} { + set angle [expr $FULL_CIRCLE - $angle] + } + + puts -nonewline [expr $angle / 2] + if {$angle % 2 == 1} { + puts -nonewline ".5" + } + puts "" +} -- cgit