Skip to content

Instantly share code, notes, and snippets.

@dnovais
Created March 3, 2022 03:06
Show Gist options
  • Save dnovais/f2c49b95e91a22a988441ebbc0704052 to your computer and use it in GitHub Desktop.
Save dnovais/f2c49b95e91a22a988441ebbc0704052 to your computer and use it in GitHub Desktop.

Matchstick factory Challenge

A matchstick factory has to put n matchsticks in boxes. The factory has the following three types of boxes:

A big box holds 50 matchsticks A medium box holds 20 matchsticks A small box holds five 5 matchsticks The boxes can’t have fewer matchsticks than they can hold; they must be full.

Implement the boxes/1 function in the MatchstickFactory module. The boxes/1 function returns the number of boxes of each size needed to accommodate all the matchsticks and the number of remaining matchsticks as a map (The map is included in the started code). It should work like this:

MatchStickFactory.boxes(98)
#Output -> %{big: 1, medium: 2, remaining_matchsticks: 3, small: 1}

MatchStickFactory.boxes(39)
#Output -> %{big: 0, medium: 1, remaining_matchsticks: 4, small: 3}

Solution

defmodule MatchStickFactory do
  @big_boxes_quantity 50
  @medium_boxes_quantity 20
  @small_boxes_quantity 5

  def boxes(quantity) do
    big_boxes = quantity / @big_boxes_quantity |> trunc()
    remaining = rem(quantity, @big_boxes_quantity)

    medium_boxes = remaining / @medium_boxes_quantity |> trunc()
    remaining = rem(remaining, @medium_boxes_quantity)

    small_boxes = remaining / @small_boxes_quantity |> trunc()
    remaining = rem(remaining, @small_boxes_quantity) |> trunc()

    %{
      big: big_boxes,
      medium: medium_boxes,
      small: small_boxes,
      remaining_match_sticks: remaining
    }
  end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment