Day 04
The elves have stacked the toilet rolls (@) amongst other empty slots (.) in a storage room as shown below:
..@@.@@@@.
@@@.@.@.@@
@@@@@.@.@@
@.@@@@..@.
@@.@@@@.@@
.@@@@@@@.@
.@.@.@.@@@
@.@@@.@@@@
.@@@@@@@@.
@.@.@@@.@.
To get to them, they need space around them (<4 neighbouring rolls @)
Tips
-
parse the file into a text array and then convert (
replace) all the positions with.by 0 and all the positions with@by 1 to get a binary matrix -
this is a classic “convolution” problem - you can use
conv2()with a 3x3 kernel of ones to count the number of neighbouring rolls for each position -
be careful on how you construct the neighbourhood kernel… and finally
-
remember that the neighbour count is only relevant at positions where there is a roll (@ / 1)
-
so you will have to combine the neighbourhood information with the current layout of rolls.
Second problem
The second problem is similar, but now you have to iterate the process described in step 1 until no more rolls can be removed.
- there are no
do... whileloops in Matlab, but you can start with awhile trueand thenbreakwhen the condition (e.g.no_more_rolls_removed) is met
Code
Matlab solution
Matlab code / solution for the first part of that problem.
Second part solution of that problem.