In my previous post, I introduced you to the Advent of Code programming challenge, and explained how I’m going through the days, solving both parts of each puzzle. Continuing on, today we have day 3. Since you can only see this if you are up to this level in the challenge, let me explain it to you. In this puzzle, Santa starts delivering packages at a location (based on an infinite two-dimensional grid), and gets directions for which house is next (via N/S/E/W) commands from his trusted elf. Unfortunately, this elf has started his Christmas celebration too early and has consumed too much eggnog, so some houses are being skipped, and others are being visited numerous times. For Part 1, your task is to determine how many houses receive at least one present.
The input file for this challenge is a random series of characters. Going to the North is represented by “^”; South by “v”; East by “>”; and West by “<“. The input file can be obtained from here.
Once again, I’m going to start off by using a virtual / dynamic tally table. This is used to split the string apart position by position. It will then evaluate the character to see if the X or Y offset needs to be adjusted. At this point, the code that I’m using looks like this:
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 |
-- Table to store the interim results DECLARE @temp TABLE ( N INTEGER PRIMARY KEY CLUSTERED, Direction CHAR(1), X_Offset INTEGER, Y_Offset INTEGER, X INTEGER, Y INTEGER); DECLARE @Day3 VARCHAR(MAX) = '>^^v^<>v<<<v<v^>>v^^^<v<>^^><^<<^vv>>>^<<^>><vv<<v^<^^><>>><>v<><>^^<^^^<><>>vv>vv>v<<^>v<>^>v<v^<>v>><>^v<<<<v^vv^><v>v^>>>vv>v^^^<^^<>>v<^^v<>^<vv^^<^><<>^>><^<>>><><vv><>v<<<><><>v><<>^^^^v>>^>^<v<<vv^^<v<^<^>^^v^^^^^v<><^v><<><^v^>v<<>^<>^^v^<>v<v^>v>^^<vv^v><^<>^v<><^><v^><><><<<<>^vv^>^vvvvv><><^<vv^v^v>v<<^<^^v^<>^<vv><v<v^v<<v<<^^>>^^^v^>v<><^vv<<^<>v<v><><v^^><v<>^^>^^>v^>^<<<<v><v<<>v><^v>^>><v^^<^>v<vvvv<>>>>>^v^^>v<v<^<vv>^>^vv^>vv^^v<<^<^^<>v>vv^v>><>>>v^>^>^^v<>^<v<<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<^<><vv>^^^^<>v<<<<v>vv<>vv^^^>><>vv^><>>^vv<<><^^vv<>v^>>^<<>^<v^<^>v<'; -- Start by filling in a table of each step along the way -- start with a dynamic tally table WITH Tens (N) AS (SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1), Hundreds(N) AS (SELECT 1 FROM Tens t1, Tens t2), Millions(N) AS (SELECT 1 FROM Hundreds t1, Hundreds t2, Hundreds t3), Tally (N) AS (SELECT ROW_NUMBER() OVER (ORDER BY (SELECT NULL)) FROM Millions) -- use N (sequential number) to get the character at that position , cte1 AS (SELECT TOP (LEN(@Day3)) N, SUBSTRING(@Day3, N, 1) AS Item FROM Tally) -- evaluate the Directional Item to adjust the X/Y position INSERT INTO @temp (N, Direction, X_Offset, Y_Offset, X, Y) SELECT N, Item, CASE WHEN Item = '>' THEN 1 -- east WHEN Item = '<' THEN -1 -- west ELSE 0 END AS X_Offset, CASE WHEN Item = '^' THEN 1 -- north WHEN Item = 'v' THEN -1 -- south ELSE 0 END AS Y_Offset, 0 AS X, 0 AS Y FROM cte1 UNION ALL -- Add in a starting location, at X/Y 1/1 SELECT 0, 'S', 0, 0, 1, 1 ORDER BY N; |
For the next step, I’m going to use a version of the UPDATE statement that allows updating variables and rows at the same time – and the rows will be updated with the current value of the variables. Sounds kinda quirky, doesn’t it? Well, it’s been a supported syntax in SQL Server for like, forever. Many of those that use it have given it a slang name: “Quirky Update”. Basically, there are two supported syntaxes:
1 2 |
@Variable = expression @Variable = Column = expression |
What we will be doing is to get the X/Y offset, and put this into a variable. Then, the X/Y position will be updated by adding the current value in the X/Y variable to the offset and storing it in the X/Y variable, and also storing it in that row. As the update progresses through the recordset, each row will be updated with the current calculation. This code also includes a “safety check” to ensure that the recordset it indeed updated in the desired order – if at any time the current row being updated is not the next sequentially numbered row, then a divide-by-zero error will be generated, causing the entire operation to fail.
In the manner that we’re using the UPDATE statement, we need to control the order of the rows being operated on. In order to do this, there are about a dozen rules that need to be followed… rules which aren’t documented by Microsoft. Thankfully, Jeff Moden (this really smart guy over at SQLServerCentral.com) has a great article that goes through all of this. So, read and heed the rules at: //www.sqlservercentral.com/articles/T-SQL/68467/.
The final part of the code is:
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 |
-- need some variables for the next step -- since we have a starting position of 0, set @N to -1. Set X & Y to 1. DECLARE @N INTEGER = -1, @X_Offset INTEGER, @Y_Offset INTEGER, @X INTEGER = 1, @Y INTEGER = 1; /* This utilizes a method called the "Quirky Update". This method has a dozen (or so) rules that need to be followed. Unfortunately, these rules are not given by MS, but by some really smart guy over at SQLServerCentral.com So, read and heed the rules at: //www.sqlservercentral.com/articles/T-SQL/68467/ */ UPDATE A SET @N = CASE WHEN N = @N + 1 THEN N ELSE 1/0 END, -- catch rows not being updated in the proper order @X_Offset = X_Offset, @Y_Offset = Y_Offset, @X = @X + @X_Offset, @Y = @Y + @Y_Offset, X = @X, Y = @Y FROM @temp A OPTION (MAXDOP 1); -- now we want to get just the unique houses... the unique X/Y combinations SELECT COUNT(*) AS HousesVisited FROM ( SELECT DISTINCT X, Y FROM @temp ) UselessAlias; |
And there we go – a nice, efficient set-based solution for this first part of the puzzle.
In the second part, the following year Santa decides to create a roboticized version of himself (Robo-Santa), and the workload is split between the two Santas. The elf is still tipsy from the eggnog, and he is using last years directions… but this time the directions are sent to the two Santas alternatively. Again, the puzzle is to find out how many houses received at least one present. To handle this, I’ll add some variables for Robo-Santa, reset some of the variables, and then run a slightly different quirky update:
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 39 |
-- PART 2: Robo-Santa added. Both start at same location. Alternate who moves to new house. -- Add variables for Robo-Santa, and to control which Santa is the one currently getting directions. DECLARE @SantaNbr INTEGER = 1, @X_Offset2 INTEGER, @Y_Offset2 INTEGER, @X2 INTEGER = 1, @Y2 INTEGER = 1; -- Reset the above variables. SET @N = -1; SET @X = 1; SET @Y = 1; UPDATE A SET @N = CASE WHEN N = @N + 1 THEN N ELSE 1/0 END, -- catch rows not being updated in the proper order -- these are for when Santa is getting directions (Santa #1) @X_Offset = CASE WHEN @SantaNbr % 2 <> 0 THEN X_Offset ELSE @X_Offset END, @Y_Offset = CASE WHEN @SantaNbr % 2 <> 0 THEN Y_Offset ELSE @Y_Offset END, @X = @X + CASE WHEN @SantaNbr % 2 <> 0 THEN @X_Offset ELSE 0 END, @Y = @Y + CASE WHEN @SantaNbr % 2 <> 0 THEN @Y_Offset ELSE 0 END, -- these are for when Robo-Santa is getting directions (Santa #2) @X_Offset2 = CASE WHEN @SantaNbr % 2 = 0 THEN X_Offset ELSE @X_Offset2 END, @Y_Offset2 = CASE WHEN @SantaNbr % 2 = 0 THEN Y_Offset ELSE @Y_Offset2 END, @X2 = @X2 + CASE WHEN @SantaNbr % 2 = 0 THEN @X_Offset2 ELSE 0 END, @Y2 = @Y2 + CASE WHEN @SantaNbr % 2 = 0 THEN @Y_Offset2 ELSE 0 END, -- Determine if using Santa or Robo-Santa values X = CASE WHEN @SantaNbr % 2 <> 0 THEN @X ELSE @X2 END, Y = CASE WHEN @SantaNbr % 2 <> 0 THEN @Y ELSE @Y2 END, -- Toggle between Santa and Robo-Santa @SantaNbr = CASE WHEN @SantaNbr = 1 THEN 2 ELSE 1 END FROM @temp A OPTION (MAXDOP 1); -- now we want to get just the unique houses... the unique X/Y combinations SELECT COUNT(*) AS HousesVisited FROM ( SELECT DISTINCT X, Y FROM @temp ) UselessAlias; |
Here I introduces a second set of variables for Robo-Santa, and a SantaNbr variable to flip back and forth between which Santa is the current one. The quirky update is modified to calculate the different variables based upon which Santa is currently receiving the directions, and finally the current Santa is flipped back and forth for each row.