[2022][ruby][3.x,4.x]

pull/1/head
Alpha Chen 1 year ago
parent ccdfedbbb6
commit 74d3bef510
Signed by: alpha
SSH Key Fingerprint: SHA256:3fOT8fiYQG/aK9ntivV3Bqtg8AYQ7q4nV6ZgihOA20g

@ -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]) }

@ -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)) }
Loading…
Cancel
Save