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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
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