# Magrathea 2.0-建筑山脉

50

### 输出量

• 输入中的每个数字`^`都恰好在数字指示的高度处代表一个峰（）（即9是最高高度）。
• 输出中不得有其他峰值（即在输入中有点的地方）。
• 山为三角形，即使用`/``\`字符创建坡度。
• 使用字符塑造两座山重叠的通行证`v`
• 没有多余的换行符或空行。
• 带有尾随空格的填充线是可选的。

### 例子

``````1
^

11
^^

1.2.
^
^/ \

.2.3..
^
^/ \
/    \

.2..3..
^
^ / \
/ v   \

...4...3...3..
^
/ \  ^   ^
/   \/ \ / \
/        v   \
``````

1

devnull 2014年

durron597 2014年

@ durron597输出不应有多余的换行符，请查看示例。
2014年

Paul Prestidge 2014年

@Chron是的，没关系。
2014年

11

## Javascript：272 268 233 232 201 192 189 188 178 180个字符

``p=prompt(r=t='');s=' ';for(d=10;d--;r=s+q+s,t+=q.trim()?q+'\n':'')for(q='',i=0;i<p.length;)q+=' \\/v^'[p[i++]==d?4:(/\^|\\/.test(r[i-1])+2*/\^|\//.test(r[i+1]))*(r[i]==s)];alert(t)``

``````// The output initialization is just a golfing trick suggested by @manatwork.
input = prompt(state = output = '');
space = ' ';

// Repeat for each line, from the top (the highest peak, highest digit) to the floor (digit 1). Start at 10 to avoid a bug.
for (digit = 10; digit--;

// Update the state of our automaton, at the end of the iteration.
// Add a space after and before to simplify the future pattern recognization.
state = space + line + space,

// Add the line to the output if it is not an empty line, at the end of the iteration.
output += line.trim() ? q + '\n' : '')
{ // This curly brace was added for readability, it is not in the golfed source.

// Analyze each character in the current state to produce a new state, like a cellular automaton.
for (line = '', i = 0; i < input.length;)
{ // This curly brace was added for readability, it is not in the golfed source.
line +=

// If the input is the current digit number, evaluate to 4 and put a peak in this character.
// Otherwise evaluate this expression with those rules:
// 1 means that the hill is higher only at right in the previous iteration, we do climb it to the right in this one.
// 2 means that the hill is higher only at left in the previous iteration, we do climb it to the left in this one.
// 3 means that the hill is higher at both sides in the previous iteration, we are in a v-shaped valley.
// 0 means nothing to do here. If the middle is not a space, it will be multiplied by 0 and become 0.
' \\/v^'[input[i++] == digit ? 4 : (/\^|\\/.test(state[i - 1]) + 2 * /\^|\//.test(state[i + 1])) * (r[i] == space)];
} // This curly brace was added for readability, it is not in the golfed source.
} // This curly brace was added for readability, it is not in the golfed source.

// Give the final output.
``````

manatwork 2014年

@manatwork谢谢。做完了
Victor Stafusa 2014年

user2846289 2014年

1
@VadimR谢谢，已修复。这是在受污染的环境中对其进行测试的副作用。需要在代码前加上前缀`delete r; delete s; delete q; delete p; delete t; delete i; delete d;`以确保其不受污染。
Victor Stafusa 2014年

`q.trim()?q+'\n':''`可能是`q.trim()&&q+'\n'`，节省两个。另外，`i<p.length`可能就是`p[i]`
Nicholas Pipitone

6

## 红宝石， 208201 189

``````gets.size.times{|x|0.upto(h=\$_[x].to_i-1){|d|r=\$*[h-d]||=' '*~/\$/
[x+d,x-d].map{|o|r[o]=r[o]>?!??v:o<x ??/:?\\if r[o]<?w}
d<1?r[x]=?^:r[x-d+1,w=2*d-1]=?w*w}}
puts\$*.reverse.*(\$/).tr(?w,' ')
``````

``````s=gets
9.downto(1){|h|\$0=(-1..s.size).map{|x|\$_=\$0[x,3]
s[x]=="#{h}"??^:~/  [\^\/]/??/:~/[\^\\]  /??\\:~/[\^\\] [\^\/]/??v:' '}*''
\$*<<\$0[1..-2]if\$0=~/\S/}
puts\$*
``````

``````....5.....6..6.....
^  ^
^    / \/ \
/ \  /      \
/   \/        \
/               \
/                 \
``````

1

2014年

4

C＃-588个字符-虽然不如Ray的321！

``````class P{static void Main(string[] a){char[,] w=new char[a[0].Length+1,10];int x=0;foreach(char c in a[0]){if(c!='.'){int h=int.Parse(c+"");if(w[x,h]=='\0')w[x,h]='^';int s=1;for(int l=h-1;l>0;l--){for(int m=x-s;m<=x+s;m++){if(w[m,l]!='\0'){if(w[m,l]=='^')w[m,l]='/';if(w[m,l]=='\\')w[m,l]='v';}else{if(m==x-s)w[m,l]='/';else if(m==x+s)w[m,l]='\\';else w[m,l]='\0';}bool t=false;for(int f=9;f>0;f--){if(t)w[m,f]='\0';if(w[m,f]!='\0')t=true;}}s++;}}x++;}for(int k=9;k>0;k--){string u="";for(int j=0;j<w.GetLength(0);j++){u+=w[j,k];}if(u.Replace("\0","")!="")System.Console.WriteLine(u);}}}
``````

``````F:\>mountains ".2..3..4..."
^
^ / \
^ / v   \
/ v       \
``````

``````F:\>mountains ".2..3..6.....5...3......1..3..4....2."
^
/ \    ^
/   \  / \               ^
/     \/   \ ^         ^ / \
^ /            v \       / v   \  ^
/ v                \    ^/       \/ \
``````

2
“复杂的一”格式不正确，“ 3”没有峰值。
user2846289 2014年

4

# APL，65字节

`⍉⌽↑⌽¨h↑¨'^/v\'[1+(~×a)×2+×2+/2-/0,0,⍨h←¯1+⊃⌈/a-↓|∘.-⍨⍳⍴a←11|⎕d⍳⍞]`

