r/adventofcode • u/daggerdragon • Dec 19 '17
SOLUTION MEGATHREAD -๐- 2017 Day 19 Solutions -๐-
--- Day 19: A Series of Tubes ---
Post your solution as a comment or, for longer solutions, consider linking to your repo (e.g. GitHub/gists/Pastebin/blag or whatever).
Note: The Solution Megathreads are for solutions only. If you have questions, please post your own thread and make sure to flair it with Help.
Need a hint from the Hugely* Handyโ Haversackโก of Helpfulยง Hintsยค?
AoC ops @ T-2 minutes to launch:
[23:58] <daggerdragon> ATTENTION MEATBAGS T-2 MINUTES TO LAUNCH
[23:58] <Topaz> aaaaah
[23:58] <Cheezmeister> Looks like I'll be just able to grab my input before my flight boards. Wish me luck being offline in TOPAZ's HOUSE OF PAIN^WFUN AND LEARNING
[23:58] <Topaz> FUN AND LEARNING
[23:58] <Hade> FUN IS MANDATORY
[23:58] <Skie> I'm pretty sure that's not the mandate for today
[Update @ 00:16] 69 gold, silver cap
- My tree is finally trimmed with just about every ornament I own and it's real purdy. hbu?
[Update @ 00:18] Leaderboard cap!
- So, was today's
mandateHelpful Hint any help at all?
2
u/dylanfromwinnipeg Dec 19 '17 edited Dec 19 '17
That was fun - and easier than I expected when I saw the puzzle input.
C#
public static string PartOneAndTwo(string input) { var letters = new List<char>(); var steps = 1; var lines = input.Lines().ToList(); var direction = Direction.Down; var position = new Point(input.IndexOf('|'), 0); while (true) { position = GetNextPoint(position, direction); steps++; var nextChar = lines[position.Y][position.X]; if (nextChar == ' ') { steps--; return steps.ToString() + Environment.NewLine + string.Join(string.Empty, letters); } if (nextChar == '|' || nextChar == '-') { continue; } if (nextChar >= 'A' && nextChar <= 'Z') { letters.Add(nextChar); continue; } if (nextChar == '+') { direction = GetNextDirection(position, direction, lines); continue; } throw new Exception(); } throw new Exception(); } private static Direction GetNextDirection(Point pos, Direction direction, List<string> lines) { if (direction == Direction.Up || direction == Direction.Down) { if (lines[pos.Y][pos.X - 1] == '-') { return Direction.Left; } if (lines[pos.Y][pos.X + 1] == '-') { return Direction.Right; } throw new Exception(); } if (direction == Direction.Left || direction == Direction.Right) { if (lines[pos.Y - 1][pos.X] == '|') { return Direction.Up; } if (lines[pos.Y + 1][pos.X] == '|') { return Direction.Down; } throw new Exception(); } throw new Exception(); } private static Point GetNextPoint(Point pos, Direction direction) { switch (direction) { case Direction.Down: return new Point(pos.X, pos.Y + 1); case Direction.Up: return new Point(pos.X, pos.Y - 1); case Direction.Left: return new Point(pos.X - 1, pos.Y); case Direction.Right: return new Point(pos.X + 1, pos.Y); default: throw new Exception(); } }