Skip to content

Commit c164f3c

Browse files
committed
day 10
1 parent 3765afd commit c164f3c

File tree

4 files changed

+177
-1
lines changed

4 files changed

+177
-1
lines changed

2020/day10/data.txt

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
44
2+
41
3+
48
4+
17
5+
35
6+
146
7+
73
8+
3
9+
16
10+
159
11+
11
12+
29
13+
32
14+
63
15+
65
16+
62
17+
126
18+
151
19+
6
20+
124
21+
87
22+
115
23+
122
24+
43
25+
12
26+
85
27+
2
28+
98
29+
59
30+
156
31+
149
32+
66
33+
10
34+
82
35+
26
36+
79
37+
56
38+
22
39+
74
40+
49
41+
25
42+
69
43+
54
44+
19
45+
108
46+
18
47+
55
48+
131
49+
140
50+
15
51+
125
52+
37
53+
129
54+
91
55+
51
56+
158
57+
117
58+
136
59+
142
60+
109
61+
64
62+
36
63+
160
64+
150
65+
42
66+
118
67+
101
68+
78
69+
28
70+
105
71+
110
72+
40
73+
157
74+
70
75+
97
76+
139
77+
152
78+
47
79+
104
80+
81
81+
27
82+
116
83+
132
84+
143
85+
1
86+
80
87+
75
88+
141
89+
133
90+
9
91+
50
92+
153
93+
123
94+
111
95+
119
96+
130
97+
112
98+
94
99+
90
100+
86

2020/day10/data_test.txt

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
28
2+
33
3+
18
4+
42
5+
31
6+
14
7+
46
8+
20
9+
48
10+
47
11+
24
12+
23
13+
49
14+
45
15+
19
16+
38
17+
39
18+
11
19+
1
20+
32
21+
25
22+
35
23+
8
24+
17
25+
7
26+
9
27+
4
28+
2
29+
34
30+
10
31+
3

2020/day10/main.rb

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#!/usr/bin/ruby
2+
require 'test/unit'
3+
require 'pry'
4+
5+
def resolve_1(arr)
6+
diff = arr.push(0).sort.each_cons(2).map { |e| e.inject(&:-) }
7+
diff.count { |v| v == -1 } * (diff.count { |v| v == -3 } + 1)
8+
end
9+
10+
def resolve_2(arr)
11+
arr.push(0).sort.each_with_object([1]) do |e, lengthes|
12+
lengthes[e] = lengthes[([0, e-3].max)..([0, e-1].max)].compact.sum
13+
end.last
14+
end
15+
16+
17+
class Day10Test < Test::Unit::TestCase
18+
def data(name)
19+
File.read(name).split("\n").map(&:to_i)
20+
end
21+
22+
def arr_test
23+
data('data_test.txt')
24+
end
25+
26+
def arr
27+
data('data.txt')
28+
end
29+
30+
def test_1
31+
assert_equal 220, resolve_1(arr_test)
32+
end
33+
34+
def test_1_final
35+
assert_equal 2170, resolve_1(arr)
36+
end
37+
38+
def test_2
39+
assert_equal 19208, resolve_2(arr_test)
40+
end
41+
42+
def test_2_final
43+
assert_equal 24803586664192, resolve_2(arr)
44+
end
45+
end

2020/day9/main.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ def resolve_2(arr, target)
3333
end
3434

3535

36-
class Day8Test < Test::Unit::TestCase
36+
class Day9Test < Test::Unit::TestCase
3737
def data(name)
3838
File.read(name).split("\n").map(&:to_i)
3939
end

0 commit comments

Comments
 (0)