LogoWCF

janka102's Solution

2025

class Day04
  def initialize(filename)
    @filename = filename
  end

  def solve
    total_time = 0

    # Snap, Crackle, Pop, and Steve, one of the better looking elves
    elves_multiplyer = [1.4, 0.7, 1.1, 1]
    working_elves = [0, 0, 0, 0] # how long each elf is still working on a present

    File.open(@filename).each(' ') do |present|
      time = present.to_i
      next_elf = working_elves.find_index(0) # find first non-working elf

      # if they are all working then advance time by the minimum time left for an elf to finish
      if next_elf.nil?
        min_time_left = working_elves.min
        total_time += min_time_left
        working_elves = working_elves.map { |t| t - min_time_left }
        next_elf = working_elves.find_index(0)
      end

      # get that elf workin
      time_needed = (time / elves_multiplyer[next_elf]).ceil + 1
      working_elves[next_elf] = time_needed
    end.close

    max_time_left = working_elves.max
    total_time += max_time_left

    puts "Total Time: #{total_time} minutes"
  end
end

# Expects the input file to be named 04.txt and in the current working directory
if __FILE__ == $PROGRAM_NAME
  challenge = Day04.new('04.txt')
  challenge.solve
end

Day 4: No Time To Waste!

It’s crunch time! Holly (one of the floor managers in manufacturing warehouse 14) is getting pretty stressed about being able to finish the required amount of work that has been allocated to her team. She needs to figure out how much time it is going to take her team to finish this batch of presents so that she can get a portion of this batch reassigned before it is too late.

Holly has queried all of the presents (in order) that are going to be coming down the conveyor belt in the next batch, and output the time it takes for an average elf to assemble and package each present into a file (presents.txt). Her team currently consists of 4 elves: Snap, Crackle, Pop, and Steve. Each of the elves work at different paces. Snap is very speedy and works at 1.4 times the speed of the average elf. Crackle is slower than most, and works at .7 times the speed of the average elf. Pop is fairly normal, working at 1.1 times the speed of the average elf, and Steve works at the average speed of the average elf. He is one of the better looking elves.

When the elves finish a present, if they finish in between minutes they always wait for the next minute on the clock to actually get up to get the next project. It takes the elves one minute to get up and grab the next project off of the conveyer belt, and if more that one elf is getting up to get a present at the same time, they grab the next presents in order (Snap, Crackle, Pop, and then Steve).

Problem: Please figure out how many minutes this batch in presents.txt is going to take!

Note: Yes it’s sad, but elves don’t get breaks. Ever. No need to calculate in breaks or sleep time!