39
\$\begingroup\$

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.

\$\endgroup\$
14
  • 16
    \$\begingroup\$ 4. 4 lights are on. \$\endgroup\$
    – Beta Decay
    Commented Aug 15, 2016 at 20:36
  • 4
    \$\begingroup\$ No, there are five. \$\endgroup\$ Commented Aug 15, 2016 at 20:39
  • 9
    \$\begingroup\$ Oh, people, don't be so harsh on a newcomer. \$\endgroup\$
    – Leaky Nun
    Commented Aug 15, 2016 at 21:33
  • 7
    \$\begingroup\$ The challenge looks pretty clear (and interesting) to me. Voting to reopen \$\endgroup\$
    – Luis Mendo
    Commented Aug 15, 2016 at 21:51
  • 5
    \$\begingroup\$ I don't see any harshness, just advice on what needs improving, and now the challenge is ready. Nice first challenge! \$\endgroup\$ Commented Aug 15, 2016 at 22:08

24 Answers 24

28
\$\begingroup\$

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
\$\endgroup\$
8
  • \$\begingroup\$ This looks curious \$\endgroup\$
    – Leaky Nun
    Commented Aug 15, 2016 at 23:38
  • \$\begingroup\$ If you mean the spacing, yes, it's ugly. On Matlab it does look better: 0+1i 0-1i -1+0i 1+0i \$\endgroup\$
    – Luis Mendo
    Commented Aug 15, 2016 at 23:41
  • \$\begingroup\$ No, that was golfing you 3 bytes \$\endgroup\$
    – Leaky Nun
    Commented Aug 15, 2016 at 23:45
  • \$\begingroup\$ @LeakyNun Yes, I just realized :-) 13 bytes now! \$\endgroup\$
    – Luis Mendo
    Commented Aug 15, 2016 at 23:49
  • \$\begingroup\$ Why use i instead of j? \$\endgroup\$
    – Leaky Nun
    Commented Aug 15, 2016 at 23:54
19
\$\begingroup\$

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!

\$\endgroup\$
1
  • 4
    \$\begingroup\$ It's shorter to use a set doing d^={p} and len(d). \$\endgroup\$
    – xnor
    Commented Aug 16, 2016 at 1:38
8
\$\begingroup\$

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;
}
\$\endgroup\$
1
  • \$\begingroup\$ 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. \$\endgroup\$ Commented Jun 14, 2019 at 8:42
7
\$\begingroup\$

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

\$\endgroup\$
7
\$\begingroup\$

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}{
\$\endgroup\$
6
\$\begingroup\$

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

\$\endgroup\$
4
\$\begingroup\$

Mathematica, 64 bytes

A port of Luis Mendo's MATL answer.

Count[{_,_?OddQ}]@Tally@Accumulate[I^Mod[ToCharacterCode@#,11]]&
\$\endgroup\$
1
3
\$\begingroup\$

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
\$\endgroup\$
2
  • \$\begingroup\$ easily followed, but how is the performance? \$\endgroup\$
    – zinking
    Commented Aug 16, 2016 at 9:35
  • \$\begingroup\$ @zinking That depends on how you execute it. Really fast with a static compile at engine load time, much slower with compile "CODE". \$\endgroup\$
    – Οurous
    Commented Aug 16, 2016 at 21:35
3
\$\begingroup\$

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.

\$\endgroup\$
10
  • \$\begingroup\$ I don't know Python, but I think 1-space indenting is enough, and it will save you a few bytes \$\endgroup\$
    – Luis Mendo
    Commented Aug 15, 2016 at 22:34
  • 4
    \$\begingroup\$ But not in code golf :-) \$\endgroup\$
    – Luis Mendo
    Commented Aug 15, 2016 at 22:37
  • 5
    \$\begingroup\$ You can't assume that the input is pre-stored. Just use input() and store it somewhere \$\endgroup\$
    – Leaky Nun
    Commented Aug 15, 2016 at 23:02
  • 1
    \$\begingroup\$ If you ever need more than one level of nested indentation, there's a great tip about mixing spaces and tabs \$\endgroup\$ Commented Aug 15, 2016 at 23:10
  • 2
    \$\begingroup\$ Um... Maybe removing the whitespace around the first = and l+=[[x,y]] is also shorter then l.append([x,y]). \$\endgroup\$
    – univalence
    Commented Aug 18, 2016 at 18:22
3
\$\begingroup\$

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

\$\endgroup\$
5
  • \$\begingroup\$ I believe the -rset flag only counts as 4 bytes. \$\endgroup\$
    – Cyoce
    Commented Aug 18, 2016 at 20:17
  • 1
    \$\begingroup\$ With the newest meta changes the -rset flag now counts as 0 bytes. \$\endgroup\$
    – Value Ink
    Commented Jun 15, 2019 at 0:44
  • \$\begingroup\$ @ValueInk Oof. I don’t like that at all. Thanks for the heads-up, though. \$\endgroup\$
    – Jordan
    Commented Jun 15, 2019 at 0:48
  • 1
    \$\begingroup\$ I 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. \$\endgroup\$
    – Value Ink
    Commented Jun 15, 2019 at 2:27
  • 1
    \$\begingroup\$ Another 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. \$\endgroup\$
    – Value Ink
    Commented Jun 18, 2019 at 22:25
3
\$\begingroup\$

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

\$\endgroup\$
4
  • 1
    \$\begingroup\$ Use [...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]. \$\endgroup\$
    – Neil
    Commented Aug 16, 2016 at 18:35
  • 1
    \$\begingroup\$ If 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.) \$\endgroup\$
    – Neil
    Commented Aug 16, 2016 at 18:38
  • 1
    \$\begingroup\$ You don't have to count the S= part. \$\endgroup\$ Commented Aug 17, 2016 at 12:50
  • 1
    \$\begingroup\$ @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»)). \$\endgroup\$ Commented Aug 17, 2016 at 13:56
