File tree Expand file tree Collapse file tree 1 file changed +15
-13
lines changed
Filter options
Expand file tree Collapse file tree 1 file changed +15
-13
lines changed
Original file line number Diff line number Diff line change 1
1
#!/usr/bin/env ruby
2
- arr_1 = File . readlines ( "data_1.txt" )
3
- arr_2 = File . readlines ( "data_2.txt" )
2
+ # frozen_string_literal: true
4
3
5
- NUMBERS = %w[ one two three four five six seven eight nine ]
4
+ arr1 = File . readlines ( 'data_1.txt' )
5
+ arr2 = File . readlines ( 'data_2.txt' )
6
+
7
+ NUMBERS = %w[ one two three four five six seven eight nine ] . freeze
6
8
7
9
def string_to_number ( str )
8
- index = NUMBERS . find_index ( str )
9
- index ? index + 1 : str . to_i
10
+ index = NUMBERS . find_index ( str )
11
+ index ? index + 1 : str . to_i
10
12
end
11
13
12
- def first_and_last arr , regexp = /\d /
13
- arr . map do
14
- first = _1 [ /#{ regexp } / ]
15
- last = _1 [ /.*(#{ regexp } ).*/ , 1 ]
16
- string_to_number ( first ) * 10 + string_to_number ( last )
17
- end
14
+ def first_and_last ( arr , regexp = /\d / )
15
+ arr . map do
16
+ first = _1 [ /#{ regexp } / ]
17
+ last = _1 [ /.*(#{ regexp } ).*/ , 1 ]
18
+ string_to_number ( first ) * 10 + string_to_number ( last )
19
+ end
18
20
end
19
21
20
- puts first_and_last ( arr_1 ) . sum
21
- puts first_and_last ( arr_2 , /\d |#{ NUMBERS . join ( '|' ) } / ) . sum
22
+ puts first_and_last ( arr1 ) . sum
23
+ puts first_and_last ( arr2 , /\d |#{ NUMBERS . join ( '|' ) } / ) . sum
You can’t perform that action at this time.
0 commit comments