Advent of Code Day1- Contains spoilers!

So I was sat in the office today and my clunky day1 code was irritating me.
I only had a quick 20mins but when I thought about it, it was possible to really cut the code down:

declare @i varchar(max);
declare @n int;
declare @e int;
declare @c int;
declare @d varchar(1);
declare @s int;

set @i='R3, L5, R1, R2, L5, R2, R3, L2, L5, R5, L4, L3, R5, L1, R3, R4, R1, L3, R3, L2, L5, L2, R4, R5, R5, L4, L3, L3, R4, R4, R5, L5, L3, R2, R2, L3, L4, L5, R1, R3, L3, R2, L3, R5, L194, L2, L5, R2, R1, R1, L1, L5, L4, R4, R2, R2, L4, L1, R2, R53, R3, L5, R72, R2, L5, R3, L4, R187, L4, L5, L2, R1, R3, R5, L4, L4, R2, R5, L5, L4, L3, R5, L2, R1, R1, R4, L1, R2, L3, R5, L4, R2, L3, R1, L4, R4, L1, L2, R3, L1, L1, R4, R3, L4, R2, R5, L2, L3, L3, L1, R3, R5, R2, R3, R1, R2, L1, L4, L5, L2, R4, R5, L2, R4, R4, L3, R2, R1, L4, R3, L3, L4, L3, L1, R3, L2, R2, L4, L4, L5, R3, R5, R3, L2, R5, L2, L1, L5, L1, R2, R4, L5, R2, L4, L5, L4, L5, L2, L5, L4, R5, R3, R2, R2, L3, R3, L2, L5';
set @n =0;
set @e =0;
set @c =0

while len(@i) > 0
begin
 set @d= left(left(@i, charindex(',', @i+',')-1),1); --set direction
 set @s =  rtrim(substring(ltrim((left(@i, charindex(',', @i+',')-1))),2,999)) --set steps
 set @c = @c + iif(@c = 0 ,
					iif( @d ='L',270,90),
						iif(@c=270, iif(@d = 'R',-270,-90),
							iif(@d='L',-90,90) )); -- set compas
  set @n= @n + iif(@c = 0, +@s, iif( @c=180,-@s,0));--add to north
  set @e= @e + iif(@c =90, +@s, iif( @c=270,-@s,0));--add to east
  set @i = ltrim(stuff(@i, 1, charindex(',', @i+','), ''));
end
select sum(abs(@n)+abs(@e));

Ok so I had a quick re-write of part 2 as well

declare @i varchar(max);
declare @n int;
declare @e int;
declare @c int;
declare @d varchar(1);
declare @s int;
declare @o varchar(max);
declare @a varchar(10);

set @i='R3, L5, R1, R2, L5, R2, R3, L2, L5, R5, L4, L3, R5, L1, R3, R4, R1, L3, R3, L2, L5, L2, R4, R5, R5, L4, L3, L3, R4, R4, R5, L5, L3, R2, R2, L3, L4, L5, R1, R3, L3, R2, L3, R5, L194, L2, L5, R2, R1, R1, L1, L5, L4, R4, R2, R2, L4, L1, R2, R53, R3, L5, R72, R2, L5, R3, L4, R187, L4, L5, L2, R1, R3, R5, L4, L4, R2, R5, L5, L4, L3, R5, L2, R1, R1, R4, L1, R2, L3, R5, L4, R2, L3, R1, L4, R4, L1, L2, R3, L1, L1, R4, R3, L4, R2, R5, L2, L3, L3, L1, R3, R5, R2, R3, R1, R2, L1, L4, L5, L2, R4, R5, L2, R4, R4, L3, R2, R1, L4, R3, L3, L4, L3, L1, R3, L2, R2, L4, L4, L5, R3, R5, R3, L2, R5, L2, L1, L5, L1, R2, R4, L5, R2, L4, L5, L4, L5, L2, L5, L4, R5, R3, R2, R2, L3, R3, L2, L5';
set @n =0;
set @e =0;
set @c =0;
set @a='';
set @o='';

while len(@i) > 0  and @a=''
begin
 set @d= left(left(@i, charindex(',', @i+',')-1),1); --set direction
 set @s =  rtrim(substring(ltrim((left(@i, charindex(',', @i+',')-1))),2,999)) --set steps
 set @c = @c + iif(@c = 0 ,
                    iif( @d ='L',270,90),
                        iif(@c=270, iif(@d = 'R',-270,-90),
                            iif(@d='L',-90,90) )); -- set compass

    -- need another while to walk the line
    while @s >0 and @a=''
    begin
 	   set @n= @n + iif(@c = 0, +1, iif( @c=180,-1,0));--add to north
 	   set @e= @e + iif(@c =90, +1, iif( @c=270,-1,0));--add to east
	   set @a= iif(@o LIKE '%'+cast(@n as varchar)+';'+cast(@e as varchar)+'%',cast(@n as varchar)+';'+cast(@e as varchar),'');
	   set @o = @o + cast(@n as varchar)+';'+cast(@e as varchar)+',';
	   set @s = @s-1
    end

  set @i = ltrim(stuff(@i, 1, charindex(',', @i+','), ''));
end
select abs(left(@a,charindex(';',@a)-1)) + abs(right(@a,charindex(';',@a)-1))

Leave a Reply