`⍞` 此符号返回原始（未评估）输入作为字符数组。

``````      s←'...4...3...3..' ⍝ let's use s instead of ⍞
⎕d ⍝ the digits
0123456789
⎕d⍳s ⍝ the indices of s in ⎕d or 11-s if not found
11 11 11 5 11 11 11 4 11 11 11 4 11 11
11|⎕d⍳s ⍝ modulo 11, so '.' is 0 instead of 11
0 0 0 5 0 0 0 4 0 0 0 4 0 0
a←11|⎕d⍳s ⍝ remember it, we'll need it later
⍴a ⍝ length of a
14
⍳⍴a
1 2 3 4 5 6 7 8 9 10 11 12 13 14
⍝ ∘.-    subtraction table
⍝ ∘.-⍨A  same as: A ∘.- A
⍝ |      absolute value
|∘.-⍨⍳⍴a
0  1  2  3 4 5 6 7 8 9 10 11 12 13
1  0  1  2 3 4 5 6 7 8  9 10 11 12
2  1  0  1 2 3 4 5 6 7  8  9 10 11
...
13 12 11 10 9 8 7 6 5 4  3  2  1  0
⍝ ↓      split the above matrix into rows
⍝ a-     elements of "a" minus corresponding rows
⍝ ⊃⌈/    max them together
⊃⌈/a-↓|∘.-⍨⍳⍴a
2 3 4 5 4 3 3 4 3 2 3 4 3 2
⍝ This describes the desired landscape,
⍝ except that it's a little too high.
⍝ Add -1 to correct it:
¯1+⊃⌈/a-↓|∘.-⍨⍳⍴a
1 2 3 4 3 2 2 3 2 1 2 3 2 1
⍝ Perfect!  Call it "h":
h←¯1+⊃⌈/a-↓|∘.-⍨⍳⍴a
0,⍨h ⍝ append a 0 (same as h,0)
1 2 3 4 3 2 2 3 2 1 2 3 2 1 0
0,0,⍨h ⍝ also prepend a 0
0 1 2 3 4 3 2 2 3 2 1 2 3 2 1 0
2-/0,0,⍨h ⍝ differences of pairs of consecutive elements
¯1 ¯1 ¯1 ¯1 1 1 0 ¯1 1 1 ¯1 ¯1 1 1 1
⍝ this gives us slopes between elements
2+/2-/0,0,⍨h ⍝ sum pairs: left slope + right slope
¯2 ¯2 ¯2 0 2 1 ¯1 0 2 0 ¯2 0 2 2
×2+/2-/0,0,⍨h ⍝ signum of that
¯1 ¯1 ¯1 0 1 1 ¯1 0 1 0 ¯1 0 1 1
2+×2+/2-/0,0,⍨h ⍝ add 2 to make them suitable for indexing
1 1 1 2 3 3 1 2 3 2 1 2 3 3
⍝ Almost ready.  If at this point we replace
⍝ 1:/ 2:v 3:\, only the peaks will require fixing.
~×a ⍝ not signum of a
1 1 1 0 1 1 1 0 1 1 1 0 1 1
(~×a)×2+×2+/2-/0,0,⍨h ⍝ replace peaks with 0-s
1 1 1 0 3 3 1 0 3 2 1 0 3 3
⍝ Now replace 0:^ 1:/ 2:v 3:\
⍝ We can do this by indexing a string with the vector above
⍝ (and adding 1 because of stupid 1-based indexing)
'^/v\'[1+(~×a)×2+×2+/2-/0,0,⍨h]
///^\\/^\v/^\\
⍝ Looks like our mountain, only needs to be raised according to h
r←'^/v\'[1+(~×a)×2+×2+/2-/0,0,⍨h] ⍝ name it for convenience
h¨↑r ⍝ extend r[i] with spaces to make it h[i] long
/  /   /    ^     \    \   /   ^    \   v  /   ^    \   \
↑⌽¨h¨↑r ⍝ reverse each and mix into a single matrix
/
/
/
^
\
\
/
^
\
v
/
^
\
\
⍉⌽↑⌽¨h¨↑r ⍝ reverse and transpose to the correct orientation
^
/ \  ^   ^
/   \/ \ / \
/        v   \
``````

