Sha256: 5ccda371729cb76ad41bbe5bcbe706e62e4ee3aae0b8c2b9e7263e2b7338834e

Contents?: true

Size: 934 Bytes

Versions: 4

Compression:

Stored size: 934 Bytes

Contents

#
# https://adventofcode.com/2018/day/4
#
# Does not use any cem functions \o/

Guard = Struct.new("Guard", :id, :total, :minutes)

guards = {}
guard = nil
asleep = nil

File.readlines("inputs/day4_input.txt", chomp: true).sort.each { |l|

  if l =~ /Guard \#(\d+) begins shift$/
  
    id = $1.to_i
    guard = guards[id] ||= Guard.new(id, 0, [0] * 60)
  
  elsif / 00:(?<minute>\d+)\] falls asleep$/ =~ l
  
    asleep = minute.to_i 
  
  elsif l =~ / 00:(\d+)\] wakes up$/
  
    awake = $1.to_i
    
    guard.total += awake - asleep
    (asleep...awake).each { |min|
      guard.minutes[min] += 1
    }
    
  end
}

guard = (guards.values.sort_by {|g| g.total}).last
puts "Part1: #{guard.id * guard.minutes.each_with_index.max[1]}"

guard = (guards.values.sort_by {|g| g.minutes.each_with_index.max[0] }).last
puts "Part2: #{guard.id * guard.minutes.each_with_index.max[1]}"






Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
cem-0.1.7 examples/aoc2018/day4.rb
cem-0.1.6 examples/aoc2018/day4.rb
cem-0.1.5 examples/aoc2018/day4.rb
cem-0.1.4 examples/aoc2018/day4.rb