多少灯点亮?


40

您在笛卡尔平面上几乎没有斑点。考虑到自己的存在,您决定在飞机上漫游并触摸物体。您会注意到,您访问的每个坐标对都有一个电灯开关。为了娱乐自己,您决定切换遇到的每个开关。

挑战:创建一个程序,将采取随机长度的输入时,仅使用字符v^<,和>。这些字符对应于动作。

  • < 向左移动一个单位
  • ^ 使您向上移动一个单位
  • > 将您向右移动一个单位
  • v 下移一个单位

使用此随机生成的输入,ON假设从(0,0)开始,所有灯都在该OFF状态下开始,并且在您进行第一个举动之前就不要打开任何灯,计算该位置上还剩下多少个开关。(意思是,如果您从(0,0)开始,并向上移动1个单位到(0,1),则现在将点亮1个灯。)如果您碰到ON位置上的开关,它将被打开OFF

测试用例:

v>v<^^<v<<^<^><<>^^>>><v>vv^v>v^><><<^>><<<vvv^vvv>v>>v><vv^^<<>vv^^>v^<>>^^<^^>^^v<^>^<vvv^v^v><^<<v<<>><<>v>>^><^>^^<>>>>vv>^<<^<<><vvv<v^>>vvv>v^>>><<v^>^^^^v>>^>^v>v<vv^<>^<<v>vv>><^^<^><vv^^v<v<v^^^>v<^vv^v<><vv^^^>v^>v>vv<<^><v<^v><<v^^v>>v<vv<><^^^v<^v><><<^^<>^>><^^^>vv^<>>>>^<^<<<<>>>v<<v<v<vv><<vv<vv<^v^^^>>vv<>v>><<<v^>vv><v<<<v<<>^vvv^<v^v>^^v^v><<v^>>>v<v<v^>>>v><>>>^<><<<<>vv>v><v>v><^v<>v>>>vv<<>^>^>v<^><^<^vv^><^^>v<^^v>v^v<^^^^vv<>v<>><v^^><>v<<<>v^<v^^><>^<>^<>><>^^<>^v><>>><v<^^>>v>^^^<v

产生125盏灯。

>>><^>>>^vv><^^v^<<<>>^<>>^<^>^<<^^v^<v^>>^<<><<>^v<^^^vv>v><^>>^<^<v><^v<^<<^^v><>>^v<^>^v<vvv<<<v^vv>>^vv>^^<>>vv>^<^>vv<>v^<^v<^^<v^^^<<^><><>^v>vvv<^vvv<vv><vv<^^v^^<^^>>><<<>^<>>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^><vv>v^^^<^>>^<<^<^><<<^^<v<<^vv<^<>v<^<<^^<v<vv<<><v<v^<>^<>v>>v<^v>v<>^^vvv<>vv^v^<><v^vv^<^v^v><>^><v^<>>^^^<>>vv^<v>^^v><v<^>^^^^^^><>>vvv<<><><v<^>v<>v^v<<<<>v^>>>>^v>^^<v^>v><v^<^^v<<<<v<<<>^v<^>^v>v^^>v^^vvv>vv<>^>v><v<>^<vv><>>><<^>>><<v>v^^<^<<<<v^<>>>v<<<^v^vv<>^v>v<<<<>^^>><v><>v<v><^^>><>^>^>v>>><v>^vvvv<><><^>>^v^><<>^v<><><^><<<>v^^>v>^>v^<v^vv<>><^vv^^>^^><vv<<>v>v^^>><v^>^<^<>>>vv<>^>v>v^<>v<^<<v>>>^<>v^>v>>vv^^<>>v<v<<^<>v>v^<^^^>v^^>v>v>vv<^<v>v^^><<<v<><>^^<>v>v>^^v>v>v^v>>^<v^v>><>^^>^<>>>^vv^><v^<^>v^>^v><^>^^^vv^^v<>vv<>>^><<^v>^v^>>v>^v^<<^^^vv<<vvv>^vv^v<<<v^^<<><vv<>>^^vv>^^^vv>><><v>v<^v^>>>vv^><>><v<^v<>^><v<^^^^>><^<>v>^v<^vv>v>v<^<>v>v>^<vv>v<^>vvv<v^<vv<vv<>v>^><v^v<>>>>>v>><^v<>v>^v><v^v^vv<>^<vvv^>><v^<vvv^^<^vvv^v^<>><v>v^^v<><>v^^^v<<<^><v<<<>><<vv<<><vvv^v>>v^v<v^>>><<v<>^v><>vv<<v>v^vv>v^v<^<vv<><><^v>^<vv>v^^>>^^^><vv<><^>>>^<v^<<^^>^>vv^><v<vvv>^^>>>^><<vv>vv>^<>>^^><^v><<>^<<<v^>^

