[2018][ruby][4.x] golfing

sorbet
Alpha Chen 6 years ago
parent 6e8f39dbd2
commit 53ce2c6898

@ -11,19 +11,19 @@ records.each do |record|
elsif record =~ /\[\d{4}-\d{2}-\d{2} \d{2}:(\d{2})\] falls asleep/ elsif record =~ /\[\d{4}-\d{2}-\d{2} \d{2}:(\d{2})\] falls asleep/
fell_asleep_at = $1.to_i fell_asleep_at = $1.to_i
elsif record =~ /\[\d{4}-\d{2}-\d{2} \d{2}:(\d{2})\] wakes up/ elsif record =~ /\[\d{4}-\d{2}-\d{2} \d{2}:(\d{2})\] wakes up/
stats[guard] << (fell_asleep_at...$1.to_i) stats[guard].concat((fell_asleep_at...$1.to_i).to_a)
else else
p record p record
end end
end end
stats.transform_values! {|v| v.group_by {|x| x }.transform_values(&:count) }
# Part One # Part One
guard, ranges = stats.max_by {|_,v| v.map(&:size).sum } guard, minutes = stats.max_by {|_,v| v.values.sum }
minute = ranges.flat_map(&:to_a).group_by {|x| x }.transform_values(&:count).max_by(&:last).first minute = minutes.max_by(&:last).first
p guard * minute p guard * minute
# Part Two # Part Two
guard, (minute, _) = stats.map {|k,v| guard, (minute, _) = stats.map {|k,v| [k, v.max_by(&:last)] }.max_by {|_,(_,v)| v }
[k, v.flat_map(&:to_a).group_by {|x| x }.transform_values(&:count).max_by(&:last)]
}.max_by {|_,(_,v)| v }
p guard * minute p guard * minute

Loading…
Cancel
Save