diff options
| -rw-r--r-- | challenge-121/abigail/README.md | 3 | ||||
| -rw-r--r-- | challenge-121/abigail/awk/ch-1.gawk | 13 | ||||
| -rw-r--r-- | challenge-121/abigail/bash/ch-1.sh | 20 | ||||
| -rw-r--r-- | challenge-121/abigail/bc/ch-1.bc | 26 | ||||
| -rw-r--r-- | challenge-121/abigail/befunge-93/ch-1.bf93 | 6 | ||||
| -rw-r--r-- | challenge-121/abigail/c/ch-1.c | 21 | ||||
| -rw-r--r-- | challenge-121/abigail/go/ch-1.go | 25 | ||||
| -rw-r--r-- | challenge-121/abigail/java/ch-1.java | 27 | ||||
| -rw-r--r-- | challenge-121/abigail/lua/ch-1.lua | 23 | ||||
| -rw-r--r-- | challenge-121/abigail/node/ch-1.js | 16 | ||||
| -rw-r--r-- | challenge-121/abigail/pascal/ch-1.p | 18 | ||||
| -rw-r--r-- | challenge-121/abigail/perl/ch-1.pl | 31 | ||||
| -rw-r--r-- | challenge-121/abigail/python/ch-1.py | 15 | ||||
| -rw-r--r-- | challenge-121/abigail/r/ch-1.r | 20 | ||||
| -rw-r--r-- | challenge-121/abigail/ruby/ch-1.rb | 15 | ||||
| -rw-r--r-- | challenge-121/abigail/scheme/ch-1.scm | 22 | ||||
| -rw-r--r-- | challenge-121/abigail/tcl/ch-1.tcl | 12 |
17 files changed, 312 insertions, 1 deletions
diff --git a/challenge-121/abigail/README.md b/challenge-121/abigail/README.md index a89019cc7b..2112493fea 100644 --- a/challenge-121/abigail/README.md +++ b/challenge-121/abigail/README.md @@ -33,6 +33,7 @@ Output: 26 * [AWK](awk/ch-1.awk) * [Bash](bash/ch-1.sh) * [bc](bc/ch-1.bc) +* [Befunge-93](befunge-93/ch-1.bf) * [C](c/ch-1.c) * [Go](go/ch-1.go) * [Java](java/ch-1.java) @@ -71,7 +72,7 @@ Output: ### Solutions ### Blog -[he Travelling Salesman][blog2] +[The Travelling Salesman][blog2] diff --git a/challenge-121/abigail/awk/ch-1.gawk b/challenge-121/abigail/awk/ch-1.gawk new file mode 100644 index 0000000000..2dac5aea72 --- /dev/null +++ b/challenge-121/abigail/awk/ch-1.gawk @@ -0,0 +1,13 @@ +#!/opt/local/bin/gawk + +# +# See ../README.md +# + +# +# Run as: gawk -f ch-1.gawk < input-file +# + +{ + print xor ($1, lshift (1, $2 - 1)) +} diff --git a/challenge-121/abigail/bash/ch-1.sh b/challenge-121/abigail/bash/ch-1.sh new file mode 100644 index 0000000000..affeeb846e --- /dev/null +++ b/challenge-121/abigail/bash/ch-1.sh @@ -0,0 +1,20 @@ +#!/bin/sh + +# +# See ../README.md +# + +# +# Run as: bash ch-1.sh < input-file +# + +set -f + +while read m n +do ((n = 2 ** (n - 1))) + if (((m / n) % 2)) + then ((m = m - n)) + else ((m = m + n)) + fi + echo $m +done diff --git a/challenge-121/abigail/bc/ch-1.bc b/challenge-121/abigail/bc/ch-1.bc new file mode 100644 index 0000000000..70a4aec17b --- /dev/null +++ b/challenge-121/abigail/bc/ch-1.bc @@ -0,0 +1,26 @@ +# +# See ../README.md +# + +# +# Run as: bc ch-1.bc < input-file +# +# The input-file should end with -1 +# + +while (1) { + m = read () + if (m == -1) { + break + } + n = read () + p = 2 ^ (n - 1) + b = (m / p) % 2 + if (b == 1) { + m = m - p + } + if (b == 0) { + m = m + p + } + m +} diff --git a/challenge-121/abigail/befunge-93/ch-1.bf93 b/challenge-121/abigail/befunge-93/ch-1.bf93 new file mode 100644 index 0000000000..b001430bab --- /dev/null +++ b/challenge-121/abigail/befunge-93/ch-1.bf93 @@ -0,0 +1,6 @@ +>>> & :1+!#@_ :& 1- 1 >> \ :! #v_ 1- \ 2* v +^ ^ $ v +^ ^<<<<<<< <<<<<<<<<< +^ v - < v +^< ,+55 . < | %2 \g11 / p11: < + ^ + < diff --git a/challenge-121/abigail/c/ch-1.c b/challenge-121/abigail/c/ch-1.c new file mode 100644 index 0000000000..5232ef7262 --- /dev/null +++ b/challenge-121/abigail/c/ch-1.c @@ -0,0 +1,21 @@ +# include <stdlib.h> +# include <stdio.h> +# include <string.h> + +/* + * See ../README.md + */ + +/* + * Run as: cc -o ch-1.o ch-1.c; ./ch-1.o < input-file + */ + +int main (void) { + int m, n; + + while (scanf ("%d %d", &m, &n) == 2) { + printf ("%d\n", m ^ (1 << -- n)); + } + + return (0); +} diff --git a/challenge-121/abigail/go/ch-1.go b/challenge-121/abigail/go/ch-1.go new file mode 100644 index 0000000000..32f293ac3a --- /dev/null +++ b/challenge-121/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 () { + var m, n, c int + + for { + c, _ = fmt . Scanf ("%d %d", &m, &n) + if (c != 2) { + break + } + fmt . Printf ("%d\n", m ^ (1 << (n - 1))) + } +} diff --git a/challenge-121/abigail/java/ch-1.java b/challenge-121/abigail/java/ch-1.java new file mode 100644 index 0000000000..db7264601e --- /dev/null +++ b/challenge-121/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 m = scanner . nextInt (); + int n = scanner . nextInt (); + System . out . println (m ^ (1 << (n - 1))); + } + } + catch (Exception e) { + // + // EOF + // + } + } +} diff --git a/challenge-121/abigail/lua/ch-1.lua b/challenge-121/abigail/lua/ch-1.lua new file mode 100644 index 0000000000..52b7529007 --- /dev/null +++ b/challenge-121/abigail/lua/ch-1.lua @@ -0,0 +1,23 @@ +#!/opt/local/bin/lua + +-- +-- See ../README.md +-- + +-- +-- Run as: lua ch-1.lua < input-file +-- + +for line in io . lines () do + _, _, m, n = line : find ("(%d+)%s+(%d+)") + x = 1 + for i = 1, n - 1 do + x = x * 2 + end + if math . floor (m / x) % 2 == 1 then + m = m - x + else + m = m + x + end + print (m) +end diff --git a/challenge-121/abigail/node/ch-1.js b/challenge-121/abigail/node/ch-1.js new file mode 100644 index 0000000000..77ccfc32f2 --- /dev/null +++ b/challenge-121/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', line => { + [m, n] = line . split (" ") . map (_ => +_) + console . log (m ^ (1 << -- n)) +}) diff --git a/challenge-121/abigail/pascal/ch-1.p b/challenge-121/abigail/pascal/ch-1.p new file mode 100644 index 0000000000..2b2c5c71d0 --- /dev/null +++ b/challenge-121/abigail/pascal/ch-1.p @@ -0,0 +1,18 @@ +Program XXX; + +(* *) +(* See ../README.md *) +(* *) + +(* *) +(* Run as: fpc -och-1.out ch-1.p; ./ch-1.out < input-file *) +(* *) + +var m, n: integer; + +begin + while not eof () do begin + readln (m, n); + writeln (m xor (1 shl (n - 1))); + end +end. diff --git a/challenge-121/abigail/perl/ch-1.pl b/challenge-121/abigail/perl/ch-1.pl new file mode 100644 index 0000000000..3ac18cee35 --- /dev/null +++ b/challenge-121/abigail/perl/ch-1.pl @@ -0,0 +1,31 @@ +#!/opt/perl/bin/perl -pla + +use 5.032; + +use strict; +use warnings; +no warnings 'syntax'; + +use experimental 'signatures'; +use experimental 'lexical_subs'; + +# +# See ../README.md +# + +# +# Run as: perl -pla ch-1.pl < input-file +# + +# +# Third week in a row with some trivial bit fiddling... +# +# Restricting ourselves to 8 bit input doesn't make the code any simpler +# -- this code works up to $m = 2^63 - 1, $n = 63 (or 2^31 - 1/31 if +# you're running a 32-bit integer perl). +# + +$_=$F[0]^1<<--$F[1] + + +__END__ diff --git a/challenge-121/abigail/python/ch-1.py b/challenge-121/abigail/python/ch-1.py new file mode 100644 index 0000000000..559b5e16d0 --- /dev/null +++ b/challenge-121/abigail/python/ch-1.py @@ -0,0 +1,15 @@ +#!/opt/local/bin/python + +# +# See ../README.md +# + +# +# Run as: python ch-1.py < input-file +# + +import fileinput + +for line in fileinput . input (): + [m, n] = map (lambda _: int (_), line . rstrip () . split (" ")) + print (m ^ (1 << (n - 1))) diff --git a/challenge-121/abigail/r/ch-1.r b/challenge-121/abigail/r/ch-1.r new file mode 100644 index 0000000000..329fa2e84c --- /dev/null +++ b/challenge-121/abigail/r/ch-1.r @@ -0,0 +1,20 @@ +# +# See ../README.md +# + +# +# Run as: Rscript ch-1.r < input-file +# + +stdin <- file ('stdin', 'r') +repeat { + line <- readLines (stdin, n = 1) + if (length (line) == 0) { + break + } + parts <- strsplit (line, " ") + m <- as.numeric (parts [[1]] [[1]]) + n <- as.numeric (parts [[1]] [[2]]) + + cat (bitwXor (m, (bitwShiftL (1, n - 1))), "\n") +} diff --git a/challenge-121/abigail/ruby/ch-1.rb b/challenge-121/abigail/ruby/ch-1.rb new file mode 100644 index 0000000000..b677ed8ead --- /dev/null +++ b/challenge-121/abigail/ruby/ch-1.rb @@ -0,0 +1,15 @@ +#!/usr/bin/ruby + +# +# See ../README.md +# + +# +# Run as: ruby ch-1.rb < input-file +# + +ARGF . each_line do + |line| + (m, n) = line . split(" ") . map {|_| _ . to_i} + puts (m ^ (1 << (n - 1))) +end diff --git a/challenge-121/abigail/scheme/ch-1.scm b/challenge-121/abigail/scheme/ch-1.scm new file mode 100644 index 0000000000..742359b040 --- /dev/null +++ b/challenge-121/abigail/scheme/ch-1.scm @@ -0,0 +1,22 @@ +;;; +;;; See ../README.md +;;; + +;;; +;;; Run as: guile --no-auto-compile ch-1.scm +;;; + +(use-modules (ice-9 format)) + +(define (main) + (define m (read)) + (define n (read)) + (if (not (eof-object? m)) + (begin + (format #t "~d\n" (logxor m (ash 1 (- n 1)))) + (main) + ) + ) +) + +(main) diff --git a/challenge-121/abigail/tcl/ch-1.tcl b/challenge-121/abigail/tcl/ch-1.tcl new file mode 100644 index 0000000000..5fe52269a8 --- /dev/null +++ b/challenge-121/abigail/tcl/ch-1.tcl @@ -0,0 +1,12 @@ +# +# See ../README.md +# + +# +# Run as: tclsh ch-1.tcl < input-file +# + +while {[gets stdin line] >= 0} { + lassign [split $line " "] m n + puts [expr $m ^ (1 << ($n - 1))] +} |
