File tree 2 files changed +19
-4
lines changed
2 files changed +19
-4
lines changed Original file line number Diff line number Diff line change 1
- data = File . open ( "data.txt" )
1
+ DATA = File . open ( "data.txt" )
2
2
. read ( )
3
3
. split
4
4
5
5
# First part
6
- c = data . map { |row | row . split ( "" ) }
6
+ c = DATA . map { |row | row . split ( "" ) }
7
7
. map { |row | row . uniq . map { |char | row . count ( char ) if [ 2 , 3 ] . include? row . count ( char ) } . compact . uniq }
8
8
. reduce ( [ ] , :concat )
9
9
10
10
puts "First part: " + c . uniq
11
11
. map { |x | c . count ( x ) }
12
- . inject ( :* ) . to_s
12
+ . inject ( :* ) . to_s
13
+
14
+ # Second part
15
+
16
+ def hamming ( p )
17
+ h = p [ 0 ] . split ( "" ) . zip ( p [ 1 ] . split ( "" ) ) . map { |p | p . first ==p . last }
18
+ return h . count ( FALSE ) , h . each_with_index . select { |v , i | v ==FALSE } . map { |p | p [ 1 ] }
19
+ end
20
+
21
+ puts "Second part: " + DATA . combination ( 2 )
22
+ . map { |p | [ p [ 0 ] , hamming ( p ) [ 1 ] ] if hamming ( p ) [ 0 ] ==1 }
23
+ . compact
24
+ . map { |p | p . first . split ( "" ) . each_with_index . select { |v , i | i !=p . last . last } . map { |q | q [ 0 ] } }
25
+ . first
26
+ . join ( "" )
Original file line number Diff line number Diff line change @@ -7,12 +7,13 @@ Just for practice and fun. Thank You [Eric](http://was.tl/)!
7
7
| Day | Name | Python | Ruby |
8
8
| :---: | :----------------------------------- | :------------------------: | :------------------------: |
9
9
| 01 | [ Chronal Calibration] [ day01 ] | [ :heavy_check_mark : ] [ py01 ] | [ :heavy_check_mark : ] [ rb01 ] |
10
- | 02 | [ Inventory Management System] [ day02 ] | [ :heavy_check_mark : ] [ py02 ] | |
10
+ | 02 | [ Inventory Management System] [ day02 ] | [ :heavy_check_mark : ] [ py02 ] | [ : heavy_check_mark : ] [ rb02 ] |
11
11
12
12
[ py01 ] : ./01/solution.py
13
13
[ py02 ] : ./02/solution.py
14
14
15
15
[ rb01 ] : ./01/solution.rb
16
+ [ rb02 ] : ./02/solution.rb
16
17
17
18
[ day01 ] : https://adventofcode.com/2018/day/1
18
19
[ day02 ] : https://adventofcode.com/2018/day/2
You can’t perform that action at this time.
0 commit comments