From: Greg Burri Date: Thu, 3 Dec 2020 11:32:07 +0000 (+0100) Subject: Use of do block with foldl. X-Git-Url: http://git.euphorik.ch/index.cgi?a=commitdiff_plain;h=33aaebf9d0272d19946f3ecf11fdd82bcbed2182;p=advent_of_code_2020.git Use of do block with foldl. --- 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