This year, I’m posting my solutions to the puzzles in the Advent of Code.
Click the link to view the puzzle and code, along with a link to the solution on Github.
And now, on with the solution:
Day 22, part 1
Simplified Problem:
Given a list of nodes (your puzzle input), how many pairs follow these rules:
- Node A is not empty (its
Used
is not zero). - Nodes A and B are not the same node.
- The data on node A (its
Used
) would fit on node B (itsAvail
).
Day 22, Part 2
How many steps does it take to move from Node 0,0 to the node with the highest x value, y=0? The data in the nodes must be swapped at each step.
Solution
I was able to solve for Part 1, however I became stuck on Part 2.
For Part 2, I used a solution found on the Day 22 2016 solution thread on Reddit:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
public class Node { private static readonly Regex DigitRegex = new Regex(@"\d+"); public Node(string data) { MatchCollection matches = DigitRegex.Matches(data); PosX = Convert.ToInt32(matches[0].Value); PosY = Convert.ToInt32(matches[1].Value); Size = Convert.ToInt32(matches[2].Value); Used = Convert.ToInt32(matches[3].Value); Available = Convert.ToInt32(matches[4].Value); UsedPercent = Convert.ToInt32(matches[5].Value); } public int Available { get; set; } public int PosX { get; set; } public int PosY { get; set; } public int Size { get; set; } public int Used { get; set; } public int UsedPercent { get; set; } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
class Program { private static readonly List<Node> Nodes = File.ReadAllLines("input.txt").Where(x => x.StartsWith("/dev")).Select(x => new Node(x)).ToList(); static void Main(string[] args) { int nodeCount = Nodes.Where(x => x.Used != 0) .Sum( node => Nodes.Count( x => !(x.PosX == node.PosX && x.PosY == node.PosY) && x.Available >= node.Used)); Console.WriteLine($"{Nodes.Count} nodes found."); Console.WriteLine($"{Nodes.Count(x => x.Used != 0)} nodes are used."); Console.WriteLine($"{nodeCount} node pairs found."); Console.Write("Part 2 solved using http://codepen.io/anon/pen/mOYdKJ"); Console.WriteLine( "Part 2 code created by /u/AndrewGreenH at https://www.reddit.com/r/adventofcode/comments/5jor9q/2016_day_22_solutions/dbi1999/"); Console.ReadKey(); } } |
You can find the full solution on Github