2
\$\begingroup\$

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
}
\$\endgroup\$
2
\$\begingroup\$

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)

\$\endgroup\$
3
  • \$\begingroup\$ Strip some apostrophes and pull $x,$y into a variable for 127bytes \$\endgroup\$
    – Veskah
    Commented Jun 14, 2019 at 3:40
  • \$\begingroup\$ -ne works with array 115 bytes \$\endgroup\$
    – mazzy
    Commented Jun 14, 2019 at 6:37
  • \$\begingroup\$ Length -> Count for 114 \$\endgroup\$
    – Veskah
    Commented Jun 26, 2019 at 18:42
2
\$\begingroup\$

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!

\$\endgroup\$
2
\$\begingroup\$

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.
\$\endgroup\$
1
  • \$\begingroup\$ Other than writing :11 as 4P, I am unable to come up with any golf advice \$\endgroup\$
    – Leaky Nun
    Commented Aug 30, 2016 at 4:12
1
\$\begingroup\$

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.

\$\endgroup\$
1
\$\begingroup\$

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
\$\endgroup\$
3
  • 2
    \$\begingroup\$ it's the first time I see "readable" and "perl" in the same post I think! \$\endgroup\$
    – Dada
    Commented Aug 16, 2016 at 16:23
  • \$\begingroup\$ 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($&). \$\endgroup\$
    – Dada
    Commented Aug 16, 2016 at 16:25
  • \$\begingroup\$ 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 $_=<>. \$\endgroup\$
    – Dada
    Commented Aug 16, 2016 at 16:55
1
\$\begingroup\$

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
\$\endgroup\$
12
  • \$\begingroup\$ 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. \$\endgroup\$ Commented Aug 16, 2016 at 22:38
  • \$\begingroup\$ 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) \$\endgroup\$ Commented Aug 16, 2016 at 23:01
  • \$\begingroup\$ @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. \$\endgroup\$
    – user19547
    Commented Aug 16, 2016 at 23:12
  • \$\begingroup\$ @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? \$\endgroup\$
    – user19547
    Commented Aug 16, 2016 at 23:20
  • \$\begingroup\$ 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!) \$\endgroup\$ Commented Aug 17, 2016 at 6:24
1
\$\begingroup\$

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.
\$\endgroup\$
1
\$\begingroup\$

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<vv>v^>v<v^<^^^^v<^<^<<v<<<v<v^^>vv^>><<<v>>^^^>^<^>>>v^v><^^vv^>><^^vv<vv<v^><>>^><<<>>^^v^v<<v^<vv^^^>><>>^v^^^>>^>^<<^>v>^^v>><>v>^>vv^^<vvvv<><^v>^><<>>><^<^v^<<vvv^v<<<<<><><<><><<v>v<v>v><^v^^vvv>><>^>^>^v<<vv^^^v^vv><v><><v<v<<>>>v<^<^v<<>^v<<^v<><>v>>^^>^<v^<<^v^^^vv>><v^<v>^v><^<>>>>^^<vv<>^^>^>v^v^^>><>^^<^v^<v<<v<^<<^^vv>v>^<vv<<^^v^vv^>^^<>v>^>^<>vv><v>>><<><<vv^^<vv<>>^v>^<<vv>^><<>^<v>v><<v^<v<><v>^<^<^><^^^^>>>^<>^^><>>v^<vv^<^<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v>^<vv>v^^>>^^^><vv<><^>>>^<v^<<^^>^>vv^><v<vvv>^^>>>^><<vv>vv>^<>>^^><^v><<>^<<<v^>^")
\$\endgroup\$
2
  • \$\begingroup\$ 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.. \$\endgroup\$
    – mkst
    Commented Jun 18, 2019 at 21:29
  • \$\begingroup\$ @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). \$\endgroup\$ Commented Jun 19, 2019 at 14:40
1
\$\begingroup\$

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
\$\endgroup\$
6
  • 1
    \$\begingroup\$ 1 -1 -99 99"^v<>"? -> -99 0 99 -1 1@5! \$\endgroup\$
    – ngn
    Commented Jun 20, 2019 at 17:43
  • \$\begingroup\$ +/2!.#:' -> +/~=/'^ \$\endgroup\$
    – ngn
    Commented Jun 20, 2019 at 17:57
  • 1
    \$\begingroup\$ First comment works for -2, second one throws an error... what are you trying to do with the ^? \$\endgroup\$
    – mkst
    Commented Jun 21, 2019 at 18:08
  • \$\begingroup\$ 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 :( \$\endgroup\$
    – ngn
    Commented Jun 21, 2019 at 18:34
  • \$\begingroup\$ Yeah, sadly it uses ^ as 'except' rather than null :( \$\endgroup\$
    – mkst
    Commented Jun 21, 2019 at 20:37
0
\$\begingroup\$

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.

\$\endgroup\$
0
\$\begingroup\$

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

\$\endgroup\$
0
\$\begingroup\$

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!

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.