From: Greg Burri Date: Thu, 10 Dec 2020 06:51:52 +0000 (+0100) Subject: Day[9] X-Git-Url: http://git.euphorik.ch/?a=commitdiff_plain;h=refs%2Fheads%2Fgh-pages;p=advent_of_code_2020.git Day[9] --- diff --git a/advent_of_code_2020.jl b/advent_of_code_2020.jl index e1f2303..8ac82fa 100644 --- a/advent_of_code_2020.jl +++ b/advent_of_code_2020.jl @@ -607,6 +607,88 @@ md" **Result for part 2**: $(fix_to_not_loop(input_day8)) " +# ╔═╡ ed5f09a0-3a2d-11eb-3592-6bec15fbc19b +md"## Day [9]" + +# ╔═╡ f5750b80-3a2d-11eb-2edc-a767123b4bee +function parse_day9(lines) + parse.(Int64, lines) +end + +# ╔═╡ 2c12b6f0-3a2f-11eb-2aca-b54f0aeb9807 +function first_number_without_valid_pair(numbers, preamble_size) + for i ∈ preamble_size+1:length(numbers) + previous = numbers[i-preamble_size:i-1] + n = numbers[i] + if n ∉ [a + b for a ∈ previous for b ∈ previous if a ≠ b] + return n + end + end +end + +# ╔═╡ 0218f5c0-3a35-11eb-3833-a7472bcbc11b +function find_encryption_weakness(numbers, target) + l = length(numbers) + for i ∈ 1:l + for j ∈ i+1:l + ξ = numbers[i:j] + s = sum(ξ) + if s == target + return maximum(ξ) + minimum(ξ) + elseif s > target + break + end + end + end +end + +# ╔═╡ 2c96db10-3a2f-11eb-266d-3db1dfe51f8c +let + input = "35 +20 +15 +25 +47 +40 +62 +55 +65 +95 +102 +117 +150 +182 +127 +219 +299 +277 +309 +576" + numbers = parse_day9(split(input, '\n')) + + result_part1 = first_number_without_valid_pair(numbers, 5) + result_part2 = find_encryption_weakness(numbers, result_part1) + + md" +Test 1, part 1: $(result_part1 == 127) + +Test 1, part 2: $(result_part2 == 62) + " +end + +# ╔═╡ 299e6950-3a34-11eb-1080-6d172718b1ba +input_day9 = parse_day9(readlines("data/day09.txt")) + +# ╔═╡ 484f9ea0-3a34-11eb-28a2-a95d5444ad50 +let + target = first_number_without_valid_pair(input_day9, 25) + md" +**Result for part 1**: $target + +**Result for part 2**: $(find_encryption_weakness(input_day9, target)) +" +end + # ╔═╡ Cell order: # ╟─661be9a0-353b-11eb-3598-a5b5245368cb # ╟─f0dd4400-3313-11eb-3295-af913c2212fb @@ -665,3 +747,10 @@ md" # ╟─d6afedee-3a0a-11eb-31d7-3f16633dbf5d # ╟─aa322be0-3a0e-11eb-27ec-d13acf9084aa # ╟─d867dd80-3a12-11eb-1ae7-3119a294b25a +# ╟─ed5f09a0-3a2d-11eb-3592-6bec15fbc19b +# ╠═f5750b80-3a2d-11eb-2edc-a767123b4bee +# ╠═2c12b6f0-3a2f-11eb-2aca-b54f0aeb9807 +# ╠═0218f5c0-3a35-11eb-3833-a7472bcbc11b +# ╟─2c96db10-3a2f-11eb-266d-3db1dfe51f8c +# ╟─299e6950-3a34-11eb-1080-6d172718b1ba +# ╟─484f9ea0-3a34-11eb-28a2-a95d5444ad50 diff --git a/advent_of_code_2020.jl.html b/advent_of_code_2020.jl.html index 1bb5451..83a5677 100644 --- a/advent_of_code_2020.jl.html +++ b/advent_of_code_2020.jl.html @@ -149,12 +149,12 @@ foreignObject[data-mjx-xml] { -
-
9.5 Î¼s

🦌 Advent Of Code 2020

+
2.7 ms

🦌 Advent Of Code 2020

