From 9371d007756c4cca39e54a4a7b5bea56089398c5 Mon Sep 17 00:00:00 2001 From: Greg Burri Date: Thu, 3 Dec 2020 12:32:07 +0100 Subject: [PATCH] Use of do block with foldl. --- advent_of_code_2020.jl | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/advent_of_code_2020.jl b/advent_of_code_2020.jl index 9e8b81c..72a9c93 100644 --- a/advent_of_code_2020.jl +++ b/advent_of_code_2020.jl @@ -135,12 +135,11 @@ end # ╔═╡ a6abdfc0-3536-11eb-3003-c13c39952d78 function nb_trees_encountered(area :: AbstractArray{Bool, 2}, moves = [(1, 3)]) + m, n = size(area) map(moves) do (di, dj) - foldl( - ((j, sum), i) -> (mod1(j + dj, size(area, 2)), sum + area[i, j]), - 1:di:size(area, 1), - init = (1, 0) - )[2] + foldl(1:di:m, init = (1, 0)) do (j, sum), i + mod1(j + dj, n), sum + area[i, j] + end[2] end end @@ -172,7 +171,7 @@ Test 1, part 2: $(prod(n_part2) == 336) " end -# ╔═╡ c003a5ae-3537-11eb-00b2-e58a970471ac +# ╔═╡ 6b1aac80-3553-11eb-0816-6b74dcff2b86 input_day3 = parse_day3(readlines("data/day03.txt")) # ╔═╡ ab7f3260-3539-11eb-3f97-fde10d5d852b @@ -205,5 +204,5 @@ md" # ╠═a6abdfc0-3536-11eb-3003-c13c39952d78 # ╠═1814fa70-353c-11eb-20de-51f779e98bdb # ╟─da0fb260-3536-11eb-2c09-8f1b322090ab -# ╟─c003a5ae-3537-11eb-00b2-e58a970471ac +# ╟─6b1aac80-3553-11eb-0816-6b74dcff2b86 # ╟─ab7f3260-3539-11eb-3f97-fde10d5d852b -- 2.45.2