Continuing with the language-independent code challenge on Advent of Code – Day 3, I’m tasked with figuring out how well a drunken elf can guide Santa to houses. Since his instructions had Santa stumbling into the same house multiple times they then added in a robot to help Santa using the same instruction set, which went on to prove automation can be harmful if not done properly.
Wow, I bet you never thought you’d read that paragraph…
The elf gave Santa instructions on how to get around a grid of houses using ^, v, <, and > to point the way. When that failed, they tried again with a robot assistant using the same instructions with Santa taking the odd numbered instructions and his automated assistant taking the even numbered instructions.
My code did not have to change much to partially automate Santa’s job, which is actually kinda disturbing. However, since it didn’t work out so well I went back and added a parameter for them to easily roll back the change. Now it’s easier to go back to happy and jolly instead of cold and…terminator.
SET NOCOUNT ON IF OBJECT_ID('TempDB..#visited') IS NOT NULL BEGIN DROP TABLE #visited END DECLARE @input 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<<>^vv>v^<<v>v<<><v>^vvv<v<vvv^v<vv<v^^^>v><<^<>><v^^>^v^>>^v<^<><v<>>v^<>>v<>>v^^^><^>>vvvv>^v<^><<>>^<>^>vv><v<<>>^^>v^^^><^<<^^v>v<^<<>v>^^vvv^v^>v^<>^^<>v^v>v>v<v^>vv>^^v<>v>>^<>><>v>v^<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><^>>>>>v><><<<>vv<^v>><<>vvv<><<vv<<<^>v^^>>^>^v>><><^^v<>><>>v^>^<vv><<<>><><<v>^^<>>v<><^<vv>vv<^v>^<<<<v<^<<^^>>^<><^>><<>^>v>^^^v>>^<^^v><v^v>^><<><>>^>>^<<v<>^v<>^>^<v>>vv>^vvv<<v<<^>^>^<<^^<>^^^^vvv<>^vv<vvvvv^^>^^<^>>><>v^<><^<<^>v^^v<>>^vv<>v^^<>>v^vvvvv<<v^<v^^>>><vvvvv>><^>vv>v^v^<v<^>^^><^>^^^^v<><^v<<>v^>v>>vv<<>^<v^^>vvv>^^<v^<>vv^><>><v^^v<>^>>^>v><>>^^v>^>^>>>^>v<^v>v>^<^^^^^>>v<v<>>v<<^>^<v<<>^^>><<^><>v<>^^^vv<>^^>><<^^>v>vv>vv>v^>^v>v^^<>>><<v><v<<>>v><>vvv^^v>^^>^vvvv^>^<>^vvvv><v><v<>>><>^<^vv<>^v<^v<>^vvv<<>><vvv^>>^><<vv^<v^>^<v<<^^>^^<^^v^>v<>v^v><>><v^^>>^vvv><^vv>v^<^<^v>>v^^>^vvv^<v^^v^^>v<^<>>^<>>>^^<><^^vv<>^vv^<>>>>^^<<^^<>vv^^><>^^<v<<v>^<v^^>^v<><><>vvv>^v^>>vv<<^v<<>><v>^><^>>>^<^<^^>vv^<<^<>>^^><><<v>^^<v>>v<<vvvv>^v^vv>><^^<<^>>v>v<^^^<^><^^vv>^vv<^<vv<>v><^<><v><^^^>>^<><^<v>>>>v^<v>>>>>v<><^^>v<^<^>><v<>^>vv>^^v^v^<<v<><<<^v^><<^<><<<<v<^>><<<>v>>vv><vv<><<^<^<><vv>^^^^<>v<<<<v>vv<>vv^^^>><>vv^><>>^vv<<><^^vv<>v^>>^<<>^<v^<^>v<' DECLARE @sx smallint = 0 , @sy smallint = 0 , @rx smallint = 0 , @ry smallint = 0 , @useRobot bit = 0 , @x smallint = 0 , @y smallint = 0 , @i smallint = 0 , @i_max smallint = 0 , @char char(1) SELECT @i_max = len(@input) CREATE TABLE #visited ( x smallint , y smallint , PRIMARY KEY (x,y) ) WHILE @i <= @i_max BEGIN SELECT @char = substring(@input, @i, 1) IF @i % 2 = 0 AND @useRobot = 1 BEGIN SELECT @x = @sx , @y = @sy END ELSE BEGIN SELECT @x = @rx , @y = @ry END IF @char = '>' BEGIN SELECT @x = @x + 1 END ELSE IF @char = '<' BEGIN SELECT @x = @x - 1 END ELSE IF @char = '^' BEGIN SELECT @y = @y + 1 END ELSE IF @char = 'V' BEGIN SELECT @y = @y - 1 END IF 0 = (SELECT COUNT(1) FROM #visited WHERE x = @x AND y = @y) BEGIN INSERT INTO #visited(x, y) SELECT @x, @y END IF @i % 2 = 0 AND @useRobot = 1 BEGIN SELECT @sx = @x , @sy = @y END ELSE BEGIN SELECT @rx = @x , @ry = @y END SELECT @i = @i + 1 END SELECT COUNT(1) FROM #visited
I’m not the only one doing this with T-SQL. Andy Warren also posted his very different, yet same results, version in separate posts for the two parts: Day 3 Part 1 and Day 3 Part 2. His came out more complex, but more set-based.
My problem is that I develop T-SQL for the problem at hand with the expected size of the data. Andy is looking ahead and developed a method that will probably scale much better than mine. I’m hedging my bets and hoping the elf in charge of this either sobers up or gets fired.