产生1408灯。

要生成输入,可以使用以下Python脚本:

import random
length = random.randint(15, 10000)
walk = ''
chars = ['v', '<', '>', '^']
for i in range(length):
    walk += random.choice(chars)
print(walk)

标准得分,获胜者是最小的字节数。


16
4. 4灯点亮。
Beta Decay's

4
不,有五个。
CasperTheFriendlyCode '16

9
哦,人们,对新来者不要那么苛刻。
Leaky Nun

7
对我来说,挑战看起来非常清晰(而且很有趣)。投票重新开放
路易斯·门多

5
我看不出有什么苛刻的地方,只是建议需要改进的地方,现在挑战已经准备就绪。不错的第一个挑战!
trichoplax

Answers:


28

MATL19 18 17 13字节

@LeakyNun的想法(请参见他的答案)使用虚构的单位作为幂的基数,因此减少了5个字节。

Jj11\^Ys8#uos

在线尝试!测试用例:12

说明

该代码使用复杂平面中的单位步长来跟踪路径。然后,它计算访问每个位置的次数,并输出访问奇数次的位置。

J         % Push the imaginary unit, 1j
j         % Input string
11\       % Modulo 11. This gives 7 6 5 8 for > ^ < v
^         % 1j raised to those numbers, element-wise. This gives -1j for >, -1 for ^,
          % 1j for < and 1 for v. So it gives the displacements in a "transposed"
          % complex plane
Ys        % Cumulative sum. This transforms displacements into positions
8#u       % Count of occurrences of each unique position
o         % 1 if odd, 0 if even
s         % Sum. Implicitly display

看起来很好奇
Leaky Nun

如果您指的是间距,是的,这很丑。在Matlab上看起来更好:0+1i 0-1i -1+0i 1+0i
Luis Mendo

不,那是打高尔夫球的3个字节
Leaky Nun

@LeakyNun是的,我刚刚意识到:-)现在有13个字节!
路易斯·门多

为什么用i代替j
Leaky Nun

19

Python,68个字节

25字节归功于Sp3000。

路易斯·门多(Luis Mendo)的模数取11的想法,因此得到2个字节。

感谢xnor,共17个字节。

d=set()
p=0
for c in input():p+=1j**(ord(c)%11);d^={p}
print(len(d))

伊迪恩!


4
使用set做d^={p}和的时间要短一些len(d)
xnor

8

Java的8,169个 130 127字节

感谢Leaky Nun节省了29 32个字节。

s->{int l=s.length,d=2*l+1,p=l*d+d,r=0,m[]=new int[d*d];for(char c:s){p+=c<61?-1:c<63?1:c<95?-l:l;m[p]^=2;r+=m[p]-1;}return r;}

非高尔夫(sorta):

public static int f(char[] s) {
    int l = s.length, d = 2*l+1, p=l*d+d, r = 0;
    int[] m = new int[d*d];
    for(char c : s) {
        p += c<61 ? -1 : c<63 ? 1 : c<95 ? -l : l;
        m[p] ^= 2;
        r += m[p] - 1;
    }
    return r;
}

for(char c:s){p+=c<61?-1:c<63?1:c<95?-l:l;m[p]^=2;r+=m[p]-1;}可以for(int c:s)r+=(m[p+=c<61?-1:c<63?1:c<95?-l:l]^=2)-1;保存8个字节:在线尝试。
凯文·克鲁伊森


7

Perl,49个字节

包含+1,用于-p 通过STDIN上的控制字符串运行

./lights.pl <<< "<<>"

lights.pl

