aoc2023/day_4/impl.rb

29 lines
611 B
Ruby

#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)
result = 0
data.each do |d|
win = 0
winners, draw = d
matches = (winners & draw).length
win = if matches == 0
0
else
2**(matches-1)
end
result += win
end
result
end