3

# Ruby，390个字符

``````m=[gets.chomp]
a=m[0].scan(/\d/).max.to_i
m[0].gsub!(/./){|n|n==?. ? ' ':a-n.to_i}
s=nil
until a==0
o=''
m[-1].chars{|c|o+=case c
when ?0;?^
when ' ';t=s;s=nil;t ? '':' '
when /\d/;(c.to_i-1).to_s
when ?^;s=1;o.slice! -1;"/ \\"
when ?/;t=s;s=nil;t ? "#{o.slice! -1;' '}":o.slice!(-1)=='\\' ? 'v ':"/ "
when ?\\;s=1;' \\'
when ?v;' '
end}
m.push o
a-=1
end
puts (m[1..-1]*"\n").gsub /\d/,' '
``````

``````m | The mountain array.
a | The highest height of a mountain. Used for counting when to stop.
s | Whether or not to skip the next character. 1 for yes, nil for no.
o | Temp string that will be appended to mountain.
t | Temp variable to hold the old value of s.
``````

2014年

2

# Java中，377 407

``````class M{public static void main(String[]m){char[]n=m[0].toCharArray();int e=n.length,h=9,x=-1,p;char[][]o=new char[11][e];char l,r,u;boolean a,b,c;for(;h>=0;h--){for(p=0;p<e;p++){if(n[p]-49==h){o[h][p]=94;if(x==-1)x=h;}else{l=(p>0)?o[h+1][p-1]:0;r=(p<e-1)?o[h+1][p+1]:0;u=o[h+1][p];a=l>91&&l<99;b=r==94||r==47;c=u<33;o[h][p]=(char)((a&&b)?'v':(c&&b)?47:(c&&a)?92:32);}}if(x>=h)System.out.println(o[h]);}}}
``````

