[2022][ruby][3.x,4.x]
change topkvsxpolnqvzsnkyuswvzvooqpsszr
commit 7fe3906c36813d37260d6e46010ccbe71b333650
author Alpha Chen <alpha@kejadlen.dev>
date
parent vsovovrw
diff --git a/2022/ruby/day_03.rb b/2022/ruby/day_03.rb
index bea94f1..26335f1 100644
--- a/2022/ruby/day_03.rb
+++ b/2022/ruby/day_03.rb
@@ -1,13 +1,8 @@
-priorities = ((?a..?z).to_a + (?A..?Z).to_a).map.with_index { [_1, _2+1]  }.to_h
+priorities = (?a..?z).chain(?A..?Z).each.with_index.to_h { [_1, _2+1] }
+input = ARGF.read.lines(chomp: true).map(&:chars)
 
 # part 1
-# p ARGF.read.lines(chomp: true).map {|line|
-#   len = line.length
-#   a = line[0...len/2]
-#   b = line[len/2..]
-#   priorities.fetch((a.chars & b.chars)[0])
-# }.sum
+p input.sum { priorities.fetch(_1.each_slice(_1.length/2).inject(&:&)[0]) }
 
-p ARGF.read.lines(chomp: true).each_slice(3).map {|chunk|
-  priorities.fetch(chunk.map(&:chars).inject(&:&)[0])
-}.sum
+# part 2
+p input.each_slice(3).sum { priorities.fetch(_1.inject(&:&)[0]) }
diff --git a/2022/ruby/day_04.rb b/2022/ruby/day_04.rb
new file mode 100644
index 0000000..53138c2
--- /dev/null
+++ b/2022/ruby/day_04.rb
@@ -0,0 +1,6 @@
+p ARGF.read.lines(chomp: true)
+  .map { _1.split(?,) }
+  .map {|x| x.map { _1.split(?-).map(&:to_i) }}
+  .map {|(a,b),(x,y)| [(a..b), (x..y)] }
+  # .count {|a,b| (a.cover?(b.begin) && a.cover?(b.end)) || (b.cover?(a.begin) && b.cover?(a.end)) }
+  .count {|a,b| (a.cover?(b.begin) || a.cover?(b.end)) || (b.cover?(a.begin) || b.cover?(a.end)) }