From: Greg Burri Date: Thu, 3 Dec 2020 08:31:37 +0000 (+0100) Subject: Day 3 X-Git-Url: http://git.euphorik.ch/index.cgi?a=commitdiff_plain;h=49279f40b66416439a8b855a0a8c4e19c9bc41b1;p=advent_of_code_2020.git Day 3 --- diff --git a/advent_of_code_2020.jl b/advent_of_code_2020.jl index df49843..9e8b81c 100644 --- a/advent_of_code_2020.jl +++ b/advent_of_code_2020.jl @@ -1,9 +1,17 @@ ### A Pluto.jl notebook ### -# v0.12.15 +# v0.12.16 using Markdown using InteractiveUtils +# ╔═╡ 661be9a0-353b-11eb-3598-a5b5245368cb +html""" +""" + # ╔═╡ f0dd4400-3313-11eb-3295-af913c2212fb md"# Advent Of Code 2020" @@ -56,6 +64,15 @@ struct Password p :: String end +# ╔═╡ bbda1c1e-347a-11eb-0e7a-756906c4c9f4 +nb_of_valid_passwords_part2(passwords) = + count( + password -> + (password.p[password.policy_range.start] == password.policy_char) ⊻ + (password.p[password.policy_range.stop] == password.policy_char), + passwords + ) + # ╔═╡ 10f5ffa0-3474-11eb-2a36-d5c6f78f18c6 function parse_day2(lines) line_regexp = r"^(\d+)-(\d+) ([a-z]): ([a-z]+)$" @@ -75,17 +92,7 @@ end # ╔═╡ 6695038e-3472-11eb-1846-3b1c041be59d nb_of_valid_passwords_part1(passwords) = count( - password -> - count(==(password.policy_char), password.p) ∈ password.policy_range, - passwords - ) - -# ╔═╡ bbda1c1e-347a-11eb-0e7a-756906c4c9f4 -nb_of_valid_passwords_part2(passwords) = - count( - password -> - (password.p[password.policy_range.start] == password.policy_char) ⊻ - (password.p[password.policy_range.stop] == password.policy_char), + password -> count(==(password.policy_char), password.p) ∈ password.policy_range, passwords ) @@ -118,7 +125,65 @@ md"**Result for part 1**: $(nb_of_valid_passwords_part1(input_day2))" # ╔═╡ de3c2f00-347b-11eb-0aec-8b776a855f6a md"**Result for part 2**: $(nb_of_valid_passwords_part2(input_day2))" +# ╔═╡ 14e39f60-3536-11eb-0c2a-5945a1ccb638 +md"## Day 3" + +# ╔═╡ 1b7ec110-3536-11eb-04c4-af7b43e6c68e +function parse_day3(lines) + map(==("#"), hcat(split.(lines, "")...))' +end + +# ╔═╡ a6abdfc0-3536-11eb-3003-c13c39952d78 +function nb_trees_encountered(area :: AbstractArray{Bool, 2}, moves = [(1, 3)]) + 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] + end +end + +# ╔═╡ 1814fa70-353c-11eb-20de-51f779e98bdb +nb_trees_encountered_part2(area) = nb_trees_encountered(area, [(1, 1), (1, 3), (1, 5), (1, 7), (2, 1)]) + +# ╔═╡ da0fb260-3536-11eb-2c09-8f1b322090ab +let + test_input = + """..##....... + #...#...#.. + .#....#..#. + ..#.#...#.# + .#...##..#. + ..#.##..... + .#.#.#....# + .#........# + #.##...#... + #...##....# + .#..#...#.#""" + trees = parse_day3(split(test_input, '\n')) + n_part1 = nb_trees_encountered(trees) + n_part2 = nb_trees_encountered_part2(trees) + + md" +Test 1, part 1: $(n_part1 == [7]) + +Test 1, part 2: $(prod(n_part2) == 336) + " +end + +# ╔═╡ c003a5ae-3537-11eb-00b2-e58a970471ac +input_day3 = parse_day3(readlines("data/day03.txt")) + +# ╔═╡ ab7f3260-3539-11eb-3f97-fde10d5d852b +md" +**Result for part 1**: $(nb_trees_encountered(input_day3)) + +**Result for part 2**: $(nb_trees_encountered_part2(input_day3) |> prod) +" + # ╔═╡ Cell order: +# ╟─661be9a0-353b-11eb-3598-a5b5245368cb # ╟─f0dd4400-3313-11eb-3295-af913c2212fb # ╟─0b7b8920-3314-11eb-2cfb-7d20c0967e67 # ╠═877b32e0-3315-11eb-0e96-650b19da693e @@ -128,10 +193,17 @@ md"**Result for part 2**: $(nb_of_valid_passwords_part2(input_day2))" # ╟─c7dcdae0-33b1-11eb-2cab-fbc06c8ef76b # ╟─5d9712b0-3472-11eb-1423-719331dfe52f # ╠═b9144990-3473-11eb-0ba9-59f3fe6ddbc0 +# ╠═bbda1c1e-347a-11eb-0e7a-756906c4c9f4 # ╠═10f5ffa0-3474-11eb-2a36-d5c6f78f18c6 # ╠═6695038e-3472-11eb-1846-3b1c041be59d -# ╠═bbda1c1e-347a-11eb-0e7a-756906c4c9f4 # ╟─a0cfb670-3474-11eb-1a05-c12914658a0a # ╟─4a629040-347a-11eb-2aae-6f37a4c89168 # ╟─5825aa50-347a-11eb-37ca-811e1b59d37a # ╟─de3c2f00-347b-11eb-0aec-8b776a855f6a +# ╟─14e39f60-3536-11eb-0c2a-5945a1ccb638 +# ╠═1b7ec110-3536-11eb-04c4-af7b43e6c68e +# ╠═a6abdfc0-3536-11eb-3003-c13c39952d78 +# ╠═1814fa70-353c-11eb-20de-51f779e98bdb +# ╟─da0fb260-3536-11eb-2c09-8f1b322090ab +# ╟─c003a5ae-3537-11eb-00b2-e58a970471ac +# ╟─ab7f3260-3539-11eb-3f97-fde10d5d852b diff --git a/data/day03.txt b/data/day03.txt new file mode 100644 index 0000000..26d1b5b --- /dev/null +++ b/data/day03.txt @@ -0,0 +1,323 @@ +..#......###....#...##..#.#.... +.#.#.....#.##.....###...##...## +..#.#..#...........#.#..#...... +..#......#..........###........ +...#..###..##.#..#.......##..## +......#.#.##...#...#....###.... +..........##.....##..##......#. +......#...........#............ +#....#..........#.............. +.#........##.............###.## +....#.........#.......#.#....## +#.#..#..#..#.......#...#....##. +.#........#......#.##.......#.. +..#.....#####.....#....#..#..## +.......#..##.......#......#.### +..#.#...#......#.##...#........ +##...................#...##..#. +......#...#.##...##.#......#..# +.#.................#..##...#... +...#.....#.......##.....#.#.... +.......#.#......#.....#..#..##. +..........#........#........... +..#.#.......................... +.#.##..#.#...#...#.........#... +.....#....#.....#..#.....#..... +...#.#.#.....#.#..#.......#..#. +.....#...###...##...#......##.. +#.###......#.#...#.#.#..###.... +#.....#..##......#..........#.# +#...............#........#.#..# +.....#..#.........#......##.#.. +.....#.##.##..#..##............ +...#......##...............#.#. +.#..#.#............##.#........ +#.....#..###.............##.#.. +...##..#.#..#...........#..#... +#....#.........#.#............. +##.#.........#..###......#.#..# +...#...#......#.#.#.##..#.##... +.....##............#.##.##..#.. +....#................#.##..#..# +...#..#.......#...#..#........# +....#...#...#................#. +....##...............#.#...#... +.#.....###...#.......#.##...... +....######.#..............###.# +.#..#.........##............... +................##.#..#....###. +.......#............#.#..#..#.. +......#.#...............##.#... +...#..####.#...#..#..#......#.. +....#.#...#.....#.........#..## +.##..#...#......##....##.#.#... +.##.#.........##...#....#...... +..#.#..#...#.#..#.......#...#.# +.........#..#.....##..#........ +..#......#..##.....#..#...###.. +..#...#....#.#..#..#.#.#..#.#.. +...#..#####.....#......#....... +#.#............#......#..#...#. +.........#..........###.......# +......#....#..#.##.#......#..#. +...........##.#....#.#..#...... +..#...................#..#.#... +#....##.............##....#...# +##..#....#.........#..........# +....#.#.#...#..#........#.##..# +...............#...#..##..#.... +.##.......#.......#...........# +#.........................##... +#........#.#..#..##..####.#.... +...................##.....###.. +.#.......#..#......#......#...# +..#.........#...#..........#... +..........#......#....#........ +.#......#..#...#..#...##....##. +...#.#..#..#......#.....##.#### +.......#.#....#.......#........ +#...#.#...##..##.#......#...... +.#.........#................... +...#..........#.#......#....... +...#.....##....#..........#.... +.#..........##..#..#..##....#.# +.##.#..........#...#.##.......# +#...###....#..#.#...#..#....... +..................##........... +..#...##.#...........#....#.##. +..#......#..##..#....##..#...#. +..#....#.....#.##..#.......#..# +#...#....#..#.#....#......##... +.......##..#..........#........ +..#.............##.#.....#...#. +...............#....#...#...##. +##...........#.......#.##...... +#..#...........#.........#..... +....###.............###.##..##. +.........#.#.....###.......#... +..#.##....#.#..........#....#.. +#........#....##...#..#........ +......#..........###..#.#...... +.....#.#......##.....#..##...#. +.#.......#......#...#...#...#.# +.#..........##.......#.....##.# +###.#...#....#.....#...#......# +..#.#.#..#.##.#..#............. +.....#......................... +.#..###..#...#...#..#..#...#.#. +#................##...##.##.... +......#...#...#..........#...#. +..........#.....##............. +..#.#......#........#.......#.. +........##.............#....... +.......#......#.##.#..#........ +#.#.#....#........#..........#. +##..##......#..#..#.....#.#..## +##..#..........#............... +#.....##...#.#......#.......#.# +#.....#...#....#..#.....##..... +##..........#.#.....#....#...## +..##.###..#.....#.......#...#.. +.#.#.......#......###........#. +.#..............#.#..###....... +.#....#..##.........#..#.#..... +....#....#.#....#..#.......##.# +#.......#.......#.........#.... +...#....#....#.....##..#..#.#.# +........#....#...........#..... +.#......##.#.#.##.............. +#..#.#.....##........#........# +##...#.#.......##.......#...#.. +#...#.....#.##...##.#.....#.... +....#..##...#........#.#...#... +...#....#.#.#..###...##.#.....# +......#..#.....#..#........##.. +.......#.....#.#.........#.#..# +..#.......#.#.#.#.#....#.##...# +.#...#........#..##..#......#.. +.#..#............#...#..#.#.... +...##......#......#............ +..#...#.#.....#.....#..##.#.... +.#......#.#......#..#.#........ +..#..........##...#.#.....#..#. +#...#.....#..#...#............. +..##.................#....#.... +.#....#.......#..##....#......# +.#....###............##....##.# +##..#........#..#...#.......#.. +.....#.....#.#.#.##.........#.. +.......#..#....#...#...#....... +...#...#...#.#.#..#.#.....#.... +#.#........#..#.##..#..###..... +..................#..#......... +#.#.....#..##.........#.......# +###..#.......#..............#.. +......#..#.....###..........#.. +....#.#...#..#...........#.#... +...#.....#.......#.....#.#..... +#.....##..#......##...........# +#...###...........##..#...#.##. +......##.##.#...#..#....#...... +...#.#......##.#......##....#.# +..............#.#.###.......#.. +........#....#.......##..#..### +...#.....##.#....#......##..#.# +..##........#.....#.#..#...#... +.#..#.##.........#.....#...#..# +..#..#....#...........#........ +.#...#....................#.... +##.....##....#.............#.#. +....#.#..#.#..#.#.#..........## +.............##.#.....#..#..#.. +.#....#.....##...#.###......... +..#........#........#.#..###... +.##....#...#...#.......#...#.#. +..#...#...#..##........#..#.... +..##.#..#..#.....#......#.#..#. +.#........#..#....#..#......... +..#.#.....#.##..#........###.#. +.....#.##.....##.#............. +#.........#.......#...##...#... +..#.##.#..#..#............#.... +.##....#..#............#.....#. +###........##.....##.#...#..... +#......##..##.#.#.#.#.#.#..##.. +.....###.....#....#......#....# +........#.........##...#....#.# +.#.#.....#.#..#..##......#...#. +...#.##....#..#.###..#..##..... +....#..........##..#..#..#..#.. +...#..#.##..#..#....#.........# +.....#..###.#.....#.....#..#... +......#...#....#.##...#.#...... +.#.###..##.....##.##......##... +.....#.#...........#.#......... +#........#...#..#......##.#.... +..#.......##....##....#.##.#..# +...###.#.........#......#.....# +..#.##..#....#.....##...#.##... +....##.##.............#...#.... +##..#...#..#..#..#............. +.....#.....#.....#............. +...#.##.......#..#.#.....#....# +#.....##.........#......##..... +.....##..........#..#...#..#... +#...###....#.......#...##...... +.#....#..#......#.....#...#.#.. +#........#.#.#...#.....###.#.## +##...#...##..#..#....#......... +....#............#..#.....#.... +#......#.........##....#....... +.#..#..#........#.............# +.##..........#......#.......#.. +#............#..#....#......... +....#.#.....#.##...#.....#.#... +...#.#..#...##..#...#.#.#...... +#....#..#.........##..#.#.#..## +.#...#..............#.......#.. +#...#.....#.#........##......## +...#....##.####.#.........#.#.# +....###.#..#............#.#..#. +....#......#...#......##.#.#.#. +.....#..#.#.##.#...##.......... +##..#...#.#...###.............# +....#...#..#.....#.#..#..#..#.. +#..........####......#.....###. +.........#........#.##.#...#... +.........#..........#.#..###... +.....##........##.........#...# +..##....#...#.......##......... +.....#.#......##....#...#...#.. +.##..#..##..................... +.......#...#..#..#...##....#... +.#...#.......###...#..#..#..... +.......#.....##.##.#.......#..# +.##......#...#....#..#......##. +.##....#..#....#...#...#....... +.........##..#..#.#.#.....##... +...#..............#..#.....#### +.#.#.#..#.......#.......#...... +..#.#......#..........#........ +.#...#.#..#.......#..#..#..#... +.......##.#...#..#....#.....#.. +.##...##....##...#........####. +....#.#..##....#...#....#.#.... +.....#.....#..#..#.#.##..#..... +..#....#..............#....#... +..#.#.#.....##.#.....#..##..... +....#.....#....#...#...#..#.#.. +#...#...........#..#..#........ +...#.#..#.........##.#...#..##. +......#.#.........#.#...#...... +......#..##.###......##.#....#. +.....#...#..#.......#.......... +.#...#.......#.....###......#.. +...........##.....#..#..#....#. +..#....#..#...#......#.......#. +..#...#...#.#..#....#...#...... +.......#....###.####...###.#... +#.##.#.......#.......#....#.#.# +.##..........#.....#..###...... +.....#...........#.##..#....#.. +........##.....#.#........##... +#..#..#..................##.... +#...###..........#............. +.......#.#.......#.#.......##.. +.....#.#...#....#...####.....#. +..##.....##.......#....#....... +##..........#...#..##....##.... +..........#..#......#........#. +##..#....#..#....#.....##....#. +##.##.....#...##.##.......#.... +..#..#.###.#..##.#..#..#...#... +.#..#.....#........#...##.#.... +..#..#.....#.#......##.#.#..... +.#..##...#.#....#...#...#.#.##. +.........#...#....###.#.....#.. +...........###.#.#..#..#...#.#. +##...#......##...........#..#.. +.........##..#...#.......#..... +#......#.#..........#..#....... +...#.................#....#.... +#....#......................##. +##.......#..#......#.#...###.#. +..#....#..#.#......#........... +...#...........###.#.#......... +..#..##.....#.....##...##...... +..#..#.#.#.#..#..#..##....#...# +#......##.....##..##.##...#.... +#.....#.....#.#........#....... +.#.....#.................#....# +.###....#...#............#.#.#. +.#...#.#......#.#.............. +....#...#......#.....#.......#. +........#.....#..........#....# +#..#......#...#...#.........#.. +#....#......#...##.#...#...#... +#...#....#....#..#..#.....#..#. +#......##..#..#.#.#..#.#....... +..#..#...............#...##...# +............#..............#.## +.#.#.#......##.......#.......#. +....#.........##.......#...###. +.......#.#...#.#.#.......#..... +....#..#..#...#....#.##.#.##... +...##.##.#...#......#.......... +#.....#...#.#...#.##..##.#..... +.......#.....#...#.#...##.#.... +.#.............#.....#....##..# +##......#.......#...#....#..... +.###......#.................#.. +#.#......##.........##..#...... +...#....#..........#.#......... +..##..#.........#.............. +.....#...#..................#.# +.............#.........#...#..# +....#....#......#.#.......#...# +#..#............#.#.......#...# +..#.....#............#......... +.#.....................###....# +........#.####.........#.#.#... +#...........##...#.........#..# +...........#..#......#...#.#... +....##...##.....#.....#........