[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/
fell_asleep_at = $1.to_i
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
p record
end
end
stats.transform_values! {|v| v.group_by {|x| x }.transform_values(&:count) }
# Part One
guard, ranges = stats.max_by {|_,v| v.map(&:size).sum }
minute = ranges.flat_map(&:to_a).group_by {|x| x }.transform_values(&:count).max_by(&:last).first
guard, minutes = stats.max_by {|_,v| v.values.sum }
minute = minutes.max_by(&:last).first
p guard * minute
# Part Two
guard, (minute, _) = stats.map {|k,v|
[k, v.flat_map(&:to_a).group_by {|x| x }.transform_values(&:count).max_by(&:last)]
}.max_by {|_,(_,v)| v }
guard, (minute, _) = stats.map {|k,v| [k, v.max_by(&:last)] }.max_by {|_,(_,v)| v }
p guard * minute

Loading…
Cancel
Save