#!/usr/bin/perl -p
$\-=1-($a{$x+=/</-/>/,$y+=/\^/-/v/}^=2)for/./g}{

如果输入限制为10000个字节,则减少为44+1

$\+=$#{$x+=/</-/>/.e5+/\^/-/v/}*=-1for/./g}{

6

TSQL,238个 235 203 191字节

使用递归SQL在内存中创建表。从该表中选择并计算(一个行代码)。

打高尔夫球:

DECLARE @ varchar(max)= 'v>v<';

WITH C as(SELECT 0 a,b=1UNION ALL SELECT
a+POWER(CHARINDEX(SUBSTRING(@,b,1),'> <v')-2,15),b+1FROM
C WHERE b<=LEN(@))SELECT top
1sum(sum(1-1/b)%2)over()FROM c
GROUP BY a OPTION(MAXRECURSION 0)

取消高尔夫:

DECLARE @ varchar(max)= '>>>><^v^v';

WITH C as
(
  SELECT 0 a,b=1
  UNION ALL
  SELECT a+POWER(CHARINDEX(SUBSTRING(@,b,1),'> <v')-2,15),b+1
  FROM C
  WHERE b<=LEN(@)
)
SELECT top 1sum(sum(1-1/b)%2)over()
FROM c
GROUP BY a
OPTION(MAXRECURSION 0)

小提琴



3

SQF,160字节

使用文件功能格式:

x=0;y=0;a=[];{switch(_x){case"v":{y=y-1};case"^":{y=y+1};case"<":{x=x-1};case">":{x=x+1};};if([x,y]in a)then{a=a-[[x,y]]}else{a=a+[[x,y]]}}forEach _this;count a

致电为: "STRING" call NAME_OF_COMPILED_FUNCTION

取消高尔夫:

//position tracker variables
x = 0; y = 0;
//`a` keeps track of which coords are switched on
a = [];
{
    //switch based on the magic variable `_x`
    //which is the current element of the forEach
    //and adjust coord tracker variables
    switch(_x) {
        case "v": {
            y = y - 1
        };
        case "^": {
            y = y + 1
        };
        case "<": {
            x = x - 1
        };
        case ">": {
            x = x + 1
        };
    };
    //check if the coord is already turned on
    if ([x, y] in a) then {
        //remove it from `a` using array exclusion
        a = a - [[x, y]]
    } else {
        //append it to `a`
        a = a + [[x, y]]
    }
//do this for each character in _this (the argument)
} forEach _this;
//return the length of `a`
count a

容易遵循,但是性能如何?
zinking

@zinking取决于您如何执行它。在引擎加载时使用静态编译确实非常快,而使用则要慢得多compile "CODE"
2016年

3

Python的:207 189 181个字节

a = input()
l=[]
for i in range(0,len(a)):
 if a[i]=='v':y-=1
 if a[i]=='^':y+=1
 if a[i]=='<':x-=1
 if a[i]=='>':x+=1
 if [x,y] in l:l.remove([x,y])
 else:l.append([x,y])
print(l)

输入是一个字符串。

感谢@LuisMendo指出我实际上只能缩进1个空格。我的Python老师该死,告诉我始终以4为增量进行操作。

对其进行修复,以便获得输入而不是假定存储,实际上节省了我8个字节。


我不了解Python,但是我认为1位空格缩进就足够了,它将节省您几个字节
Luis Mendo

4
但没有参加代码高尔夫球运动:-)
路易斯·门多

5
您不能假定输入已预先存储。只需使用input()并将其存储在某个地方
Leaky Nun

1
如果您需要多个级别的嵌套缩进,则有一个关于混合空格和制表符的绝妙技巧
trichoplax

2
嗯...也许删除第一个=和l + = [[x,y]]周围的空格也比l.append([x,y])短。
洛克人

3

Ruby,64 + 6 = 70字节

+6个字节用于-rset标志。

Leaky Nun的Python答案的直截了当。

->s{d=Set.new
q=0
s.each_char{|c|d^=[q+=1i**(c.ord%11)]}
d.size}

在repl.it上查看:https ://repl.it/Cnjy


我相信该-rset标志仅计为4个字节。
Cyoce


@ValueInk Oof。我一点都不喜欢。不过请多加注意。
约旦

1
我认为他们对为什么要这样做提出了一些很好的观点(例如,-e在Ruby中“免费”意味着-n1字节而不是3字节,但是不了解的人可能会报告不同的字节数),但是每个人拥有。顺便说一句,Set[]保存2个字节Set.new
价值墨水

1
我在想一个无人rset接听时遇到的另一个想法:bytes.map节省了4个字节,each_char因为您可以ord接听电话。
价值墨水

