How many lights are on?

39

6

You are little speck on a Cartesian plane. Pondering your existence, you decide to roam around your plane and touch things. You notice that every coordinate pair you visit has a light switch. To entertain yourself, you decide to toggle every switch that you come across.

The Challenge: Create a program that will take an input of random length, using only the characters v,^,<, and >. These characters correspond to movements.

  • < moves you one unit to the left
  • ^ moves you one unit up
  • > moves you one unit to the right
  • v moves you one unit down

Using this randomly generated input, calculate how many switches will be left in the ON position, assuming you start at (0,0), all lights begin in the OFF state, and you don't turn on any lights until you make your first move. (Meaning, if you start at (0,0), and move up 1 unit to (0,1), there will now be 1 light on). If you come across a switch that is on the ON position, it will be turned OFF.

Test cases:

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

yields 125 lights on.

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

yields 1408 lights on.

To generate input, you may use the following Python script:

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

Standard scoring, winner is the smallest number of bytes.

CasperTheFriendlyCode

Posted 2016-08-15T20:31:45.300

Reputation: 557

16 start="4">

  • 4 lights are on.
  • < – Beta Decay – 2016-08-15T20:36:07.953

    4No, there are five. – CasperTheFriendlyCode – 2016-08-15T20:39:06.573

    So... if you come across a light that is already on? Does it remain on? Edit: what i mean is, can this challenge be summarized as "the number of unique coordinates reached by the given path, starting at 0,0?" – Socratic Phoenix – 2016-08-15T20:54:06.053

    Sorry, I should have been more clear. You toggle each switch you come across. If you come across one that is on, you turn it off. I can post a couple test cases, if you like. – CasperTheFriendlyCode – 2016-08-15T20:58:55.700

    9Oh, people, don't be so harsh on a newcomer. – Leaky Nun – 2016-08-15T21:33:18.643

    7The challenge looks pretty clear (and interesting) to me. Voting to reopen – Luis Mendo – 2016-08-15T21:51:32.260

    Thank you for the reopen. If nobody else answers within the next 10 minutes, I'll post my rather clunky Python solution. – CasperTheFriendlyCode – 2016-08-15T22:07:12.553

    3Welcome to Programming Puzzles and Code Golf! :) – Adnan – 2016-08-15T22:07:56.507

    5I don't see any harshness, just advice on what needs improving, and now the challenge is ready. Nice first challenge! – trichoplax – 2016-08-15T22:08:37.963

    1Does your script mean that the input length limit is 10000? – Leaky Nun – 2016-08-15T23:12:28.053

    I edited the generator script to make it more idiomatic, feel free to roll back the edit if you want to. – Loovjo – 2016-08-16T11:46:30.700

    I don't understand what the output should do. If I input ">>vvv<<", What output will do? – Chad – 2016-08-16T11:56:21.470

    1@Chad : the number of switches in the ON position, at the end. So with your input, the output will be "7". – Michaël Polla – 2016-08-16T14:44:54.367

    Can you confirm if functions are allowed or if our submissions must be complete programs? – None – 2016-08-18T15:49:51.050

    Answers

    28

    MATL, 19 18 17 13 bytes

    5 bytes off thanks to @LeakyNun's idea (see his answer) of using the imaginary unit as a base for exponentiation.

    Jj11\^Ys8#uos
    

    Try it online! Test cases: 1, 2.

    Explanation

    The code traces the path using unit steps in the complex plane. Then it counts how many times each position was visited, and outputs how many positions were visited an odd number of times.

    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
    

    Luis Mendo

    Posted 2016-08-15T20:31:45.300

    Reputation: 87 464

    This looks curious – Leaky Nun – 2016-08-15T23:38:17.560

    If you mean the spacing, yes, it's ugly. On Matlab it does look better: 0+1i 0-1i -1+0i 1+0i – Luis Mendo – 2016-08-15T23:41:24.880

    No, that was golfing you 3 bytes – Leaky Nun – 2016-08-15T23:45:12.153

    @LeakyNun Yes, I just realized :-) 13 bytes now! – Luis Mendo – 2016-08-15T23:49:25.910

    Why use i instead of j? – Leaky Nun – 2016-08-15T23:54:26.673

    @LeakyNun Actually either can be used here. j avoids the enclosing quotes, because it considers the input as an unevaluated string – Luis Mendo – 2016-08-16T00:12:58.360

    But you did change from j to i – Leaky Nun – 2016-08-16T00:16:00.897

    @LeakyNun Because I got confused. When you have something like 11j, the parser interprets that as the number 11j. So you'd need 11 j, or 11i to avoid that. But in this case it's j11, which works fine. So I went back to j – Luis Mendo – 2016-08-16T00:17:24.480

    19

    Python, 68 bytes

    25 bytes thanks to Sp3000.

    2 bytes thanks to Luis Mendo's idea of taking modulus with 11.

    17 bytes thanks to xnor.

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

    Ideone it!

    Leaky Nun

    Posted 2016-08-15T20:31:45.300

    Reputation: 45 011

    4It's shorter to use a set doing d^={p} and len(d). – xnor – 2016-08-16T01:38:28.373

    8

    Java 8, 169 130 127 bytes

    Thanks to Leaky Nun for saving 29 32 bytes.

    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;}
    

    Ungolfed (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;
    }
    

    user55852

    Posted 2016-08-15T20:31:45.300

    Reputation:

    for(char c:s){p+=c<61?-1:c<63?1:c<95?-l:l;m[p]^=2;r+=m[p]-1;} can be for(int c:s)r+=(m[p+=c<61?-1:c<63?1:c<95?-l:l]^=2)-1; to save 8 bytes: Try it online. – Kevin Cruijssen – 2019-06-14T08:42:58.507

    7

    Jelly, 13 bytes

    A port of Luis Mendo's answer in MATL

    O%11*@ı+\œ^/L
    

    Try it online!

    O%11*@ı+\œ^/L
    O             convert to codepoint
     %11          modulo 11
        *@ı       power of i (imaginary unit)
           +\     cumulative sum
             œ^/  reduce by multiset symmetric difference
                L length
    

    Credits of the œ^/ trick to Dennis

    Leaky Nun

    Posted 2016-08-15T20:31:45.300

    Reputation: 45 011

    7

    Perl, 49 bytes

    Includes +1 for -p Run with the control string on STDIN

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

    lights.pl:

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

    If input is restricted to 10000 bytes this reduces to 44+1:

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

    Ton Hospel

    Posted 2016-08-15T20:31:45.300

    Reputation: 14 114

    6

    TSQL, 238 235 203 191 bytes

    Creating a table in memory using recursive SQL. Selecting and calculating from that table(one line code).

    Golfed:

    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)
    

    Ungolfed:

    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)
    

    Fiddle

    t-clausen.dk

    Posted 2016-08-15T20:31:45.300

    Reputation: 2 874

    4

    Mathematica, 64 bytes

    A port of Luis Mendo's MATL answer.

    Count[{_,_?OddQ}]@Tally@Accumulate[I^Mod[ToCharacterCode@#,11]]&
    

    alephalpha

    Posted 2016-08-15T20:31:45.300

    Reputation: 23 988

    162 bytes by using infix notation – Roman – 2019-06-15T09:16:37.410

    3

    SQF, 160 bytes

    Using the function-as-a-file format:

    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
    

    Call as: "STRING" call NAME_OF_COMPILED_FUNCTION

    Ungolfed:

    //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
    

    Οurous

    Posted 2016-08-15T20:31:45.300

    Reputation: 7 916

    easily followed, but how is the performance? – zinking – 2016-08-16T09:35:14.463

    @zinking That depends on how you execute it. Really fast with a static compile at engine load time, much slower with compile "CODE". – Οurous – 2016-08-16T21:35:35.223

    3

    Python: 207 189 181 Bytes

    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)
    

    Input is a string.

    Thanks to @LuisMendo for pointing out that I can actually only indent by 1 space. Darn my Python teachers for telling me to always do it increments of four.

    Fixing it so that input was obtained rather than assumed stored actually saved me 8 bytes.

    CasperTheFriendlyCode

    Posted 2016-08-15T20:31:45.300

    Reputation: 557

    I don't know Python, but I think 1-space indenting is enough, and it will save you a few bytes – Luis Mendo – 2016-08-15T22:34:51.287

    All these years, I've been making sure to always indent at increments of 4. – CasperTheFriendlyCode – 2016-08-15T22:37:02.173

    4But not in code golf :-) – Luis Mendo – 2016-08-15T22:37:18.280

    5You can't assume that the input is pre-stored. Just use input() and store it somewhere – Leaky Nun – 2016-08-15T23:02:30.570

    1

    If you ever need more than one level of nested indentation, there's a great tip about mixing spaces and tabs

    – trichoplax – 2016-08-15T23:10:46.700

    @LeakyNun Fixed the code so that input was not assumed. – CasperTheFriendlyCode – 2016-08-16T15:05:42.700

    @CasperTheFriendlyCode You can use tabs instead of spaces, too. A tab is only one character. – mbomb007 – 2016-08-16T21:11:29.140

    As I only have one level of nesting, tabs would not save any bytes here. – CasperTheFriendlyCode – 2016-08-16T21:15:52.200

    Which Python? - on 2.7 you'd need to add x=y=0 before the loop. Also shouldn't you print len(l) for the "how many" aspect? On the other hand you can do for b in a: and then if b=='v' etc. That gets you to 165 – Chris H – 2016-08-17T15:48:54.843

    2Um... Maybe removing the whitespace around the first = and l+=[[x,y]] is also shorter then l.append([x,y]). – Mega Man – 2016-08-18T18:22:04.427

    3

    Ruby, 64 + 6 = 70 bytes

    +6 bytes for -rset flag.

    A straight port of Leaky Nun's Python answer.

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

    See it on repl.it: https://repl.it/Cnjy

    Jordan

    Posted 2016-08-15T20:31:45.300

    Reputation: 5 001

    I believe the -rset flag only counts as 4 bytes. – Cyoce – 2016-08-18T20:17:09.597

    @ValueInk Oof. I don’t like that at all. Thanks for the heads-up, though. – Jordan – 2019-06-15T00:48:50.637

    1I think they made some pretty good points as to why they want to do this (like how -e being "free" in Ruby means -n is 1 byte instead of 3 but people who don't know about that could report different bytecounts) but to each their own. BTW, Set[] saves 2 bytes over Set.new. – Value Ink – 2019-06-15T02:27:43.213

    1Another thought I had while thinking up a non-rset answer: bytes.map saves 4 bytes over each_char because you can take out the ord call. – Value Ink – 2019-06-18T22:25:34.927

    2

    PowerShell, 136 bytes

    $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
    

    Try it online!

    I think there's more to optimize here, but this is the lowest I got for now.

    Easier to read:

    $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
    

    Explanation: The input is changed to a character array and each piece is matched to one of the first 4 switch cases to increment the x or y coord. Everything matches the 5th case where we decide if a light is turned on or off. Length at the end determines total number of switches turned on.

    Note: The comma in the coordinates is necessary so we don't get collisions like (10,10) and (101,0)

    Sinusoid

    Posted 2016-08-15T20:31:45.300

    Reputation: 451

    Strip some apostrophes and pull $x,$y into a variable for 127bytes

    – Veskah – 2019-06-14T03:40:53.357

    -ne works with array 115 bytes – mazzy – 2019-06-14T06:37:15.190

    Length -> Count for 114 – Veskah – 2019-06-26T18:42:10.420

    2

    PowerShell, 96 95 bytes

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

    Try it online!

    Unrolled:

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

    PowerShell, 112 109 bytes

    A port of Luis Mendo's MATL answer.

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

    Try it online!

    mazzy

    Posted 2016-08-15T20:31:45.300

    Reputation: 4 832

    2

    Javascript (ES6), 97 93 85 bytes

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

    EDIT: -8 bytes, thanks to Neil

    Test:

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

    Arnauld

    Posted 2016-08-15T20:31:45.300

    Reputation: 111 334

    1Use [...i] instead of i.split(''). Also, I think you can write (p=1E6,v=[-1,1,-1E3,1E3],0) as 0,p=1E6,v=[-1,1,-1E3,1E3]. – Neil – 2016-08-16T18:35:35.777

    1If you can arrange for the ^ to be first, search saves a byte over indexOf. (You need the ^ to be first because search treats ^ as a regexp metacharacter.) – Neil – 2016-08-16T18:38:29.383

    1You don't have to count the S= part. – user2428118 – 2016-08-17T12:50:56.453

    1@Arnauld No, the argument name(s) itself is/are part of the function and should remain. However, assigning the function to a variable ("S=") is optional (as you can just call the function like («function code here»)(«argument»)). – user2428118 – 2016-08-17T13:56:34.697

    2

    R, 172 156 137 101 96 bytes

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

    Using @LeakyNun's complex plane idea.

    ungolfed

    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
    }
    

    user5957401

    Posted 2016-08-15T20:31:45.300

    Reputation: 699

    2

    Actually, 24 23 bytes

    This is a port of Luis Mendo's MATL answer. Golfing suggestions welcome. Try it online!

    Edit: One byte thanks to Leaky Nun.

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

    Ungolfing:

    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.
    

    Sherlock9

    Posted 2016-08-15T20:31:45.300

    Reputation: 11 664

    Other than writing :11 as 4P, I am unable to come up with any golf advice – Leaky Nun – 2016-08-30T04:12:18.907

    1

    K (oK), 37 29 27 bytes

    Solution:

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

    Try it online!

    Explanation:

    Create list of steps, create path, group, count each repeat, if mod 2 is 0 then off, else on. Felt very 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
    

    Notes:

    • -8 bytes by stealing David Horák's method of squashing x-y into x (I know it has limitations, but it works for the examples)
    • -2 bytes thanks to ngn

    streetster

    Posted 2016-08-15T20:31:45.300

    Reputation: 3 635

    11 -1 -99 99"^v<>"? -> -99 0 99 -1 1@5! – ngn – 2019-06-20T17:43:28.890

    +/2!.#:' -> +/~=/'^ – ngn – 2019-06-20T17:57:51.567

    1First comment works for -2, second one throws an error... what are you trying to do with the ^? – streetster – 2019-06-21T18:08:55.773

    my mistake. i wanted to use it as "is null?" on the dict's values (to make them all 0s) but apparently oK doesn't work that way :( – ngn – 2019-06-21T18:34:54.033

    Yeah, sadly it uses ^ as 'except' rather than null :( – streetster – 2019-06-21T20:37:45.160

    except is dyadic, null is monadic. oK does have null but it's buggy and doesn't penetrate in dictionary values

    – ngn – 2019-06-21T21:25:14.763

    1

    Perl 6, 47 bytes

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

    Try it online!

    • (-i, *i, ... *) is the infinite repeating sequence -i, 1, i, -1, ...
    • [.ords X% 11] slices into that sequence with the ordinal values of the characters in the input string, modulo 11.
    • [\+] performs a triangular reduction (or "scan") on those values, producing a list of the coordinates visited on the complex plane.
    • bag() creates a Bag containing those coordinates, each of which has an associated multiplicity (the number of times it appeared in the list).
    • {*} fetches all of the multiplicities from the bag.
    • X% 2 crosses those multiplicities with the number 2 using the modulus operator %. Odd multiplicities result in a 1, even multiplicities result in a 0.
    • sum sums those remainders.

    Sean

    Posted 2016-08-15T20:31:45.300

    Reputation: 4 136

    1

    Common Lisp, 198 bytes 186 bytes

    Solution:

    (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)))
    

    Run it!

    Explanation:

    (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 is a hashtable map key, generated as an index of the character within the string "^>v<", remapped to values 1000, -1000, 1 -1 representing the x,y. (x axis is +-1, y axis is +-1000)
    • the hash value at that index is incremented by 1 (switch counter)
    • the hashtable is than looped trough, and the map function increment the final count as modulo 2 of the value on a given key

    Note: i (input) is defined it test as:

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

    David Horák

    Posted 2016-08-15T20:31:45.300

    Reputation: 121

    Does this mean that going up 1 is the equivalent of going right 1000 times - and you could clash? I like the idea of compressing x/y into a single dimension though so I'm going to nab it :) PS - you can use (99 1 -99 -1) to shave 2 bytes.. – streetster – 2019-06-18T21:29:54.730

    @streetster correct, that's the idea. To compress the x/y into 1d, where y must have some buffer to not interfere with the x. (and yes it might actually clash - therefore bigger gab, smaller the chance - thus I am using actually 1000 (1e3), so 999 would be the same byte length). – David Horák – 2019-06-19T14:40:19.023

    1

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

    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'
    

    Readable:

    $_ = <>; # -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;
    
    • thanks Dada for -4 and -7

    choroba

    Posted 2016-08-15T20:31:45.300

    Reputation: 161

    2it's the first time I see "readable" and "perl" in the same post I think! – Dada – 2016-08-16T16:23:56.680

    More seriously, great answer. You can still win a few byte if you use for/./g instead of while, which allows you to use ord() instead of ord($&). – Dada – 2016-08-16T16:25:36.780

    A few more things that can win a few bytes on PPCG : use say instead of print (since -M5.010 or -E are free) and use -n flag (cost 1 byte) instead of doing $_=<>. – Dada – 2016-08-16T16:55:21.537

    1

    C, 203 190 189 bytes

    #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) with O(1) memory algorithm.

    It loops over every position, checks if this position occurs earlier, and if not checks if this position occurs an even/odd amount of time afterwards, adding it to the total.

    orlp

    Posted 2016-08-15T20:31:45.300

    Reputation: 37 067

    1

    C# 210 205 139 138 135 128 113 Bytes

    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);
    

    Explanation

    //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);
    
    • Thanks to VisualMelon for 5 bytes switching Count to Sum
    • Thanks again to VisualMelon, this time for 66 bytes by suggesting some fancy math to get the translations.
    • VisualMelon to the rescue again with combining the select and group by for 13 bytes and just using the input string for the x/y separator for another 2

    user19547

    Posted 2016-08-15T20:31:45.300

    Reputation:

    Nice... I like this a lot. Can you not use Distinct() rather than GroupBy(z=>z)? You can also use Sum(g=>g.Count()%2), since it's always 0 or 1, I should think. – VisualMelon – 2016-08-16T22:38:38.163

    I also recon you can (sadly, I rather like it...) replace the func indexing with something like (x+=c%6%3-1)+"."+(y-=c%18/5-1) (just add a number we compute from the char with some stupid modulus and such). I've not tested, I'll let you fix it if it's broken ;) This ought to save a lot of bytes if I've not messed up! (And there is surly a cheaper pair of expressions, but these are just what I could come up with in the last 20minutes) – VisualMelon – 2016-08-16T23:01:47.653

    @VisualMelon Distinct() doesn't work (unless i'm missing something) as it returns one of each string, rather than giving me a count of the occurences. – None – 2016-08-16T23:12:01.277

    @VisualMelon good idea with using the sum, saved 5 bytes. As for your suggestion regarding the funcs, i think i need to understand why that works (it does) before i post it. is the choosing of the numbers based on the underlying values of the chars? – None – 2016-08-16T23:20:01.570

    Yes, don't know what I was thinking about Distinct... I'm far too used to using Group as a lazy man's distinct I suppose... Aye, the stupid expression c%6%3-1 simply maps ASCII values of <>v^ to -1,1,0,0 (change in x, note the zero values) and the other expression produces a similar result for y. There are probably better expressions (I didn't look very hard), a drop of ternary might pay off. This was my first effort, which perhaps makes it more obvious what is going on: (c<61?--x:c<63?++x:x)+"."+(c>95?--y:c>93?++y:y) (but obviously it's quite a bit longer!) – VisualMelon – 2016-08-17T06:24:52.753

    @VisualMelon thanks for the description, it does make sense now, though my math skills are nowhere near good enough to golf those expressions down without just pure brute force. :) – None – 2016-08-17T16:01:25.940

    @VisualMelon Managed to reduce the y expression by a character, it does flip the y directions but it still produces the correct count. – None – 2016-08-17T16:13:50.647

    This is very cool - it was even cool @VisualMelon's awesome improvements, so kudos to both of you. But (and sorry to be a wet blanket), the question asks for a program, and this isn't a complete program. I haven't been around here for a while so forgive me if I've missed some consensus on what's acceptable for C# submissions. I just feel that to be fair to each other we have to have a level playing field (among submissions of the same language of course). Not that I plan to compete with this (I was going to, but after VisuaMelon's input I can't better it). – Igby Largeman – 2016-08-17T23:28:29.763

    @IgbyLargeman While i am still quite new here it has been my experience that functions and even lambdas are generally accepted unless called out specifically. I have left a comment on the question asking the OP to clarify, I will of course modify my answer if they only want complete programs. – None – 2016-08-18T15:53:14.567

    @IgbyLargeman if you've got any suggetions for improvement, do share them! We C#ers need to put on as best a show as possible, and any opportunity we have to beat Java is a good opportunity. Yes, as much I prefer to always post complete programs, functions are generally accepted. Even none-static functions are usually accepted, so I doubt anyone is going to question the ethics of this post, especially when the Java question is just a lambda expression, it's not even named ;) – VisualMelon – 2016-08-18T17:07:04.530

    Just realised we can save 2 bytes by replace +"."+ with +s+ since it's non-numerical; might use a tad more memory though ;) Also, I've not tested this, but can we not simply have the GroupBy on the c=>... expression, and forget the select? s.GroupBy(c=>(x+=..... – VisualMelon – 2016-08-18T17:15:33.327

    @VisualMelon nice catch on both of those. And also for reminding me to remove the static, noticed it last night, missed it while copying it from my test project into np++ for byte counting. – None – 2016-08-18T17:36:42.300

    0

    krrp, 137 bytes

    ^>:\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.
    

    Try it online!


    Explanation

    ^>:\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)
    

    Try it online!

    Unfortunately, krrp only has one rather slow implementation, making the long test cases difficult to verify.
    krrp String conversion.

    Jonathan Frech

    Posted 2016-08-15T20:31:45.300

    Reputation: 6 681

    0

    Ruby, 67 bytes

    Inspired by orlp's C solution. Assembles a list of all visited positions, and then for each unique one (using setwise intersection (d&d) since it saves a byte over d.uniq), count the ones with an odd number of occurrences (technically, map it to the occurances%2, and sum them together, since that does the same thing)

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

    Try it online!

    Value Ink

    Posted 2016-08-15T20:31:45.300

    Reputation: 10 608

    0

    APL (Dyalog Unicode), 21 20 bytesSBCS

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

    Try it online!

    uses ⎕io←1

    input

    '^<v'⍳ find the index of each input char among '^<v', i.e. ^ becomes 1, < 2, v 3, and anything else 4

    0j1* i to the power (the imaginary constant)

    +\ partials sums

    ⊢⌸ matrix in which each row is the list of occurrences (indices) of a unique partial sum; padded with 0s to make it rectangular

    × signum - all indices become 1s, the padding remains 0s

    ≠/ sum mod 2 for each row

    +/ sum

    ngn

    Posted 2016-08-15T20:31:45.300

    Reputation: 11 449