aoc2023/day_4/impl_2.rb

30 lines
652 B
Ruby
Raw Permalink Normal View History

2023-12-09 12:27:35 +01:00
#prepare the input which is a string containing new lines
def parse(input)
data = []
input.each_line do |line|
winners, draw = line.split(":").last.split("|")
winners =winners.split(" ").map {|entry| entry.strip.to_i }
draw = draw.split(" ").map {|entry| entry.strip.to_i }
data << [winners,draw]
end
data
end
# result should a single string or integer
def calculate(data)
cards = []
data.length.times.each do |i|
cards << 1
end
data.each_with_index do |d,i|
winners, draw = d
matches = (winners & draw).length
matches.times do |j|
cards[i+j+1] += cards[i]
end
end
cards.reduce(&:+)
end