2

R,172个 156 137 101 96字节

n=function(a)sum(table(cumsum(sapply(strsplit(a,"")[[1]],switch,v=-1,"^"=1,">"=1i,"<"=-1i)))%%2)

使用@LeakyNun的复杂平面思想。

不打高尔夫球

n=function(a){
    b=strsplit(a,"")[[1]]                        #Splits String up
    p=sapply(b,switch,v=-1,"^"=1,">"=1i,"<"=-1i) #Replaces characters with directions
    q=cumsum(p)                                  #Finds each location visitied
    t=table(q)%%2                                #Determines if spots were visited an odd # of times
    sum(t)                                       #counts odd visited spots
}

2

Javascript(ES6),97 93 85字节

i=>[...i].reduce((c,d)=>c-1+(v[p+=v['^<>v'.search(d)]]^=2),0,p=1E6,v=[-1E3,-1,1,1E3])

编辑:-8个字节,感谢尼尔

测试:

var test0 = 'v>v<^^<v<<^<^><<>^^>>><v>vv^v>v^><><<^>><<<vvv^vvv>v>>v><vv^^<<>vv^^>v^<>>^^<^^>^^v<^>^<vvv^v^v><^<<v<<>><<>v>>^><^>^^<>>>>vv>^<<^<<><vvv<v^>>vvv>v^>>><<v^>^^^^v>>^>^v>v<vv^<>^<<v>vv>><^^<^><vv^^v<v<v^^^>v<^vv^v<><vv^^^>v^>v>vv<<^><v<^v><<v^^v>>v<vv<><^^^v<^v><><<^^<>^>><^^^>vv^<>>>>^<^<<<<>>>v<<v<v<vv><<vv<vv<^v^^^>>vv<>v>><<<v^>vv><v<<<v<<>^vvv^<v^v>^^v^v><<v^>>>v<v<v^>>>v><>>>^<><<<<>vv>v><v>v><^v<>v>>>vv<<>^>^>v<^><^<^vv^><^^>v<^^v>v^v<^^^^vv<>v<>><v^^><>v<<<>v^<v^^><>^<>^<>><>^^<>^v><>>><v<^^>>v>^^^<v';
var test1 = '>>><^>>>^vv><^^v^<<<>>^<>>^<^>^<<^^v^<v^>>^<<><<>^v<^^^vv>v><^>>^<^<v><^v<^<<^^v><>>^v<^>^v<vvv<<<v^vv>>^vv>^^<>>vv>^<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v><>^>^><vvvv<>^<<<><<<^<>>v^vv^>><^vv^^>^<v^<v>><^^>>>^v>^v<>^v<><^><vv>v^^^<^>>^<<^<^><<<^^<v<<^vv<^<>v<^<<^^<v<vv<<><v<v^<>^<>v>>v<^v>v<>^^vvv<>vv^v^<><v^vv^<^v^v><>^><v^<>>^^^<>>vv^<v>^^v><v<^>^^^^^^><>>vvv<<><><v<^>v<>v^v<<<<>v^>>>>^v>^^<v^>v><v^<^^v<<<<v<<<>^v<^>^v>v^^>v^^vvv>vv<>^>v><v<>^<vv><>>><<^>>><<v>v^^<^<<<<v^<>>>v<<<^v^vv<>^v>v<<<<>^^>><v><>v<v><^^>><>^>^>v>>><v>^vvvv<><><^>>^v^><<>^v<><><^><<<>v^^>v>^>v^<v^vv<>><^vv^^>^^><vv<<>v>v^^>><v^>^<^<>>>vv<>^>v>v^<>v<^<<v>>>^<>v^>v>>vv^^<>>v<v<<^<>v>v^<^^^>v^^>v>v>vv<^<v>v^^><<<v<><>^^<>v>v>^^v>v>v^v>>^<v^v>><>^^>^<>>>^vv^><v^<^>v^>^v><^>^^^vv^^v<>vv<>>^><<^v>^v^>>v>^v^<<^^^vv<<vvv>^vv^v<<<v^^<<><vv<>>^^vv>^^^vv>><><v>v<^v^>>>vv^><>><v<^v<>^><v<^^^^>><^<>v>^v<^vv>v>v<^<>v>v>^<vv>v<^>vvv<v^<vv<vv<>v>^><v^v<>>>>>v>><^v<>v>^v><v^v^vv<>^<vvv^>><v^<vvv^^<^vvv^v^<>><v>v^^v<><>v^^^v<<<^><v<<<>><<vv<<><vvv^v>>v^v<v^>>><<v<>^v><>vv<<v>v^vv>v^v<^<vv<><><^v>^<vv>v^^>>^^^><vv<><^>>>^<v^<<^^>^>vv^><v<vvv>^^>>>^><<vv>vv>^<>>^^><^v><<>^<<<v^>^';

