Skip to content

Commit 6f31700

Browse files
committed
Day 2 - added complete Ruby solution and updated README.md file
1 parent f7f67e1 commit 6f31700

File tree

2 files changed

+19
-4
lines changed

2 files changed

+19
-4
lines changed

02/solution.rb

+17-3
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,26 @@
1-
data = File.open("data.txt")
1+
DATA = File.open("data.txt")
22
.read()
33
.split
44

55
# First part
6-
c = data.map{|row| row.split("")}
6+
c = DATA.map{|row| row.split("")}
77
.map{|row| row.uniq.map{ |char| row.count(char) if [2, 3].include? row.count(char) }.compact.uniq}
88
.reduce([], :concat)
99

1010
puts "First part: " + c.uniq
1111
.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("")

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,13 @@ Just for practice and fun. Thank You [Eric](http://was.tl/)!
77
| Day | Name | Python | Ruby |
88
| :---: | :----------------------------------- | :------------------------: | :------------------------: |
99
| 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] |
1111

1212
[py01]: ./01/solution.py
1313
[py02]: ./02/solution.py
1414

1515
[rb01]: ./01/solution.rb
16+
[rb02]: ./02/solution.rb
1617

1718
[day01]: https://adventofcode.com/2018/day/1
1819
[day02]: https://adventofcode.com/2018/day/2

0 commit comments

Comments
 (0)