From 1f659592631e3d54dc768b2e61dc1db93850ad0a Mon Sep 17 00:00:00 2001 From: Greg Burri Date: Wed, 2 Dec 2020 13:56:51 +0100 Subject: [PATCH] Rename 'main.jl' + some code clean up. --- advent_of_code_2020.jl | 137 +++++++++++++++ data/day01.txt | 200 ++++++++++++++++++++++ main.jl => data/day02.txt | 351 +------------------------------------- 3 files changed, 339 insertions(+), 349 deletions(-) create mode 100644 advent_of_code_2020.jl create mode 100644 data/day01.txt rename main.jl => data/day02.txt (80%) diff --git a/advent_of_code_2020.jl b/advent_of_code_2020.jl new file mode 100644 index 0000000..df49843 --- /dev/null +++ b/advent_of_code_2020.jl @@ -0,0 +1,137 @@ +### A Pluto.jl notebook ### +# v0.12.15 + +using Markdown +using InteractiveUtils + +# ╔═╡ f0dd4400-3313-11eb-3295-af913c2212fb +md"# Advent Of Code 2020" + +# ╔═╡ 0b7b8920-3314-11eb-2cfb-7d20c0967e67 +md"## Day 1" + +# ╔═╡ 877b32e0-3315-11eb-0e96-650b19da693e +input_day1 = parse.(Int64, split(readline("data/day01.txt"))) |> sort + +# ╔═╡ 4adcf8d0-33a8-11eb-0220-696f06894ef3 +function day1_part1(input) + for n ∈ input + r = searchsorted(input_day1, 2020 - n) + if r.start ≤ r.stop + return n * input[r.start] + end + end +end + +# ╔═╡ 1e8e7230-33ae-11eb-0a77-035e6a4b9ba8 +function day1_part2(input) + l = length(input) + for i ∈ 1:l + for j ∈ i+1:l + v = 2020 - input[i] - input[j] + if v ≤ 0 + break + end + r = searchsorted(input_day1, v) + if r.start ≤ r.stop + return input[i] * input[j] * input[r.start] + end + end + end +end + +# ╔═╡ 7a037310-33ac-11eb-34fb-9bf552883937 +md"**Result for part 1:** $(day1_part1(input_day1))" + +# ╔═╡ c7dcdae0-33b1-11eb-2cab-fbc06c8ef76b +md"**Result for part 2:** $(day1_part2(input_day1))" + +# ╔═╡ 5d9712b0-3472-11eb-1423-719331dfe52f +md"## Day 2" + +# ╔═╡ b9144990-3473-11eb-0ba9-59f3fe6ddbc0 +struct Password + policy_range :: UnitRange + policy_char :: Char + p :: String +end + +# ╔═╡ 10f5ffa0-3474-11eb-2a36-d5c6f78f18c6 +function parse_day2(lines) + line_regexp = r"^(\d+)-(\d+) ([a-z]): ([a-z]+)$" + + function parse_line(line) + m = match(line_regexp, line) + Password( + UnitRange(parse(Int, m.captures[1]), parse(Int, m.captures[2])), + m.captures[3][1], + m.captures[4] + ) + end + + parse_line.(lines) +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), + passwords + ) + +# ╔═╡ a0cfb670-3474-11eb-1a05-c12914658a0a +let + test1_input = + split(""" + 1-3 a: abcde + 1-3 b: cdefg + 2-9 c: ccccccccc""", + '\n' + ) + + test1_part1_result = nb_of_valid_passwords_part1(parse_day2(test1_input)) + test1_part2_result = nb_of_valid_passwords_part2(parse_day2(test1_input)) + + md" +Test 1, part 1: $(test1_part1_result == 2) + +Test 1, part 2: $(test1_part2_result == 1) + " +end + +# ╔═╡ 4a629040-347a-11eb-2aae-6f37a4c89168 +input_day2 = parse_day2(readlines("data/day02.txt")) + +# ╔═╡ 5825aa50-347a-11eb-37ca-811e1b59d37a +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))" + +# ╔═╡ Cell order: +# ╟─f0dd4400-3313-11eb-3295-af913c2212fb +# ╟─0b7b8920-3314-11eb-2cfb-7d20c0967e67 +# ╠═877b32e0-3315-11eb-0e96-650b19da693e +# ╠═4adcf8d0-33a8-11eb-0220-696f06894ef3 +# ╠═1e8e7230-33ae-11eb-0a77-035e6a4b9ba8 +# ╟─7a037310-33ac-11eb-34fb-9bf552883937 +# ╟─c7dcdae0-33b1-11eb-2cab-fbc06c8ef76b +# ╟─5d9712b0-3472-11eb-1423-719331dfe52f +# ╠═b9144990-3473-11eb-0ba9-59f3fe6ddbc0 +# ╠═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 diff --git a/data/day01.txt b/data/day01.txt new file mode 100644 index 0000000..a679df5 --- /dev/null +++ b/data/day01.txt @@ -0,0 +1,200 @@ +1254 +1313 +1456 +1782 +1742 +1391 +1273 +1286 +1373 +1891 +1188 +1994 +1887 +1816 +1984 +961 +1428 +1105 +1123 +1699 +1154 +1953 +1977 +1450 +1696 +1068 +1241 +1926 +1228 +1591 +1789 +1966 +1508 +1193 +1190 +1654 +444 +1282 +1169 +1165 +1778 +1669 +1570 +1671 +1845 +1208 +1728 +1798 +847 +1300 +1817 +1200 +1297 +1658 +1296 +1571 +1991 +1305 +1314 +1903 +1472 +1359 +1506 +1999 +1877 +1168 +1137 +1288 +1083 +1656 +413 +1430 +1408 +1397 +1846 +1218 +684 +1234 +2007 +900 +1604 +1460 +1848 +1693 +1324 +1401 +1968 +1918 +1975 +1665 +1413 +1874 +1852 +1521 +1753 +1229 +1940 +1650 +1976 +1235 +1130 +1927 +1365 +1908 +1441 +1302 +1986 +1449 +1692 +1944 +1277 +1312 +1826 +1231 +1289 +1814 +1170 +1606 +1608 +1773 +1883 +1936 +1626 +1497 +1860 +1673 +1295 +2005 +1481 +1995 +1734 +1646 +1557 +1298 +1174 +1894 +1309 +1240 +1982 +1414 +1799 +1620 +1863 +1220 +1642 +508 +1146 +1187 +1253 +1284 +1952 +1527 +1610 +1333 +1221 +1362 +1457 +1721 +1493 +1330 +156 +1647 +1841 +1724 +2000 +1398 +1745 +1985 +1269 +1722 +2001 +1923 +1395 +1094 +1140 +1958 +1239 +1336 +1588 +1338 +1750 +1757 +1444 +1822 +1335 +1941 +1865 +1767 +1881 +1499 +1157 +1990 +1210 +1779 +1201 +1784 +1961 +1476 +1861 +1468 \ No newline at end of file diff --git a/main.jl b/data/day02.txt similarity index 80% rename from main.jl rename to data/day02.txt index dd5e7f5..4c4c5a3 100644 --- a/main.jl +++ b/data/day02.txt @@ -1,323 +1,4 @@ -### A Pluto.jl notebook ### -# v0.12.15 - -using Markdown -using InteractiveUtils - -# ╔═╡ f0dd4400-3313-11eb-3295-af913c2212fb -md"# Advent Of Code 2020" - -# ╔═╡ 0b7b8920-3314-11eb-2cfb-7d20c0967e67 -md"## Day 1" - -# ╔═╡ 877b32e0-3315-11eb-0e96-650b19da693e -begin - input_day1_raw = "1254 - 1313 - 1456 - 1782 - 1742 - 1391 - 1273 - 1286 - 1373 - 1891 - 1188 - 1994 - 1887 - 1816 - 1984 - 961 - 1428 - 1105 - 1123 - 1699 - 1154 - 1953 - 1977 - 1450 - 1696 - 1068 - 1241 - 1926 - 1228 - 1591 - 1789 - 1966 - 1508 - 1193 - 1190 - 1654 - 444 - 1282 - 1169 - 1165 - 1778 - 1669 - 1570 - 1671 - 1845 - 1208 - 1728 - 1798 - 847 - 1300 - 1817 - 1200 - 1297 - 1658 - 1296 - 1571 - 1991 - 1305 - 1314 - 1903 - 1472 - 1359 - 1506 - 1999 - 1877 - 1168 - 1137 - 1288 - 1083 - 1656 - 413 - 1430 - 1408 - 1397 - 1846 - 1218 - 684 - 1234 - 2007 - 900 - 1604 - 1460 - 1848 - 1693 - 1324 - 1401 - 1968 - 1918 - 1975 - 1665 - 1413 - 1874 - 1852 - 1521 - 1753 - 1229 - 1940 - 1650 - 1976 - 1235 - 1130 - 1927 - 1365 - 1908 - 1441 - 1302 - 1986 - 1449 - 1692 - 1944 - 1277 - 1312 - 1826 - 1231 - 1289 - 1814 - 1170 - 1606 - 1608 - 1773 - 1883 - 1936 - 1626 - 1497 - 1860 - 1673 - 1295 - 2005 - 1481 - 1995 - 1734 - 1646 - 1557 - 1298 - 1174 - 1894 - 1309 - 1240 - 1982 - 1414 - 1799 - 1620 - 1863 - 1220 - 1642 - 508 - 1146 - 1187 - 1253 - 1284 - 1952 - 1527 - 1610 - 1333 - 1221 - 1362 - 1457 - 1721 - 1493 - 1330 - 156 - 1647 - 1841 - 1724 - 2000 - 1398 - 1745 - 1985 - 1269 - 1722 - 2001 - 1923 - 1395 - 1094 - 1140 - 1958 - 1239 - 1336 - 1588 - 1338 - 1750 - 1757 - 1444 - 1822 - 1335 - 1941 - 1865 - 1767 - 1881 - 1499 - 1157 - 1990 - 1210 - 1779 - 1201 - 1784 - 1961 - 1476 - 1861 - 1468" - input_day1 = parse.(Int64, split(input_day1_raw)) |> sort -end - -# ╔═╡ 4adcf8d0-33a8-11eb-0220-696f06894ef3 -function day1_part1(input) - for n ∈ input - r = searchsorted(input_day1, 2020 - n) - if r.start ≤ r.stop - return n * input[r.start] - end - end -end - -# ╔═╡ 1e8e7230-33ae-11eb-0a77-035e6a4b9ba8 -function day1_part2(input) - l = length(input) - for i ∈ 1:l - for j ∈ i+1:l - v = 2020 - input[i] - input[j] - if v ≤ 0 - break - end - r = searchsorted(input_day1, v) - if r.start ≤ r.stop - return input[i] * input[j] * input[r.start] - end - end - end -end - -# ╔═╡ 7a037310-33ac-11eb-34fb-9bf552883937 -md"**Result for part 1:** $(day1_part1(input_day1))" - -# ╔═╡ c7dcdae0-33b1-11eb-2cab-fbc06c8ef76b -md"**Result for part 2:** $(day1_part2(input_day1))" - -# ╔═╡ 5d9712b0-3472-11eb-1423-719331dfe52f -md"## Day 2" - -# ╔═╡ b9144990-3473-11eb-0ba9-59f3fe6ddbc0 -struct Password - policy_range :: UnitRange - policy_char :: Char - p :: String -end - -# ╔═╡ 10f5ffa0-3474-11eb-2a36-d5c6f78f18c6 -function parse_day2(input :: String) :: AbstractArray{Password} - line_regexp = r"^(\d+)-(\d+) ([a-z]): ([a-z]+)$" - result = Array{Password, 1}() - for line ∈ split(input, '\n') - m = match(line_regexp, line) - push!( - result, - Password( - UnitRange(parse(Int64, m.captures[1]), parse(Int64, m.captures[2])), - m.captures[3][1], - m.captures[4] - ) - ) - end - result -end - -# ╔═╡ 6695038e-3472-11eb-1846-3b1c041be59d -function nb_of_valid_passwords_part1(passwords :: AbstractArray{Password}) - sum( - function (password) - m = 0 - for c in password.p - m += c == password.policy_char - end - m ∈ password.policy_range - end, - passwords - ) -end - -# ╔═╡ bbda1c1e-347a-11eb-0e7a-756906c4c9f4 -function nb_of_valid_passwords_part2(passwords :: AbstractArray{Password}) - sum( - password -> - (password.p[password.policy_range.start] == password.policy_char) ⊻ - (password.p[password.policy_range.stop] == password.policy_char), - passwords - ) -end - -# ╔═╡ a0cfb670-3474-11eb-1a05-c12914658a0a -let - test1_input = "1-3 a: abcde -1-3 b: cdefg -2-9 c: ccccccccc" - - test1_part1_result = nb_of_valid_passwords_part1(parse_day2(test1_input)) - test1_part2_result = nb_of_valid_passwords_part2(parse_day2(test1_input)) - - md" -Test 1, part 1: $(test1_part1_result == 2) - -Test 1, part 2: $(test1_part2_result == 1) - " -end - -# ╔═╡ 2b112350-347a-11eb-1725-5f70e3623379 -input_day2_raw = "1-13 r: gqdrspndrpsrjfjx +1-13 r: gqdrspndrpsrjfjx 5-16 j: jjjjkjjzjjjjjfjzjjj 14-16 r: rrrnrrrrrcnrgxrr 1-3 k: bkktwhgktv @@ -1316,32 +997,4 @@ input_day2_raw = "1-13 r: gqdrspndrpsrjfjx 4-6 g: kggggg 6-7 c: cccccdqcc 2-6 x: vjkxbrfwnj -16-18 s: kssssssswssssssssssb" - -# ╔═╡ 4a629040-347a-11eb-2aae-6f37a4c89168 -input_day2 = parse_day2(input_day2_raw) - -# ╔═╡ 5825aa50-347a-11eb-37ca-811e1b59d37a -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))" - -# ╔═╡ Cell order: -# ╟─f0dd4400-3313-11eb-3295-af913c2212fb -# ╟─0b7b8920-3314-11eb-2cfb-7d20c0967e67 -# ╟─877b32e0-3315-11eb-0e96-650b19da693e -# ╠═4adcf8d0-33a8-11eb-0220-696f06894ef3 -# ╠═1e8e7230-33ae-11eb-0a77-035e6a4b9ba8 -# ╟─7a037310-33ac-11eb-34fb-9bf552883937 -# ╟─c7dcdae0-33b1-11eb-2cab-fbc06c8ef76b -# ╟─5d9712b0-3472-11eb-1423-719331dfe52f -# ╠═b9144990-3473-11eb-0ba9-59f3fe6ddbc0 -# ╟─10f5ffa0-3474-11eb-2a36-d5c6f78f18c6 -# ╠═6695038e-3472-11eb-1846-3b1c041be59d -# ╠═bbda1c1e-347a-11eb-0e7a-756906c4c9f4 -# ╟─a0cfb670-3474-11eb-1a05-c12914658a0a -# ╟─2b112350-347a-11eb-1725-5f70e3623379 -# ╟─4a629040-347a-11eb-2aae-6f37a4c89168 -# ╟─5825aa50-347a-11eb-37ca-811e1b59d37a -# ╟─de3c2f00-347b-11eb-0aec-8b776a855f6a +16-18 s: kssssssswssssssssssb \ No newline at end of file -- 2.45.2