S=i=>[...i].reduce((c,d)=>c-1+(v[p+=v['^<>v'.search(d)]]^=2),0,p=1E6,v=[-1E3,-1,1,1E3])

console.log(S(test0))
console.log(S(test1))


1
使用[...i]代替i.split('')。另外,我认为您可以编写(p=1E6,v=[-1,1,-1E3,1E3],0)0,p=1E6,v=[-1,1,-1E3,1E3]
尼尔

1
如果您可以安排为^第一个,则search保存一个字节indexOf。(您需要^成为第一个,因为它被search视为正则^表达式元字符。)
Neil

1
您不必计算S=零件。
user2428118 '16

1
@Arnauld否,参数名称本身是函数的一部分,应保留。但是,将函数分配给变量(“ S=”)是可选的(因为您可以像那样调用函数(«function code here»)(«argument»))。
user2428118 '16

2

Powershell,136字节

$a=@();switch -w($args|% t*y){'^'{$y++}'>'{$x++}'v'{$y--}'<'{$x--}*{if("$x,$y"-in$a){$a=$a|?{$_-ne"$x,$y"}}else{$a+="$x,$y"}}};$a.length

在线尝试!

我认为这里还有更多要优化的地方,但这是我目前获得的最低点。

更容易阅读:

$a=@();switch -w($args|% t*y){
    '^'{$y++}
    '>'{$x++}
    'v'{$y--}
    '<'{$x--}
    *{if("$x,$y"-in$a){$a=$a|?{$_-ne"$x,$y"}}else{$a+="$x,$y"}}
};$a.length

说明:输入已更改为字符数组,并且每个部分都与前4个切换条件之一匹配,以递增x或y坐标。一切都与第5种情况相符,我们决定是打开还是关闭灯。末端的长度决定了打开的开关总数。

注意:坐标中的逗号是必需的,因此不会发生像(10,10)和(101,0)这样的碰撞


去除一些撇号并将$ x,$ y放入一个127bytes
Veskah

-ne与阵列作品115字节
MAZZY


2

PowerShell96 95字节

$(switch -w($args|% t*y){^{$y++}'>'{$x++}v{$y--}'<'{$x--}*{"$x,$y"}})|group|% c*|%{$r+=$_%2}
$r

在线尝试!

展开:

$plane=$(switch -w($args|% toCharArray){
    ^  {$y++}
    '>'{$x++}
    v  {$y--}
    '<'{$x--}
    *  {"$x,$y"}
})
$plane|group|% count|{$result+=$_%2}
$result

PowerShell中112个 109字节

路易斯·门多Luis Mendo)的MATL答案港口。

$args|% t*y|%{($p+=($c=[numerics.complex])::Pow($c::new(0,1),$_%11))}|group{'{0:N2}'-f$_}|% c*|?{$r+=$_%2};$r

在线尝试!


2

事实上24个 23字节

这是路易斯·门多Luis Mendo)的MATL答案的港口。欢迎打高尔夫球。在线尝试!

编辑:感谢Leaky Nun一个字节。

O⌠4P@%ïⁿ⌡Mσ;╗╔⌠╜c2@%⌡MΣ

开球:

O        ord(c) of the input string (implicit input)
⌠...⌡M   Start a function, and map over the list of ord(). Call the variable i.
  4P@%     i mod 11 (the 4-th prime).
  ïⁿ       1i to the power of (i%11).

σ        Push a list of cumulative sums of the new list of complex numbers.
           This is a list of the coordinate visited.
;╗       Duplicate coord_list, and push to register 0.
╔        uniquify(coord_list)

⌠...⌡M   Map over the uniquified list. Call the variable j.
  ╜c       Push coord_list.count(j)
  2@%      Push count mod 2

Σ        Return the sum of this last list.

除了创作:114P,我无法想出任何高尔夫球意见
漏尼姑