The goals are:

  • Learn Julia

    @@ -165,18 +165,18 @@ main {

See Advent Of Code 2020 for more information.

-
12.7 Î¼s

Day 1

-
8.8 Î¼s
input_day1
39.2 ms
day1_part1 (generic function with 1 method)
36.2 Î¼s
day1_part2 (generic function with 1 method)
42.9 Î¼s

Result for part 1: 913824

+
3.4 ms

Day 1

+
6.3 Î¼s
input_day1
69.6 ms
day1_part1 (generic function with 1 method)
29.5 Î¼s
day1_part2 (generic function with 1 method)
45.7 Î¼s

Result for part 1: 913824

Result for part 2: 240889536

-
30.0 Î¼s

Day 2

-
10.3 Î¼s
520 Î¼s
nb_of_valid_passwords_part2 (generic function with 1 method)
54.3 Î¼s
parse_day2 (generic function with 1 method)
51.0 Î¼s
nb_of_valid_passwords_part1 (generic function with 1 method)
39.7 Î¼s

Test 1, part 1: true

+
60.1 ms

Day 2

+
4.2 Î¼s
495 Î¼s
nb_of_valid_passwords_part2 (generic function with 1 method)
40.2 Î¼s
parse_day2 (generic function with 1 method)
47.3 Î¼s
nb_of_valid_passwords_part1 (generic function with 1 method)
33.9 Î¼s

Test 1, part 1: true

Test 1, part 2: true

-
187 ms
input_day2
11.0 ms

Result for part 1: 600

-
117 Î¼s

Result for part 2: 245

+
116 ms
input_day2
7.5 ms

Result for part 1: 600

+
157 Î¼s

Result for part 2: 245

130 Î¼s

Day 3

-
13.1 Î¼s
parse_day3 (generic function with 1 method)
29.5 Î¼s
nb_trees_encountered (generic function with 2 methods)
158 Î¼s
nb_trees_encountered_part2 (generic function with 1 method)
25.9 Î¼s

Test 1, part 1: true

+
4.6 Î¼s
parse_day3 (generic function with 1 method)
51.6 Î¼s
nb_trees_encountered (generic function with 2 methods)
127 Î¼s
nb_trees_encountered_part2 (generic function with 1 method)
31.0 Î¼s

Test 1, part 1: true

Test 1, part 2: true

-
463 ms
input_day3
323×31 LinearAlgebra.Adjoint{Bool,Array{Bool,2}}:
+
421 ms
input_day3
323×31 LinearAlgebra.Adjoint{Bool,Array{Bool,2}}:
  0  0  1  0  0  0  0  0  0  1  1  1  0  0  …  0  0  1  1  0  0  1  0  1  0  0  0  0
  0  1  0  1  0  0  0  0  0  1  0  1  1  0     1  1  1  0  0  0  1  1  0  0  0  1  1
  0  0  1  0  1  0  0  1  0  0  0  0  0  0     0  1  0  1  0  0  1  0  0  0  0  0  0
@@ -190,29 +190,29 @@ main {
  0  0  0  0  0  0  0  0  1  0  1  1  1  1     0  0  0  0  0  1  0  1  0  1  0  0  0
  1  0  0  0  0  0  0  0  0  0  0  0  1  1  …  0  0  0  0  0  0  0  0  0  1  0  0  1
  0  0  0  0  0  0  0  0  0  0  0  1  0  0     0  0  0  1  0  0  0  1  0  1  0  0  0
- 0  0  0  0  1  1  0  0  0  1  1  0  0  0     0  0  0  0  1  0  0  0  0  0  0  0  0
62.6 ms

Result for part 1: 262

+ 0 0 0 0 1 1 0 0 0 1 1 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0
113 ms

Result for part 1: 262

Result for part 2: 2698900776

-
12.8 ms

Day 4

-
11.7 Î¼s
parse_day4 (generic function with 1 method)
51.2 Î¼s
count_nb_valid_passport (generic function with 2 methods)
169 Î¼s

Test 1, part 1: true

+
10.5 ms

Day 4

+
4.9 Î¼s
parse_day4 (generic function with 1 method)
45.5 Î¼s
count_nb_valid_passport (generic function with 2 methods)
191 Î¼s

Test 1, part 1: true

Test 1, part 2: true

Test 2, part 2: true

-
187 ms
input_day4
10.8 ms

Result for part 1: 264

+
140 ms
input_day4
9.1 ms

Result for part 1: 264

Result for part 2: 224

-
902 Î¼s

Day 5

-
12.2 Î¼s
parse_day5 (generic function with 1 method)
103 Î¼s

Test 1, part 1: true

-
217 ms
find_missing_id (generic function with 1 method)
46.4 Î¼s
input_day5
13.4 ms

Result for part 1: 850

+
1.3 ms

Day 5

+
4.5 Î¼s
parse_day5 (generic function with 1 method)
52.3 Î¼s

Test 1, part 1: true

+
147 ms
find_missing_id (generic function with 1 method)
35.2 Î¼s
input_day5
9.3 ms

Result for part 1: 850

Result for part 2: 599

-
32.8 ms

Day 6

-
16.8 Î¼s
parse_day6 (generic function with 1 method)
45.4 Î¼s
sum_of_group_lengths (generic function with 1 method)
48.1 Î¼s

Test 1, part 1: true

+
22.8 ms

Day 6

+
3.4 Î¼s
parse_day6 (generic function with 1 method)
39.4 Î¼s
sum_of_group_lengths (generic function with 1 method)
45.1 Î¼s

Test 1, part 1: true

Test 1, part 2: true

-
792 ms

Number of groups read from data/day06.txt: 490

-
1.1 ms

Result for part 1: 6735

+
631 ms

Number of groups read from data/day06.txt: 490

+
37.4 ms

Result for part 1: 6735

Result for part 2: 3221

-
2.5 ms

Day 7

-
14.6 Î¼s
847 Î¼s
parse_day7 (generic function with 1 method)
94.5 Î¼s
bags_that_contain (generic function with 1 method)
52.1 Î¼s
nb_bags_contained (generic function with 1 method)
75.8 Î¼s

Test 1, part 1: true

+
2.0 ms

Day 7

+
6.3 Î¼s
607 Î¼s
parse_day7 (generic function with 1 method)
69.9 Î¼s
bags_that_contain (generic function with 1 method)
42.4 Î¼s
nb_bags_contained (generic function with 1 method)
59.2 Î¼s

Test 1, part 1: true

Test 1, part 2: true

Test 2, part 2: true

-
127 Î¼s
input_day7
385 msinput_day7
3.4 ms

Result for part 1: 335

+ 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0names
17.3 ms

Result for part 1: 335

Result for part 2: 2431

-
2.2 ms

Day 8

-
30.1 Î¼s
1.0 ms
parse_day8 (generic function with 1 method)
33.7 Î¼s
execute (generic function with 1 method)
43.4 Î¼s
fix_to_not_loop (generic function with 1 method)
67.1 Î¼s

Test 1, part 1: true

+
1.6 ms

Day 8

+
5.4 Î¼s
623 Î¼s
parse_day8 (generic function with 1 method)
32.3 Î¼s
execute (generic function with 1 method)
40.5 Î¼s
fix_to_not_loop (generic function with 1 method)
56.4 Î¼s

Test 1, part 1: true

Test 1, part 2: true

-
43.0 ms
input_day8
497 Î¼s

Result for part 1: (false, 1928)

+
90.3 ms
input_day8
8.3 ms

Result for part 1: (false, 1928)

Result for part 2: 1319

-
1.8 ms
+1.1 ms

Day [9]

+
5.2 Î¼s
parse_day9 (generic function with 1 method)
25.9 Î¼s
first_number_without_valid_pair (generic function with 1 method)
79.7 Î¼s
find_encryption_weakness (generic function with 1 method)
38.0 Î¼s

Test 1, part 1: true

+

Test 1, part 2: true

+
40.7 Î¼s
input_day9
9.9 ms

Result for part 1: 31161678

+

Result for part 2: 5453868

+
27.6 ms
diff --git a/data/day09.txt b/data/day09.txt new file mode 100644 index 0000000..100df13 --- /dev/null +++ b/data/day09.txt @@ -0,0 +1,1000 @@ +10 +21 +30 +41 +7 +31 +43 +13 +18 +16 +8 +27 +48 +49 +19 +46 +50 +37 +9 +26 +42 +15 +23 +47 +14 +17 +20 +29 +21 +92 +22 +31 +76 +24 +25 +28 +30 +73 +41 +33 +32 +34 +45 +38 +35 +36 +37 +70 +39 +50 +42 +51 +43 +46 +47 +49 +61 +52 +58 +53 +63 +102 +65 +66 +67 +68 +87 +71 +88 +73 +75 +76 +95 +85 +140 +89 +90 +115 +96 +100 +101 +118 +128 +116 +131 +129 +132 +246 +208 +135 +139 +165 +164 +148 +161 +151 +166 +179 +181 +205 +289 +186 +196 +277 +201 +217 +268 +290 +247 +316 +261 +328 +312 +286 +449 +287 +327 +299 +309 +317 +403 +347 +367 +377 +382 +387 +397 +413 +615 +749 +529 +508 +533 +534 +547 +843 +573 +664 +616 +586 +596 +608 +714 +676 +1094 +724 +1222 +915 +759 +1193 +784 +1357 +1145 +1459 +1080 +1318 +1142 +1067 +1081 +1120 +1159 +1169 +1182 +1194 +1204 +1272 +1284 +1460 +1400 +1508 +1869 +1543 +1674 +1826 +2454 +1851 +2324 +2222 +2236 +2147 +2262 +2148 +2868 +4087 +2289 +2351 +3656 +3636 +2488 +3226 +2556 +3821 +2908 +3751 +5104 +3217 +3910 +3500 +3677 +3998 +4175 +4369 +7498 +4383 +7407 +6544 +4704 +4640 +5396 +4907 +7136 +5044 +5464 +7128 +5773 +9547 +9217 +6408 +6717 +6894 +7127 +7177 +9219 +8544 +8173 +8879 +9413 +9779 +9023 +13184 +11776 +11881 +10104 +10440 +9951 +10508 +10817 +15301 +12181 +17567 +13302 +13585 +13125 +13611 +14021 +18903 +22478 +16717 +17196 +17052 +18292 +19127 +22132 +18974 +20391 +20459 +24402 +34204 +20948 +27869 +22998 +23942 +25306 +25483 +26427 +32488 +26710 +32738 +27632 +39195 +35009 +44719 +33769 +44280 +43376 +39586 +38101 +39922 +40850 +41339 +41407 +43946 +44890 +46431 +46940 +48304 +49248 +79678 +51910 +66827 +54342 +92250 +107677 +82991 +123841 +126367 +77715 +73355 +126109 +86297 +77687 +89643 +80772 +82189 +121017 +96188 +88836 +93194 +203824 +95244 +97552 +118737 +106252 +121169 +137333 +127697 +154127 +151042 +151070 +155402 +155544 +158459 +159652 +159876 +310946 +162961 +169608 +171025 +175383 +184080 +255840 +186388 +248622 +192796 +201496 +203804 +246434 +299048 +609994 +265030 +278739 +318363 +326425 +306472 +313861 +438615 +343732 +319528 +322837 +332569 +346408 +622471 +355105 +421817 +370468 +379184 +405300 +582988 +716876 +447930 +450238 +668251 +543769 +571502 +578891 +585211 +620333 +793970 +629309 +633389 +1033141 +642365 +652097 +655406 +678977 +701513 +776922 +938093 +1084277 +749652 +855538 +853230 +1662450 +1473563 +1068263 +1205544 +1115271 +2206219 +1221256 +1212280 +1297771 +2600543 +1262698 +1532207 +1275754 +1432328 +1294462 +2199548 +2708082 +1380490 +2768025 +1526574 +1602882 +4241588 +1605190 +2865580 +4806762 +2183534 +2273807 +3878997 +2320815 +2336527 +4473355 +2592233 +3886695 +2538452 +2557160 +4859267 +2807961 +6076237 +3654297 +2674952 +4373215 +2907064 +3131764 +3129456 +3208072 +7540992 +4874979 +7682940 +5990824 +4457341 +4504349 +4594622 +4657342 +4928760 +4893687 +6965448 +5095612 +5213404 +7485920 +5232112 +5582016 +5482913 +5804408 +5806716 +7802694 +6036520 +8440184 +6261220 +6337528 +7865414 +10024372 +8961690 +9114683 +15606388 +9051963 +9523382 +9251964 +13333871 +10107091 +9989299 +10309016 +10327724 +10445516 +11287321 +19595091 +24046572 +15006295 +12141936 +11843236 +14063914 +12297740 +12598748 +14126634 +14202942 +16827104 +18013653 +18638065 +18166646 +20317023 +19697480 +19579688 +19241263 +22288752 +20096390 +20434815 +23429257 +35333893 +37493171 +31030046 +32701979 +23985172 +24140976 +30612401 +33305177 +24896488 +26424374 +32216595 +28329576 +32369588 +38448468 +36180299 +36804711 +39793870 +38820951 +38938743 +39676078 +39337653 +31161678 +40531205 +51047216 +63732025 +48126148 +48881660 +49037464 +50409546 +50565350 +51320862 +53226064 +71308331 +64690358 +62604673 +71037546 +59491254 +142345877 +67341977 +67966389 +87214257 +69982629 +70837756 +70100421 +115468125 +70499331 +140481960 +91096555 +114141571 +97919124 +97007808 +99447010 +99602814 +100974896 +132656747 +104546926 +123326485 +122095927 +124181612 +133642219 +194680943 +264663572 +211319716 +135308366 +137949018 +140083050 +140599752 +167845564 +161196976 +221542937 +300502311 +188104363 +220334293 +196454818 +203993936 +355877919 +199049824 +405049237 +228728538 +226642853 +227873411 +262130630 +345724549 +366725903 +268950585 +273257384 +446977146 +349268734 +305794582 +278548770 +395718975 +308445316 +329042540 +423327606 +384559181 +387154187 +392098299 +400448754 +395504642 +651201017 +425692677 +426923235 +454516264 +455371391 +570575946 +490004041 +531081215 +597993125 +542207969 +547499355 +700543615 +754735217 +627817504 +584343352 +637487856 +755965775 +693004497 +882102340 +1179293381 +771713368 +817790976 +787602941 +854965018 +821197319 +852615912 +880208941 +881439499 +909887655 +945375432 +1427708296 +1131842707 +1240503852 +1393453631 +1089707324 +1636837557 +2264655061 +2518939897 +1212160856 +1221831208 +1330492353 +1448970272 +1944672342 +1971809664 +2093600355 +1697999917 +1670406888 +3166503550 +1673813231 +2372346559 +1732824853 +1761648440 +2035082756 +2150391507 +2077218139 +2221550031 +3365263295 +2301868180 +2311538532 +4236464725 +3796731196 +4554022653 +2552323561 +2661131128 +2892238096 +2779462625 +3119377160 +3344220119 +3371813148 +3368406805 +4738349267 +3494473293 +3406638084 +4046159790 +3767907609 +3810042992 +3838866579 +6938397886 +4227609646 +4298768170 +4523418211 +4613406712 +6005351247 +6862880098 +6576193821 +5213454689 +6491190308 +7184549339 +5440593753 +5671700721 +5898839785 +11314543088 +6712626924 +7599422794 +12496541555 +6901111377 +9668203399 +10622353611 +7577950601 +10415060400 +7648909571 +17198547432 +8526377816 +17019959766 +8822186381 +9136824923 +10054000465 +14577418676 +13477305198 +13476790386 +10885155410 +26156784689 +12341705130 +12153220677 +21300215810 +12611466709 +13613738301 +14312049718 +14479061978 +17663202739 +15427489193 +30675852630 +22568281077 +16400136982 +16175287387 +16471095952 +39039377029 +22140116117 +22299491579 +17959011304 +21290045600 +20939155875 +23038376087 +25197205128 +27090528687 +23226860540 +24494925807 +25955443431 +24764687386 +32871232934 +26225205010 +27925788019 +32438073282 +29906551171 +31602776580 +38743568464 +32575424369 +32646383339 +68945928200 +34134298691 +34430107256 +46904803503 +43238647454 +46054732986 +59682724339 +57843588467 +43977531962 +48424065668 +57832339190 +50720130817 +50989892396 +80896443567 +66669356483 +107121648577 +54150993029 +60363861301 +59528564599 +61509327751 +62481975540 +85753769609 +71318992833 +80484840242 +66780682030 +68564405947 +77668754710 +93958671855 +99413958064 +105583297585 +102575058697 +92401597630 +139904762638 +94697662779 +108822231586 +101710023213 +204285081910 +105140885425 +114514854330 +137988349316 +113679557628 +115660320780 +121873189052 +121037892350 +201522732592 +342273431226 +171139464644 +170274429160 +135345087977 +144449436740 +237533509832 +170070352340 +197984895215 +325322974260 +215735555129 +187099260409 +194111620843 +239147099519 +223337085916 +206850908638 +215389580841 +218820443053 +249024645605 +229339878408 +305415440317 +341413893804 +236698213130 +322444348386 +422586463767 +279794524717 +305619517137 +358238725053 +388890795393 +383596536259 +314519789080 +417448706759 +357169612749 +381210881252 +393950169047 +764807417511 +423451499251 +400962529481 +655933682884 +746060408142 +422240489479 +534959395545 +533340232133 +478364524013 +466038091538 +516492737847 +542317730267 +1168300897621 +585414041854 +594314313797 +715408337802 +772487331652 +671689401829 +1134951203535 +695730670332 +715482318561 +1008355821805 +738380494001 +823203018960 +794912698528 +1262553145989 +824414028732 +1533293192529 +1150053925842 +994857261860 +888278581017 +1161768761870 +1332694807798 +1060352405335 +982530829385 +1058810468114 +1127731772121 +1290044984129 +1179728355651 +1266003715626 +1367420072161 +1387171720390 +1410069895830 +1411212988893 +2132584755227 +1538685337521 +1733237755861 +2113248003089 +1618115717488 +2191834100893 +3294353517097 +2041341297499 +1870809410402 +2188084177456 +1883135842877 +2298348476847 +2537801667951 +3271923093382 +2238538823765 +3500004827388 +2186542240235 +5005160849243 +3307345013125 +2445732071277 +2797241616220 +2754591792551 +2798384709283 +3726769514977 +2949898326414 +3156801055009 +3846485758950 +3351353473349 +3488925127890 +3501251560365 +3753945253279 +3912150707901 +4069678083112 +4057351650637 +4071220020333 +4121674666642 +4425081064000 +4632274311512 +4684270895042 +4941134032786 +7473028139991 +4983783856455 +5704490118965 +5753077084402 +5552976501834 +6106699381423 +7482655604325 +5748283035697 +9595624509313 +6301251799763 +9665227792303 +8546755730642 +6840278601239 +13177518258956 +11781412634025 +7666095961180 +7969502358538 +8701952394624 +10645624151751 +8496301084333 +8753948978154 +10736860940857 +9316545206554 +22518273574882 +9924917889241 +20328168364667 +15148601011147 +11301259537531 +11306053586236 +11659675883257 +11854982417120 +19233162025190 +20535361612179 +26808276894404 +15542230995863 +19241463095795 +14506374562419 +15635598319718 +16162397045513 +16723451336692 +35562225872558 +16671454753162 +17198253478957 +24131899404051 +27490580736838 +18070494184708 +27463656583044 +20617804744085 +21226177426772 +21230971475477 +22607313123767 +23156241954651 +22960935420788 +22965729469493 +23514658300377 +28526437170282 +52041095470659 +37393368520990 +30048605558282 +31229825899111 +55361725303162 +30141972882137 +31797995365231 +32833851798675 +33394906089854 +33869708232119 +51274782985054 +38688298928793 +43833490550539 +39296671611480 +44191906896265 +41843982170857 +44382419381423 +42457148902249 +43838284599244 +46670900255028 +64624731988965 +90729394399452 +66703560030794 +53563263858659 +68736904487075 +60190578440419 +61278431457393 +61371798781248 +61939968247368 +62975824680812 +64011681114256 +124124300489306 +66228757888529 +67264614321973 +77984970540273 +89963081913847 +81140653782337 +134921301295717 +81753820513729 +89128049157277 +110682581369284 +86295433501493 +100234164113687 +115503232106027 +108042699036276 +182767846428000