``````class Magrathea2 {
public static void main(String[] mountain) {
String out = "";
char[][] output = new char[11][mountain[0].length()];
int height = 9; int maxheight = -1;
int position = 0;
char left,right,up;
char[] mount = mountain[0].toCharArray();
for (; height >= 0; height--) {
for (position=0; position < mount.length; position++) {
if (mount[position]-49 == height) {
output[height][position] = '^';
if (maxheight==-1) {
maxheight=height;
}
} else { // deal with non-numbers as '.'
left=(position>0)?output[height+1][position-1]:0;
right=(position<mount.length-1)?output[height+1][position+1]:0;
up=output[height+1][position];
if ((left=='^'||left=='\\')&&(right=='^'||right=='/')) {
output[height][position]='v';
} else if ((up==' '||up==0)&&(right=='/'||right=='^')) {
output[height][position]='/';
} else if ((up==' '||up==0)&&(left=='\\'||left=='^')) {
output[height][position]='\\';
} else {
output[height][position]=' ';
}
}
}
if (maxheight >= height) {
out+=new String(output[height]);
if (height > 0) {
out+="\n";
}
}
}
System.out.println(out);
}
}
``````

``````\$ java M ..3..4...6...5....1
^
/ \  ^
^ /   \/ \
^ / v        \
/ v            \
/                \^
``````

Victor Stafusa 2014年

2

## Perl 6中，264 224 216 206 200个194 124字节

``````\$_=get;my\$a=10;((s:g/\$a/^/;s:g/\s\.\s/ v /;s:g'\.\s'/ ';s:g/\s\./ \\/;\$!=say TR/.1..9/ /;tr'^\\/v' ')if .match(--\$a)|\$!)xx 9
``````

``````my\$t=get;for 9...1 {if \$t.match(\$_)|\$! {\$t=\$t.subst(\$_,'^',:g).subst(' . ',' v ',:g).subst('. ','/ ',:g).subst(' .',' \\',:g);\$!=say \$t.subst(/<[\.\d]>/,' ',:g);\$t.=subst(/<[^\\/v]>/,' ',:g)};}
``````

``````my \$t=slurp;
my \$s;
for 9...1 {
if \$t.match(\$_)||\$s {                    # match number or latched
\$t=\$t.subst(\$_,'^',:g)               # peaks
.subst(' . ',' v ',:g)               # troughs
.subst('. ','/ ',:g)                 # up slope
.subst(' .',' \\',:g);               # down slope
\$s=say \$t.subst(/<[\.\d]>/,' ',:g);  # clean, display, latch
\$t=\$t.subst(/<[^\\/v]>/,' ',:g)      # wipe for next line
}
}
``````

``````...4...3...33..4..4....2.3.22.33.5..22...333.222.3..
^
^           ^  ^             / \
/ \  ^   ^^ / \/ \     ^    ^^   \     ^^^     ^
/   \/ \ /  v      \  ^/ \^^/      ^^  /   \^^^/ \
/        v           \/               \/           \
``````

1

donaldh

donaldh

1
131个字节，使用`s///``tr///`。我认为最后一个可以`tr`代替，`s`但我不太想翻译反斜杠。也许第一个太

donaldh

1

## Perl中，254 218 212

