diff --git a/2017/README.md b/2017/README.md index 1c36982..f3ecd00 100644 --- a/2017/README.md +++ b/2017/README.md @@ -16,6 +16,7 @@ The current plan, in no particular order: - [ ] Haskell - [ ] Java - [ ] Kotlin +- [x] Matlab - [Day 06](./day-06) - [ ] Node.js - [ ] Objective C - [x] Perl - [Day 05](./day-05/solution.pl) diff --git a/2017/day-06/README.md b/2017/day-06/README.md new file mode 100644 index 0000000..ae519b2 --- /dev/null +++ b/2017/day-06/README.md @@ -0,0 +1,7 @@ +# Usage + +Since this solution is in matlab, it works a bit differently. You can +solve part 1 by calling `solution([original division of wealth])`, which +returns the number of cycles and the point at which it repeated. You can +then call `solution` again with that repetition point to get the length +of that cycle. diff --git a/2017/day-06/solution.m b/2017/day-06/solution.m new file mode 100644 index 0000000..d945c99 --- /dev/null +++ b/2017/day-06/solution.m @@ -0,0 +1,20 @@ +function [cycles,repeat] = solution (division) + division = vec(division); + states = zeros(0, size(division)(1)); + + while ! ismember(transpose(division), states, "rows") + states = [states; transpose(division)]; + + [maxval, maxindex] = max(division); + division(maxindex) = 0; + + for i = 1:maxval + targetIndex = mod(maxindex + i - 1, size(division)(1)) + 1; + division(targetIndex) += 1; + end + + end + + cycles = size(states)(1); + repeat = division; +endfunction