1

C,203个 190 189字节

#define S(s)1-(*s%11<7)*2<<*s%11%2*16
char*t,*u;P,p,r,L,e;
f(char*s){for(P=L=0,t=s;*t;++t){P+=S(t);e=r=1;for(p=0,u=s;u<t;u++)e*=(p+=S(u))!=P;for(p=P;*++u;r^=p==P)p+=S(u);L+=e*r;}return L;}

O(n ^ 2)与O(1)内存算法。

它循环遍历每个位置,检查此位置是否较早出现,如果不是,则检查此位置之后是否出现偶数/奇数时间,将其加到总计中。


1

Perl,102 98 91 90(89 + -n)字节

perl -nE '$i=ord()%5,$x+=(-1..1)[$i],$y+=((0)x3,1,-1)[$i],$p{$x,$y}^=1 for/./g;say~~grep$_,values%p'

可读性:

$_ = <>; # -n
for (/./g) {
    $i = ord() % 5;
    $x += (-1 .. 1)[$i];
    $y += ((0) x 3, 1, -1)[$i];
    $p{$x, $y} ^= 1;
}

say 0 + grep $_, values %p;
  • 感谢达达-4和-7

2
这是我第一次在同一篇文章中看到“可读”和“ perl”!
达达

更严重的是,很好的答案。如果您使用for/./g代替,您仍然可以赢得一些字节while,这使您可以ord()代替使用ord($&)
达达

可以在PPCG上赢得更多字节的更多操作:使用say代替print(因为-M5.010-E空闲),并使用-n标志(花费1个字节)代替do $_=<>
达达

1

C#210 205 139 138 135 128 113个字节

using System.Linq;int f(string s,int x=0,int y=0)=>s.GroupBy(c=>(x+=c%6%3-1)+s+(y-=c/3%6-2)).Sum(g=>g.Count()%2);

说明

//Have X and Y as default params allows us to use a expression bodied member
//and then remove the return statement
int f(string s, int x = 0, int y = 0) =>

    //treat the string as a char array
    s.GroupBy(c =>

            //Using the unerlying int of the char this math reduces down to either x - 1, x + 1, x + 0, y - 1 or y + 1, or y + 0
            //Concat the resulting values with the original string to ensure uniqueness ie x=1 and y=11 vs x=11 and y=1 for the grouping
            (x += c % 6 % 3 - 1) + s + (y -= c / 3 % 6 - 2))

     //Mod 2 will return either 1 or 0 so we can use sum to count the groups with odd counts
     .Sum(g => g.Count() % 2);
  • 感谢VisualMelon将5个字节切换CountSum
  • 再次感谢VisualMelon,这次通过建议一些奇特的数学方法来获取翻译,从而节省了66个字节。
  • VisualMelon通过将select和group by组合为13个字节,并仅使用输入字符串作为x / y分隔符来另2个字节来再次进行救援

很好...我很喜欢。您能不能使用Distinct()而不是GroupBy(z=>z)?您还可以使用Sum(g=>g.Count()%2),因为它始终为0或1,我应该认为。
VisualMelon

我还可以检查一下(非常喜欢,我...),将func索引替换为类似的东西(x+=c%6%3-1)+"."+(y-=c%18/5-1)(只需添加我们从char计算出的数字和一些愚蠢的模数等)即可。我还没有测试过,如果它坏了,我会让你修复它;)如果我没有弄乱的话,这应该节省很多字节!(而且有一对便宜得多的表达式,但这只是我在过去20分钟内可以想到的表达式)
VisualMelon

@VisualMelon Distinct()不起作用(除非我遗漏了一些东西),因为它返回每个字符串之一,而不是给我计数出现的次数。
JustinM-恢复莫妮卡

@VisualMelon使用总和的好主意,节省了5个字节。至于您对功能的建议,我认为我需要在发布之前理解它的工作原理。数字的选择是基于字符的基础值吗?
JustinM-恢复莫妮卡