``````\$s=<>;sub f{9-\$i-\$_[0]?\$":pop}for\$i(0..8){\$h=1;\$_=\$s;s!(\.*)(\d?)!\$D=(\$w=length\$1)+\$h-(\$2||1);join'',(map{(\$x=\$_-int\$D/2)<0?f--\$h,'\\':\$x?f++\$h,'/':\$D%2?f--\$h,v:f\$h,'/'}0..\$w-1),\$2?f\$h=\$2,'^':''!ge;print if/\S/}
``````
``````\$s=<>;
sub f{9-\$i-\$_[0]?\$":pop}
for\$i(0..8){
\$h=1;
\$_=\$s;
s!(\.*)(\d?)!
\$D=(\$w=length\$1)+\$h-(\$2||1);
join'',(map{
(\$x=\$_-int\$D/2)<0
?f--\$h,'\\'
:\$x
?f++\$h,'/'
:\$D%2
?f--\$h,v
:f\$h,'/'
}0..\$w-1),\$2
?f\$h=\$2,'^'
:''
!ge;
print if/\S/
}``````

1

# C＃ - 321 319

``````using System.Linq;class P{static void Main(string[]p){int h=p[0].Max()-48,i=h,j,n=p[0].Length;char[]A=new char[n+2],B=A;for(;i-->0;){for(j=0;j++<n;){var r=(A[j+1]==47|A[j+1]==94);B[j]=(char)(p[0][j-1]==i+49?94:i+1<h?A[j]==0?(A[j-1]>90&A[j-1]<95)?r?118:92:r?47:0:0:0);}A=(char[])B.Clone();System.Console.WriteLine(B);}}}
``````

``````using System.Linq;

class P
{
static void Main(string[] p)
{
int h = p[0].Max() - 48,    // Getting the height. Codes for 0 to 9 are 48 to 57, so subtract 48 and hope no one will input anything but dots and numbers.
i = h,
j,                      // Declaring some iterators here, saves a few chars in loops.
n = p[0].Length;
char[] A = new char[n+2],   // Creating an array of char with 2 extra members so as not to check for "index out of bounds" exceptions
B = A;               // B is referencing the same array as A at this point. A is previous row, B is the next one.
for (;i-->0;)               // Looping from top to the bottom of the mountain
{
for (j = 0; j++ < n;)   // Looping from left to right.
{
var r = (A[j + 1] == 47 | A[j + 1] == 94);  // This bool is used twice, so it saves a few characters to make it a variable

// Here's the logic
B[j] = (char)(p[0][j - 1] == i + 49 ? 94    // If at this position in the string we have a number, output "^"
: i + 1 < h ?    // And if not, check if we're on the top of the mountain
A[j] == 0 ?    // If we're not at the top, check if the symbol above is a space (0, actually)
(A[j - 1] > 90 & A[j - 1] < 95) ?   // If there's nothing above, we check to see what's to the left ( ^ or \ )
r ?            // And then what's to the right ( ^ or / )
118            // If there are appropriate symbols in both locations, print "v"
: 92             // If there's only a symbol to the left, print "\"
: r              // Otherwise check if there's a symbol to the right, but not to the left
? 47             // And if there is, print "/"
: 0 : 0 : 0);    // Print nothing if there aren't any symbols above, to the left and to the right,
// or there's a "^" right above, or we're at the top of the mountain
}
A=(char[])B.Clone();    // Clone arrays to iterate over the next line
System.Console.WriteLine(B);
}
}
}
``````

``````C:\>program .2..3..4...
^
^ / \
^ / v   \
/ v       \
``````

1

# CJam，128个117 112 106 104字节

CJam比这个挑战还年轻，因此这个答案无法与之竞争。不过，这是一个非常好的挑战！从我对J和APL的了解不多，我认为在这些方面的投稿会令人印象深刻。

``````WlW++"."Waer{_{~U(e>:U}%\W%}2*;W%]z{\$W=}%_\$W=S*\:L,2-,\f{\_)L=(~"^/ ^^/ \v ^ \\"S/2/@L>3<_\$0=f-{=}/t}zN*
``````

``````...4...3...33..4..4....2.3.22.33.5..22...333.222.3..
``````

``````                                 ^
^           ^  ^             / \
/ \  ^   ^^ / \/ \     ^    ^/   \     ^^^     ^
/   \/ \ /  v      \  ^/ \^^/      \^  /   \^^^/ \
/        v           \/               \/           \
``````

1

## 蟒蛇， 297234 218

-63字节，感谢Jo King
-16字节，`r=s.replace`而不是lambda

``````s=input()
r=s.replace
q=0
j=''.join
for i in range(9):
if`9-i`in s or q:q=s=r(`9-i`,'^');s=r(' . ',' v ');s=r('. ','/ ');s=r(' .',' \\');print j([x,' '][x in'0123456789.']for x in s);s=j([x,' '][x in'/\^v']for x in s)``````

``````s=input() # Take input
r=lambda y,z: s.replace(y,z) # Function for quick s.replace(a, b)
j=lambda x: ''.join(x)
q=0 # Acts like boolean
for i in range(9): # Count to 9
if `9-i`in s or q: # When digit has been found or found previously (no newlines at start)
q=s=r(`9-i`,'^') # Digit to ^, set q to non-zero value for always executing from now on
s=r(' . ',' v ') # ' . ' to ' v '
s=r('. ','/ ') # '. ' to '/ '
s=r(' .',' k') # ' .' to 'k'. K is a placeholder, since \\ takes two chars and `[...]`[2::5] fails
print j([x,' '][x in'0123456789.']for x in s) # Print without '0123456789.'
s=j([x,' '][x in'/\^v']for x in s) # Wipe (delete '/^\v`)``````

1

0

# Powershell，148145字节

``````param(\$s)9..1|?{(\$p+=\$s-match\$_)}|%{"\$_,^; \. , v ;\. ,/ ; \., \;\^|\\|/|v, "-split';'|%{\$x=\$s-replace'\.|\d',' '
\$s=\$s-replace(\$_-split',')}
\$x}``````

``````\$f = {

param(\$s)
9..1|?{(\$p+=\$s-match\$_)}|%{      # loop digits form 9 downto 1, execute to the end as soon as a suitable digit met
\$s=\$s-replace\$_,'^'          # replace current digit with '^'
\$s=\$s-replace' \. ',' v '    # replace ' . '  with ' v '
\$s=\$s-replace'\. ','/ '      # replace '. ' with '/ '
\$s=\$s-replace' \.',' \'      # replace ' .' with ' \'
\$s-replace'\.|\d',' '     # replace all dots and digits with ' ' and push to output. Don't store this replacement
\$s=\$s-replace'\^|\\|/|v',' ' # prepeare to the next step: replace ^ \ / and v to space
}

# Example:
#     \$s="...4...3...3.."
# 4 : \$s="...^...3...3.." output: "   ^          "
# 4 : \$s="... ...3...3.."
# 3 : \$s="../ \..^...^.." output: "  / \  ^   ^  "
# 3 : \$s="..   .. ... .."
# 2 : \$s="./   \/ \./ \." output: " /   \/ \ / \ "
# 2 : \$s=".        .   ."
# 1 : \$s="/        v   \" output: "/        v   \"
# 1 : \$s="              "

}

@(
,("1",
"^")

,("11",
"^^")

,("1.2.",
"  ^ ",
"^/ \")

,(".2.3..",
"   ^  ",
" ^/ \ ",
"/    \")

,(".2..3..",
"    ^  ",
" ^ / \ ",
"/ v   \")

,("...4...3...3..",
"   ^          ",
"  / \  ^   ^  ",
" /   \/ \ / \ ",
"/        v   \")

,("...4...3...33..4..4....2.3.22.3..5...22...333.222.3..",
"                                 ^                   ",
"   ^           ^  ^             / \                  ",
"  / \  ^   ^^ / \/ \     ^    ^/   \      ^^^     ^  ",
" /   \/ \ /  v      \  ^/ \^^/      \^^  /   \^^^/ \ ",
"/        v           \/                \/           \")

,(".2..3..6.....5...3......1..3..4....2.",
"       ^                             ",
"      / \    ^                       ",
"     /   \  / \               ^      ",
"    ^     \/   \ ^         ^ / \     ",
" ^ /            v \       / v   \  ^ ",
"/ v                \    ^/       \/ \")
) | % {
\$s,\$expected = \$_
\$result = &\$f \$s
"\$result"-eq"\$expected"
\$s
\$result
}``````

``````True
1
^
True
11
^^
True
1.2.
^
^/ \
True
.2.3..
^
^/ \
/    \
True
.2..3..
^
^ / \
/ v   \
True
...4...3...3..
^
/ \  ^   ^
/   \/ \ / \
/        v   \
True
...4...3...33..4..4....2.3.22.3..5...22...333.222.3..
^
^           ^  ^             / \
/ \  ^   ^^ / \/ \     ^    ^/   \      ^^^     ^
/   \/ \ /  v      \  ^/ \^^/      \^^  /   \^^^/ \
/        v           \/                \/           \
True
.2..3..6.....5...3......1..3..4....2.
^
/ \    ^
/   \  / \               ^
^     \/   \ ^         ^ / \
^ /            v \       / v   \  ^
/ v                \    ^/       \/ \``````

0

# 点`-l`，100字节

``````Y#aZGMXaFi,#aIh:+a@i{(yi--h):4j:0Wh-j&++(yi-++jh-j)(yi+jh-j):2}RV Z(J*y)R`.(?=.*[^0])`0R,6;^" /\v^^"
``````

（该语言比问题要新，但无论如何它可能不会击败APL提交的内容。尽管我希望它会更短一些。）