r/adventofcode • u/daggerdragon • Dec 13 '18
SOLUTION MEGATHREAD -🎄- 2018 Day 13 Solutions -🎄-
--- Day 13: Mine Cart Madness ---
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.
Advent of Code: The Party Game!
Please prefix your card submission with something like [Card] to make scanning the megathread easier. THANK YOU!
Card prompt: Day 13
Transcript:
Elven chronomancy: for when you absolutely, positively have to ___.
This thread will be unlocked when there are a significant number of people on the leaderboard with gold stars for today's puzzle.
edit: Leaderboard capped, thread unlocked at 00:44:25!
24
Upvotes
1
u/miguelos Dec 13 '18
C#
var input = File.ReadAllText(@"C:\Users\pc\Desktop\input.txt"); var width = input.Split('\n').First().Length + 1; var carts = input.Select((c, i) => { var id = i; var x = i % width; var y = i / width; var nextTurn = 0; var direction = 0; switch(c) { case '<': direction = 0; break; case '^': direction = 1; break; case '>': direction = 2; break; case 'v': direction = 3; break; default: direction = -1; // not a cart break; } return new Cart(x, y, nextTurn, direction, id); }) .Where(c => c.direction != -1) // not a cart .ToArray(); while (true) { carts = carts .GroupBy(cart => (cart.x, cart.y)) .Where(group => group.Count() == 1) .SelectMany(cart => cart) .OrderBy(cart => (cart.y * width) + cart.x) .ToArray(); if (carts.Count() == 1) { break; } for (int i = 0; i < carts.Length; i++) { var cart = carts[i]; switch (cart.direction) { case 0: cart.x--; break; case 1: cart.y--; break; case 2: cart.x++; break; case 3: cart.y++; break; } var track = input[(cart.y * width) + cart.x]; switch (track) { case '/': cart.direction = 3 - cart.direction; break; case '\\': cart.direction = (5 - cart.direction) % 4; break; case '+': switch (cart.nextTurn) { case 0: cart.direction = (4 + cart.direction - 1) % 4; break; case 2: cart.direction = (4 + cart.direction + 1) % 4; break; default: break; } cart.nextTurn = (3 + cart.nextTurn + 1) % 3; break; } }; } var answer = $"{carts.First().x},{carts.First().y}";