Walking Through Advent Of Code Day 8

Day 8 is gonna be fun-filled and exciting I can tell it from here!

To make life a little simpler for all of us, I used a variable that I just pasted my instructions into. In my case, I called it @FollowThis allowing it to be up to 300 characters in length. Hopefully, no one’s instructions list is over that length – if so you’ll need to make that adjustment.

I also created a variable called @BackUpOfFollowThis that is strictly a copy of the original @FollowThis variable. (No muss, no fuss)

Once I place all of my inputs into the dbo.AOCDay8 table, we can easily start slicing and dicing.

I then split the strings that we were provided as Input into one simple temp table to make our work easier:

SELECT t.TheString, 
        LEFT(t.TheString, 3) AS Starter, 
        SUBSTRING(t.TheString, 8, 3) AS LeftString,
	SUBSTRING(t.TheString, 13, 3) AS RightString
INTO #ThePath
FROM AOCDay8 t 

Yes, that’s right. No need for anything crazy. Simple string manipulation since everything is the exact same length.

From there, we can work on our loop to traverse the path.

DECLARE @Direction char(1),
	@CurrStep int = 0,
	@CurrPos char(3) = 'AAA';

WHILE @CurrPos != 'ZZZ'
BEGIN
        SELECT @Direction = LEFT(@FollowThis, 1); -- Pop Off the First Direction on the Queue

	IF @Direction = 'L' 
	BEGIN
		SELECT @CurrPos = LeftString FROM #ThePath WHERE Starter = @CurrPos;
		SET @CurrStep = @CurrStep + 1;
	END

	IF @Direction = 'R'
	BEGIN
		SELECT @CurrPos = RightString FROM #ThePath WHERE Starter = @CurrPos;
		SET @CurrStep = @CurrStep + 1;
	END
		
	IF LEN(@FollowThis) > 0
	BEGIN
		SET @FollowThis = RIGHT(@FollowThis, LEN(@FollowThis) - 1)
	END
	ELSE
	BEGIN
		SET @FollowThis = CONCAT(@FollowThis, @BackupOfFollowThis)
	END


        IF @CurrStep > 25000 BREAK;   
END

SELECT @CurrStep

For this, I grab the first character off of the @FollowThis variable, check to see if it’s left or right, set that value to my current position, and do it all again. If my @FollowThis has become blank, then we add the @BackUpOfFollowThis to it and do it again.

This process keeps looping over and over until we get to ZZZ. Yes, if there were bad directions and you got caught in an infinite loop, you’d have a problem. That’s why I included the final line in the loop to check to see if the number is over 25K, just stop because something has gone horribly wrong.

Now, do I really need to put a BEGIN/END statement on everything? For example, those that only have 1 line of code between the BEGIN/END? No, I truly don’t – but I include them as a best practice for myself for a few reasons:

  1. I can tell very easily what is included in my IF statement
  2. If I ever want to add anything to that IF statement, then I don’t have to go back and type the BEGIN/END!

And that my friends, is truly how simple this one is. Not a lot of pieces for the traversing of paths, but it can still be a bit challenging. Until next time!

For this one, I wanted to highlight one of my favorite blues artists – Pat Travers… Yes, I know a stretch given today’s topic, but it’s still fun.

1 comment

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.