aoc2023/day_4/impl.rb

29 lines
611 B
Ruby
Raw 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)
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