是的,不知道我在想鲜明......我太习惯于使用集团作为一个懒惰的人的不同,我想......是呀,笨表达c%6%3-1简单的ASCII值映射的<>v^,以-1,1,0,0中(其他城市x,注零值),另一个表达式对产生类似的结果y。可能有更好的表达方式(我看上去并不难),一元三元的使用可能会有所回报。这是我的第一次努力,这也许使事情变得更加明显:((c<61?--x:c<63?++x:x)+"."+(c>95?--y:c>93?++y:y)但显然
要花

1

Perl 6,47个字节

{sum bag([\+] (-i,*i...*)[.ords X%11]){*}X%2}

在线尝试!

  • (-i, *i, ... *)是无限的重复序列-i1i-1,...
  • [.ords X% 11] 将输入字符串中字符的序数值按模11切成该序列。
  • [\+] 对这些值执行三角缩减(或“扫描”),从而生成在复杂平面上访问的坐标的列表。
  • bag()创建一个Bag包含这些坐标的坐标,每个坐标都有一个相关联的多重性(出现在列表中的次数)。
  • {*} 从袋子中取出所有物品。
  • X% 22使用模运算符将这些多重与数字交叉%。奇数个多重性导致a 1,偶数个多重性导致a 0
  • sum 总结这些余数。

1

普通Lisp198字节 186字节

解:

(let((p 0)(c 0)(h(make-hash-table)))(progn(loop for s across i do(incf(gethash(incf p(nth(position s "^>v<")'(1e3 1 -1e3 -1)))h 0)))(maphash #'(lambda(k v)(incf c(mod v 2)))h)(write c)))

运行!

说明:

(let ((p 0)(c 0)(h (make-hash-table)))
  (progn
    (loop for s across i
      do(incf (gethash (incf p (nth (position s "^>v<")'(1e3 1 -1e3 -1))) h 0)))
    (maphash #'(lambda (k v) (incf c (mod v 2))) h)
    (write c)))
  • p是一个哈希表映射键,作为字符串“ ^> v <”内的字符的索引生成,重新映射为代表x,y的值1000,-1000、1 -1。(x轴为+ -1,y轴为+ -1000)
  • 该索引处的哈希值增加1(切换计数器)
  • 哈希表将比循环槽低,并且map函数将给定键值的最终计数递增为模的2

注意:我(输入)被定义为测试为:

(defparameter i ">>><^>>>^vv><^^v^<<<>>^<>>^<^>^<<^^v^<v^>>^<<><<>^v<^^^vv>v><^>>^<^<v><^v<^<<^^v><>>^v<^>^v<vvv<<<v^vv>>^vv>^^<>>vv>^<^>vv<>v^<^v<^^<v^^^<<^><><>^v>vvv<^vvv<vv><vv<^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><^^>>>^v>^v<>^v<><^><vv>v^^^<^>>^<<^<^><<<^^<v<<^vv<^<>v<^<<^^<v<vv<<><v<v^<>^<>v>>v<^v>v<>^^vvv<>vv^v^<><v^vv^<^v^v><>^><v^<>>^^^<>>vv^<v>^^v><v<^>^^^^^^><>>vvv<<><><v<^>v<>v^v<<<<>v^>>>>^v>^^<v^>v><v^<^^v<<<<v<<<>^v<^>^v>v^^>v^^vvv>vv<>^>v><v<>^<vv><>>><<^>>><<v>v^^<^<<<<v^<>>>v<<<^v^vv<>^v>v<<<<>^^>><v><>v<v><^^>><>^>^>v>>><v>^vvvv<><><^>>^v^><<>^v<><><^><<<>v^^>v>^>v^<v^vv<>><^vv^^>^^><vv<<>v>v^^>><v^>^<^<>>>vv<>^>v>v^<>v<^<<v>>>^<>v^>v>>vv^^<>>v<v<<^<>v>v^<^^^>v^^>v>v>vv<^<v>v^^><<<v<><>^^<>v>v>^^v>v>v^v>>^<v^v>><>^^>^<>>>^vv^><v^<^>v^>^v><^>^^^vv^^v<>vv<>>^><<^v>^v^>>v>^v^<<^^^vv<<vvv>^vv^v<<<v^^<<><vv<>>^^vv>^^^vv>><><v>v<^v^>>>vv^><>><v<^v<>^><v<^^^^>><^<>v>^v<^vv>v>v<^<>v>v>^<vv>v<^>vvv<v^<vv<vv<>v>^><v^v<>>>>>v>><^v<>v>^v><v^v^vv<>^<vvv^>><v^<vvv^^<^vvv^v^<>><v>v^^v<><>v^^^v<<<^><v<<<>><<vv<<><vvv^v>>v^v<v^>>><<v<>^v><>vv<<v>v^vv>v^v<^<vv<><><^v>^<vv>v^^>>^^^><vv<><^>>>^<v^<<^^>^>vv^><v<vvv>^^>>>^><<vv>vv>^<>>^^><^v><<>^<<<v^>^")

这是否意味着上升1等于正确进行1000次-您可能会发生冲突?我喜欢将x / y压缩成一个维度的想法,所以我要抓住它:) PS-您可以(99 1 -99 -1)用来剃2个字节
。–

@streetster正确,这就是想法。要将x / y压缩为1d,其中y必须具有一些缓冲区以不干扰x。(是的,它实际上可能会发生冲突-因此gab越大,机会就越小-因此我实际上使用了1000(1e3),所以999将是相同的字节长度)。
DavidHorák

1

K(oK)37 29 27字节

解:

+/2!.#:'=+\-99 0 99 -1 1@5!

在线尝试!

说明:

创建步骤列表,创建路径,分组,计算每个重复次数,如果mod 2为0,则为off,否则为on。感觉非常AdventOfCode-ish。

+/2!.#:'=+\-99 0 99 -1 1@5! / the solution
                        5!  / input mod 5, "^v<>" yields 4 3 0 2
           -99 0 99 -1 1    / left, null, right, down, up
         +\                 / sum along
        =                   / group into key=>value
     #:'                    / count each value
    .                       / take the value 
  2!                        / modulo 2
+/                          / sum up

笔记:

  • 通过窃取DavidHorák将xy压缩为x的方法来获得-8个字节(我知道它有局限性,但适用于示例)
  • -2个字节,感谢ngn

1
1 -1 -99 99"^v<>"?->-99 0 99 -1 1@5!
ngn

+/2!.#:'->+/~=/'^
ngn

1
第一个注释适用于-2,第二个注释引发错误...您打算如何处理^
6

我的错。我想将其用作“是否为null?” 在字典的值(使它们全为0),但显然不是那样工作:(
ngn

是的,可惜它使用^作为'except'而不是null :(
streetster

0

krrp,137字节

^>:\L\T[length],^v>xy:!tTxy!V?[elem]tv[without]vLtELtv?#?E>V!c#!f>?=c$62.@V#!r>+x1y?=c$60.@V#!r>-x1y?=c$94.@V#!r>x-y1@V#!r>x+y1.LT00E>00.

在线尝试!


说明

^>:\L\T                ~ lambda expression, import list and tuple module
 [length],^v>xy:       ~  the answer is the number of lattice points
                       ~  which were visited an odd number of times
  !tTxy                ~   current lattice point 
  !V ?[elem]tv         ~   if the lattice point is already present,
      [without]vLtE    ~    remove it, else
      Ltv              ~    add it
  ?#?E> V              ~   no further moves
  !c#!f>               ~   move character
  ?=c$62. @V#!r>+x1y   ~   move right
   ?=c$60. @V#!r>-x1y  ~    move left
    ?=c$94. @V#!r>x-y1 ~     move up
     @V#!r>x+y1        ~      move down	
 .LT00E>00.            ~ initialize at point (0, 0)

在线尝试!

不幸的是,krrp的实现速度相当慢,这使得较长的测试用例难以验证。
krrp字符串转换


0

APL(Dyalog Unicode)21个 20字节SBCS

+/≠/×⊢⌸+\0j1*'^<v'⍳⎕

在线尝试!

用途 ⎕io←1

输入

'^<v'⍳发现每一个输入字符的索引中'^<v'的,即^变为1,<2,v3,和其他任何4

0j1* 的力量(虚数常数)

+\ 偏和

⊢⌸矩阵,其中每一行是唯一部分和的出现(索引)列表;用0填充以使其变为矩形

× 符号-所有索引变为1,填充保持为0

≠/ 每行总和为2

+/


0

Ruby,67个字节

受orlp的C解决方案启发。组装所有访问过的位置的列表,然后为每个唯一的位置(使用setwise相交,(d&d)因为它节省了一个字节d.uniq),对出现次数为奇数的位置计数(从技术上讲,将其映射到出现率%2,并将它们加在一起,因为这样做的目的相同)

->s,q=0{d=s.bytes.map{|c|q+=1i**(c%11)}
(d&d).sum{|c|d.count(c)%2}}

在线尝试!

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.