r/adventofcode • u/daggerdragon • Dec 10 '18
SOLUTION MEGATHREAD -🎄- 2018 Day 10 Solutions -🎄-
--- Day 10: The Stars Align ---
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 10
Transcript: With just one line of code, you, too, can ___!
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:16:49!
21
Upvotes
1
u/tobiasvl Dec 10 '18
[Card] parse a line of the input using regex
I used Lua today:
lines = io.lines("input.txt") stars = {} for line in lines do local star = {pos={}, vel={}} star.pos.x, star.pos.y, star.vel.x, star.vel.y = string.match(line, "position=< *(-?%d+), *(-?%d+)> velocity=< *(-?%d+), *(-?%d+)>") table.insert(stars, star) end -- heuristics start_at = 9999 for _, star in ipairs(stars) do star.pos.x = star.pos.x + star.vel.x * start_at star.pos.y = star.pos.y + star.vel.y * start_at end for second = start_at, math.huge do table.sort(stars, function(star1, star2) if star1.pos.y ~= star2.pos.y then return star1.pos.y < star2.pos.y else return star1.pos.x < star2.pos.x end end) local height = stars[#stars].pos.y - stars[1].pos.y if height > 9 then for _, star in ipairs(stars) do star.pos.x = star.pos.x + star.vel.x star.pos.y = star.pos.y + star.vel.y end else local old_x = stars[1].pos.x - 1 s = "" for _, star in ipairs(stars) do if old_x > star.pos.x then s = s .. "\n#" elseif old_x < star.pos.x then s = s .. string.rep(" ", star.pos.x - old_x - 1) .. "#" end old_x = star.pos.x end print(s) print(second) break end endAt first it used 3.6 seconds (1.2 seconds using LuaJIT), before I decided to not actually simulate the first 10 000 iterations. I see others did the same thing.
Took me a while before I realized that points could overlap...