From 53ce2c689809df0efc8897f9e404ca9495d57a72 Mon Sep 17 00:00:00 2001 From: Alpha Chen Date: Tue, 4 Dec 2018 07:07:15 -0800 Subject: [PATCH] [2018][ruby][4.x] golfing --- 2018/ruby/day_04.rb | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/2018/ruby/day_04.rb b/2018/ruby/day_04.rb index 36c3732..61d945c 100644 --- a/2018/ruby/day_04.rb +++ b/2018/ruby/day_04.rb @@ -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