入力としてx軸とy軸に対して対称なマップの4分の1が与えられます。プログラムは完全なマップを印刷する必要があります。
マップには次の文字を含めることができます:-+/\|.、それらは期待どおりに回転する必要があります。入力データは常に長方形で小さくなります。
例
$ cat in
+---
|./.
|/..
$ ./solution < in
+------+
|./..\.|
|/....\|
|\..../|
|.\../.|
+------+
最短のコードが優先されます。
入力としてx軸とy軸に対して対称なマップの4分の1が与えられます。プログラムは完全なマップを印刷する必要があります。
マップには次の文字を含めることができます:-+/\|.、それらは期待どおりに回転する必要があります。入力データは常に長方形で小さくなります。
$ cat in
+---
|./.
|/..
$ ./solution < in
+------+
|./..\.|
|/....\|
|\..../|
|.\../.|
+------+
最短のコードが優先されます。
回答:
║Q↷↷
キャンバスの最初の答えです。簡単なものから始めましょう。:)
@dzaimaのおかげで-1バイト。
Canvasでミラーリングまたは回転すると、スラッシュは自動的に変換されます。
1バイトだったかもしれません╬(オンラインで試してみてください)が、残念ながらドット.を一重引用符に変換します'水平方向にミラーリングするときににます。
説明:
# (Take the multi-line input implicitly as canvas object)
║ # Palindromize the canvas object (without overlap)
Q # Output it with a trailing newline (without popping)
↷↷ # Rotated the canvas object that's still on the stack by 90 degrees twice
# (and output it implicitly as well at the end)
filter x{$_-split'/'-replace'\\','/'-join'\'}$input|%{$_+-join($_[40..0]|x)}|%{$_
$s=,($_|x)+$s}
$s
ノート:
テスト:
> gc in| .\map.ps1
+------+
|./..\.|
|/....\|
|\..../|
|.\../.|
+------+
> gc in2
+\/
/\/
> gc in2| .\map.ps1
+\/\/+
/\/\/\
\/\/\/
+/\/\+
歴史
OFSに保存し-joinて保存99..0します。-replace配列に対する作品、私は3は必要ありませんので、-replaceSを行うことができますが-split、-replace、-join代わりに。$a以降は必要ありません99..0頻繁に使用され。filter定義後に空白を挿入する必要はありません。削除し$x、代わりに配列の最初の実行中にすべての行を収集してから、後半に出力します。t=->s{s.tr'/\\\\','\\\\/'}
puts a=$<.map{|l|l.chop!+t[l.reverse]}
puts a.reverse.map &t
D:\tmp>ruby cg_sym_map.rb < sym_map.in.
+------+
|./..\.|
|/....\|
|\..../|
|.\../.|
+------+
S‖M⌈
@Neilのおかげで-1バイト。
木炭は自動的にスラッシュを正しく反映します。
説明:
入力を文字列として受け取ります。
InputString()
S
反射鏡を右と下の両方にミラーリングします(:⌈の組み込み機能です:Right, :Down):
ReflectMirror(:⌈)
‖M⌈
⌈、1バイトで正しい方向と下方向を提供するなど、さらに多方向性を追加しました。
⌈Verboseコードではどのように使用しますか?:RightDownもちろん、私たちが望む結果にはなりません。
:は、Verboseモードでプレフィックスを取得します。
:Right:Down、または::RightDown、または他の何か?ただし、これらの2つの結果のどちらも⌈、エンコードされたバージョンでは-vl引数を提供しません。arg S‖M⌈を使用する場合、冗長コードは何を取得する必要があり-vlますか?
ReflectMirror(:⌈)
シェルスクリプト!!
#!/bin/sh
rm temp
touch temp
file=$1
for STRING in `cat $1`
do
printf $STRING >> temp
for ((COUNT=0; COUNT<${#STRING}; COUNT++))
do
RECORD[$COUNT]=${STRING:$COUNT:1}
done
for ((REV_COUNT=${#STRING}; REV_COUNT>=0; REV_COUNT--))
do
if [ "${RECORD[$REV_COUNT]}" = "\\" ]; then
printf "/" >> temp
elif [ "${RECORD[$REV_COUNT]}" = "/" ]; then
printf "\\" >> temp
else
printf "${RECORD[$REV_COUNT]}" >> temp
fi
done
echo >> temp
done
cat temp
tac temp > temp2
for STRING in `cat temp2`
do
for ((COUNT=0; COUNT<${#STRING}; COUNT++))
do
RECORD[$COUNT]=${STRING:$COUNT:1}
if [ "${RECORD[$COUNT]}" = "\\" ]; then
printf "/"
elif [ "${RECORD[$COUNT]}" = "/" ]; then
printf "\\"
else
printf "${RECORD[$COUNT]}"
fi
done
echo
done
I / O
./solution in
+------+
|./..\.|
|/....\|
|\..../|
|.\../.|
+------+
CJamはこのチャレンジよりも新しいため、この回答は緑色のチェックマークの対象ではありませんが、とにかく楽しい練習でした
qN/{{_W%"\/"_W%er+}%z}2*N*
qN/{{_W%"\/"_W%er+}%z}2*N*
qN/ "Read STDIN and split on newlines.";
{ }2* "Execute this block twice.";
{ }% "Map this block onto each line.";
_W% "Duplicate and reverse.";
"\/" "Push the string '\/'.";
_W% "Duplicate and reverse.";
er "Character transliteration, swaps slashes and backslashes.";
+ "Append to first half of the line.";
z "Zip, i.e. transpose the map.";
N* "Join with newlines.";
最後の転置により、列に沿って2回目の反転が実行されます。最後に、マップを再度転置するため、元の向きになります。
ジョーイの答えに触発されました。
filter x{$_;$_[40..0]|%{$_-split'/'-replace'\\','/'-join'\'}},($args|%{-join(,($_|% t*y)|x)})|x
注:40著者がコメントを投稿するためLet's say the input is at most 16 rows and 40 characters。
テストスクリプト:
$f = {
filter x{$_;$_[40..0]|%{$_-split'/'-replace'\\','/'-join'\'}}
,($args|%{-join(,($_|% t*y)|x)})|x
}
@(
,( ("+---",
"|./.",
"|/.."),
"+------+",
"|./..\.|",
"|/....\|",
"|\..../|",
"|.\../.|",
"+------+")
,( ("+\/",
"/\/"),
"+\/\/+",
"/\/\/\",
"\/\/\/",
"+/\/\+")
,( ("+---",
"|...",
"|..\"),
"+------+",
"|......|",
"|..\/..|",
"|../\..|",
"|......|",
"+------+")
) | % {
$m,$expected = $_
$result = &$f @m
"$result"-eq"$expected"
$result
}
出力:
True
+------+
|./..\.|
|/....\|
|\..../|
|.\../.|
+------+
True
+\/\/+
/\/\/\
\/\/\/
+/\/\+
True
+------+
|......|
|..\/..|
|../\..|
|......|
+------+
t=->s{s.tr '/\\\\','\\\\/'}
$<.read.split.map{|l|print l+=t[l.reverse]+"
"
l}.reverse.map{|l|print t[l]}
n%{.-1%'/'/{'\\'/'/'*}%'\\'*+}%.-1%{-1%}%+n*
結果
$ cat in2
+-/|/\
/\|//-
$ cat in2 | golfscript codegolf-761.gs
+-/|/\/\|\-+
/\|//--\\|/\
\/|\\--//|\/
+-\|\/\/|/-+
n%{.-1%'/'/'\\'*+}%.-1%{-1%}%+n*
結果
$ cat in
+---
|./.
|/..
$ cat in | golfscript codegolf-761.gs
+------+
|./..\.|
|/....\|
|\..../|
|.\../.|
+------+
$
c '/'='\\';c '\\'='/';c x=x;i=(c<$>)
q#x=x++q(reverse x)
f=((i<$>)#).map(i#)
-- Only / and \ get converted, all other chars are passed as is
c '/'='\\';c '\\'='/';c x=x
-- "Invert" the string (that is switch all / and \ in it)
-- Just map our conversion function over the string
i = (c<$>)
-- Helper: Concatenate a list with its reversed copy (with the given function applied to the copy)
q # x = x ++ q (reverse x)
-- the resulting function:
f = ((i<$>)#) . -- produce the lower half of the image by reversing the upper half and inverting slashes in each line
map (i#) -- produce the upper half or the image (by concating each input line with its reversed, inverted version)
入力:
DECLARE @s VARCHAR(100)= '+ ---' + CHAR(10)+ '| ...' + CHAR(10)+ '| .. \';
圧縮された:
declare @t TABLE(l INT IDENTITY(1,1)、s CHAR(40)); INSERT INTO @t(s)SELECT value + TRANSLATE(REVERSE(value)、 '\ /'、 '/ \')FROM STRING_SPLIT (@ s、char(10)); SELECT s FROM(SELECT l、s FROM @t UNION ALL SELECT 1e3-l、TRANSLATE(s、 '\ /'、 '/ \')FROM @t)b ORDER BY l
人間が読める:
宣言@t TABLE(l INT IDENTITY(1,1)、s CHAR(40)); @t(s)への挿入 SELECT value + TRANSLATE(REVERSE(value)、 '\ /'、 '/ \') FROM STRING_SPLIT(@ s、char(10)); SELECT から( SELECT l、s FROM @t UNION ALL SELECT 1e3-l、TRANSLATE(s、 '\ /'、 '/ \')FROM @t )b ORDER BY l
出力(管理スタジオのテキストとして):
+ ------ + | ...... | | .. \ / .. | | ../ \ .. | | ...... | + ------ + (6行が影響を受けます)