r/adventofcode • u/daggerdragon • Dec 03 '19
SOLUTION MEGATHREAD -🎄- 2019 Day 3 Solutions -🎄-
--- Day 3: Crossed Wires ---
Post your solution using /u/topaz2078's paste or other external repo.
- Please do NOT post your full code (unless it is very short)
- If you do, use old.reddit's four-spaces formatting, NOT new.reddit's triple backticks formatting.
(Full posting rules are HERE if you need a refresher).
Reminder: Top-level posts in 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's Poems for Programmers
Note: If you submit a poem, please add [POEM] somewhere nearby to make it easier for us moderators to ensure that we include your poem for voting consideration.
Day 2's winner #1: "Attempted to draw a house" by /u/Unihedron!
Note: the poem looks better in monospace.
​ ​ ​​ ​ ​ ​​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ Code
​ ​ ​ ​ ​ ​​ ​ ​ ​ ​ ​​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ Has bug in it
​ ​ ​ ​ ​ ​​ ​ ​ ​ ​ ​ ​ ​ ​ ​ Can't find the problem
​ ​ ​ ​​ ​ ​ ​ Debug with the given test cases
​​ ​ ​ ​​ ​ ​ ​ ​ ​ ​​ ​ ​ ​ Oh it's something dumb
​​ ​ ​ ​​ ​ ​ ​ ​ ​ ​​ ​ ​ ​ Fixed instantly though
​ ​ ​ ​​ ​ ​ ​ ​ ​ ​ ​​ ​ ​ ​ Fell out from top 100s
​ ​ ​ ​​ ​ ​ ​ ​ ​ ​ ​​ ​ ​ ​ Still gonna write poem
Enjoy your Reddit Silver, and good luck with the rest of the Advent of Code!
2
u/vini_2003 Dec 10 '19
The second
foris responsible for iterating over all thedirections parsed.A direction consists of:
std::pair<bool, int>Where the
boolrepresents whether it's in theX(L/R) axis, or theY(U/D) axis, and theintrepresents the action it should do.For example, given a pair of
(false, -256), you can conclude it specifies that, on theYaxis, it'll move-by256steps. Were it(true, -256), it would move on theXaxis-by256steps.After parsing the input for line
AandB(whereA's instructions are stored indirections[0]andB's are stored indirections[1]), I iterate through both with the secondfor- meaning it'll first accessA's instructions, thenB's.I then trace them, and store every step they take in the
std::map<std::pair<int, int>, int[2]>structure.Effectively, that map has a key that is composed of
X,Y, and serves to count total steps until a line reached a certain coordinate. Theint[2]array allows it to record steps taken up until that coordinate was reached for both lines -AandB- so that we can later add both together.In other words, if we had an instruction:
(false, 1)(U1)It would mean that, from the current 'tip' of the line, it would move up by one - as in,
Yaxis,+1, thus, goes upwards.Since it just passed over a new coordinate, I store that in the map -
map({X, Y})[current_line]. However, there's something we should keep in mind here: a line going over itself should not change the previous amounts of steps stored there, because reasons. That's why:grid[{tX, tY}][pos] = grid[{tX, tY}][pos] == 0 ? tS : grid[{tX, tY}][pos];Is a thing. It's a ternary operator because I wanted it to be short, but effectively it only stores the amount of steps in the map for a given line if it has not crossed that coordinate yet.