Day 06
Cephalopod maths…
Tips for first problem
-
Pretty straightforward parsing problem. Read in the input file line by line, and split each line into tokens (numbers and operators). I used
splitafterreaddlmto get a matrix of strings. -
You can accrue sum with an
ifstatement inside a loop. Keep track of the current operation (either+or*), and when you hit a blank line, add the current chunk to the total sum and reset the chunk. (I thoughtmapreducemight be useful, but it was more complicated than just a loop with anifstatement.)
Second problem
-
Same idea - but more tricky as the parsing is now harder.
-
One trick I used was to transpose the matrix of strings, so that each row corresponds to a column in the input file. This way, I could easily iterate over each line and process the tokens. In a first version I tried to accrue data for each “chunk” in a vector, but that got complicated quickly. So I ended up looping over and keeping track of the current operation and chunk value with global variables.
Also, I over-wrote the original solution.jl file … so only code for Part 2 is available here. And it needs some refactoring and cleaning up!
Code
Julia solution
Julia code / solution for the first part of that problem (simpler version of the code ended up as solution for B!).
Second part solution of that problem.