From 8eb7a0715b9da3b5e141493f81b22725e50d92ec Mon Sep 17 00:00:00 2001 From: Greg Burri Date: Thu, 10 Dec 2020 07:51:52 +0100 Subject: [PATCH] Day[9] --- advent_of_code_2020.jl | 89 +++++++++++++++++++++++++++++++++++++ advent_of_code_2020.jl.html | 63 ++++++++++++++------------ 2 files changed, 123 insertions(+), 29 deletions(-) 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
-- 2.45.2