[2021][ruby][4.x..=5.x]

pull/2/head
Alpha Chen 3 years ago
parent c6e95f860f
commit 367a35f400

@ -0,0 +1,29 @@
input = ARGF.read
numbers, *cards = input.split("\n\n")
numbers = numbers.split(?,).map(&:to_i)
cards = cards.map {|c| c.split("\n").map {|r| r.split(/\s+/).reject(&:empty?).map(&:to_i) }}
def win?(card, drawn)
return true if card.any? {|row| row.all? {|c| drawn.include?(c) }}
return true if card.transpose.any? {|col| col.all? {|c| drawn.include?(c) }}
# return true if (0..4).all? {|x| drawn.include?(card.fetch(x).fetch(x)) }
# return true if (0..4).all? {|x| drawn.include?(card.fetch(x).fetch(4-x)) }
return false
end
(0..numbers.length).each do |i|
drawn = numbers[0..i]
# winning_card = cards.find {|c| win?(c, drawn) }
# if winning_card
# p winning_card.flatten.reject {|x| drawn.include?(x) }.sum * drawn.last
# exit
# end
if cards.length == 1 && winning_card = cards.find {|c| win?(c, drawn) }
p winning_card.flatten.reject {|x| drawn.include?(x) }.sum * drawn.last
exit
else
cards = cards.reject {|c| win?(c, drawn) }
end
end

@ -0,0 +1,29 @@
vents = ARGF.read.split("\n").map {|l| l.split(" -> ").map {|x| x.split(?,).map(&:to_i) }}
straights = vents.select {|(x1,y1),(x2,y2)| x1 == x2 || y1 == y2 }
points = Hash.new(0)
straights.each do |(x1,y1),(x2,y2)|
x = [x1, x2].minmax
y = [y1, y2].minmax
(x[0]..x[1]).each do |x|
(y[0]..y[1]).each do |y|
points[[x,y]] += 1
end
end
end
diagonals = vents - straights
diagonals.each do |(x1,y1),(x2,y2)|
if x1 > x2
x1,x2 = x2,x1
y1,y2 = y2,y1
end
sign = (y1 < y2) ? 1 : -1
(x1..x2).each.with_index do |x,i|
delta = i * sign
points[[x,y1+delta]] += 1
end
end
p points.count {|_,v| v > 1 }
Loading…
Cancel
Save