Last active
December 10, 2020 07:03
-
-
Save SleeplessByte/d5347addac5be1abde7e5c8fbe28a05e to your computer and use it in GitHub Desktop.
Advent of Code 2020: Day 10 - Adapter Array
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'benchmark' | |
adapters = File.readlines('input.txt').map(&:to_i).sort | |
maximum_jolt = adapters.last + 3 | |
adapters.push(maximum_jolt) | |
# Track the number of options to get to a target jolt value | |
# and default to 0. The first jolt value is 0, and can only | |
# be reached in one way. | |
options = Hash.new(0) | |
options[0] = 1 | |
=begin | |
# For the example input, here is what is being calculated: | |
target: options => total options | |
1: only 1 option (0 + 1) => 1 | |
4: only 1 option (1 + 3) => 1 | |
5: only 1 option (4 + 1) => 1 | |
6: either 4 + 2 or 5 + 1 => 2 | |
7: 6 + 1 or 5 + 2 or 4 + 3 => 2 (6) + 1 (5) + 1 (4) | |
10: only 1 option (7 + 3) => 4 | |
11: only 1 option (10 + 1) => 4 | |
12: either 10 + 2 or 11 + 1 => 4 (10) + 4 (11) | |
15: only 1 option 12 + 3 => 8 | |
16: only 1 option 15 + 1 => 8 | |
19: only 1 option 16 + 3 => 8 | |
22: only 1 option 19 + 3 => 8 | |
=end | |
Benchmark.bmbm do |x| | |
x.report(:part_1) do | |
jolt_groups = adapters | |
.each_with_index | |
.map do |adapter, index| | |
adapter - (index > 0 ? adapters[index - 1] : 0) | |
end | |
.group_by(&:itself) | |
puts jolt_groups[1].length * jolt_groups[3].length | |
end | |
x.report(:part_2) do | |
adapters.each do |target_jolts| | |
options[target_jolts] = [1, 2, 3] | |
.sum { |difference| options[target_jolts - difference] } | |
end | |
puts options[maximum_jolt] | |
end | |
end | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 | |
4 | |
5 | |
6 | |
7 | |
10 | |
11 | |
12 | |
15 | |
16 | |
19 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 | |
33 | |
18 | |
42 | |
31 | |
14 | |
46 | |
20 | |
48 | |
47 | |
24 | |
23 | |
49 | |
45 | |
19 | |
38 | |
39 | |
11 | |
1 | |
32 | |
25 | |
35 | |
8 | |
17 | |
7 | |
9 | |
4 | |
2 | |
34 | |
10 | |
3 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 | |
111 | |
135 | |
32 | |
150 | |
5 | |
106 | |
154 | |
41 | |
7 | |
27 | |
117 | |
109 | |
63 | |
64 | |
21 | |
138 | |
98 | |
40 | |
71 | |
144 | |
13 | |
66 | |
48 | |
12 | |
55 | |
119 | |
103 | |
54 | |
78 | |
65 | |
112 | |
39 | |
128 | |
53 | |
140 | |
77 | |
34 | |
28 | |
81 | |
151 | |
125 | |
85 | |
124 | |
2 | |
99 | |
131 | |
59 | |
60 | |
6 | |
94 | |
33 | |
42 | |
93 | |
14 | |
141 | |
92 | |
38 | |
104 | |
9 | |
29 | |
100 | |
52 | |
19 | |
147 | |
49 | |
74 | |
70 | |
84 | |
113 | |
120 | |
91 | |
97 | |
17 | |
45 | |
139 | |
90 | |
116 | |
149 | |
129 | |
87 | |
69 | |
20 | |
24 | |
148 | |
18 | |
58 | |
123 | |
76 | |
118 | |
130 | |
132 | |
75 | |
110 | |
105 | |
1 | |
8 | |
86 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment