A local store is moving its inventory to a larger warehouse. You were hired to pack and move everything.
You have four tasks, all related to managing the transport.
These are the instructions mentioned in this concept:
| Instruction | Description |
|---|---|
| add a, b | a = a + b |
| adc a, b | a = a + b + CF (previous carry) |
| inc a | a = a + 1 |
| sub a, b | a = a - b |
| dec a | a = a - 1 |
| imul a | rdx:rax = a * rax (signed) |
| imul a, b | a = a * b (signed) |
| imul a, b, c | a = b * c (signed) |
| mul a | rdx:rax = a * rax (unsigned) |
| div a | rax = quotient, rdx = remainder of rdx:rax / a (unsigned) |
| idiv a | rax = quotient, rdx = remainder of rdx:rax / a (signed) |
| movzx a, b | a = b, adding 0 to the extra bits |
| movsx a, b | a = b, adding 1 to the extra bits if b < 0 or 0 otherwise |
Remember that you can access the same register with different sizes by changing the name of the operand.
For example: rax (64-bit), eax (32-bit), ax (16-bit), al (8-bit).
You can refer to the previous concept for the full table.
Items are being packed in boxes that must be labeled with their weight. There is no scale around, but luckily you know how much each item weighs on average.
To better organize things, a box holds only items of two different products.
Define a function get_box_weight that returns the total weight of a box, in g.
This function takes as parameters, in this order:
ggConsider that an empty box weighs 500 g.
A constant WEIGHT_OF_EMPTY_BOX is defined at the top of the solution file.
Example:
get_box_weight(30, 40, 50, 20);
// => 2700All arguments are 16-bit non-negative integers, and the return value is a 32-bit non-negative integer.
Boxes are being stacked and moved to the new warehouse in a truck. However, there is only so much vertical space in the truck.
Define a function max_number_of_boxes that returns how many boxes of a certain height can be stacked vertically (one on top of another) within the truck.
This function takes as parameter the height of the box, in cm.
Consider that the truck interior height is 300 cm.
A constant TRUCK_HEIGHT is defined at the top of the solution file.
Example:
max_number_of_boxes(30);
// => 10The argument and the return value are 8-bit non-negative integers.
The box height is always at least 2, so the result fits in 8 bits.
There is a checklist in the new warehouse with the number of items still unaccounted for each product. For each new box moved there, you need to calculate the new value in the checklist for each product in the box.
Define a function items_to_be_moved that returns how many items remain to be moved to the new warehouse for a given product.
This function takes as parameters, in this order:
Example:
items_to_be_moved(76532, 120);
// => 76412The arguments are 32-bit non-negative integers. The return value is a 32-bit integer. In case of an error in the process, it is possible that the result is a negative number.
Your payment is based on how many boxes were moved and how many truck trips were necessary.
For each box, you will be paid 5 dollars and for each trip, you will be paid 220 dollars.
Constants PAY_PER_BOX and PAY_PER_TRUCK_TRIP are defined at the top of the solution file.
Note that you may have received part of this payment up front to cover initial costs, and this up-front payment should be subtracted from the final pay. In addition, some products are not covered by insurance and your payment will also be reduced by the value of any of those items broken or missing. It is possible that you end up owing money if you are not careful!
This means the net amount you are owed, or owe, is:
net = boxes * PAY_PER_BOX + trips * PAY_PER_TRUCK_TRIP - up_front - broken_items * item_valueThis payment, or debt, will be divided equally between you and a number of workers you hired.
Any remaining money, or debt, is yours.
For example, if the net amount of money is 100 being shared amongst 6 people (you and 5 workers), you get 20 (100/(5 + 1) = 16 plus the remaining 4).
Define a function calculate_payment that returns how much you should be paid, or pay, at the end.
This function takes as parameters, in this order:
Example:
calculate_payment(2000, 1000, 5, 21, 2, 1);
// => 2029The return value is a 64-bit integer.
A local store is moving its inventory to a larger warehouse. You were hired to pack and move everything.
You have four tasks, all related to managing the transport.
These are the instructions mentioned in this concept:
| Instruction | Description |
|---|---|
| add a, b | a = a + b |
| adc a, b | a = a + b + CF (previous carry) |
| inc a | a = a + 1 |
| sub a, b | a = a - b |
| dec a | a = a - 1 |
| imul a | rdx:rax = a * rax (signed) |
| imul a, b | a = a * b (signed) |
| imul a, b, c | a = b * c (signed) |
| mul a | rdx:rax = a * rax (unsigned) |
| div a | rax = quotient, rdx = remainder of rdx:rax / a (unsigned) |
| idiv a | rax = quotient, rdx = remainder of rdx:rax / a (signed) |
| movzx a, b | a = b, adding 0 to the extra bits |
| movsx a, b | a = b, adding 1 to the extra bits if b < 0 or 0 otherwise |
Remember that you can access the same register with different sizes by changing the name of the operand.
For example: rax (64-bit), eax (32-bit), ax (16-bit), al (8-bit).
You can refer to the previous concept for the full table.
Items are being packed in boxes that must be labeled with their weight. There is no scale around, but luckily you know how much each item weighs on average.
To better organize things, a box holds only items of two different products.
Define a function get_box_weight that returns the total weight of a box, in g.
This function takes as parameters, in this order:
ggConsider that an empty box weighs 500 g.
A constant WEIGHT_OF_EMPTY_BOX is defined at the top of the solution file.
Example:
get_box_weight(30, 40, 50, 20);
// => 2700All arguments are 16-bit non-negative integers, and the return value is a 32-bit non-negative integer.
Boxes are being stacked and moved to the new warehouse in a truck. However, there is only so much vertical space in the truck.
Define a function max_number_of_boxes that returns how many boxes of a certain height can be stacked vertically (one on top of another) within the truck.
This function takes as parameter the height of the box, in cm.
Consider that the truck interior height is 300 cm.
A constant TRUCK_HEIGHT is defined at the top of the solution file.
Example:
max_number_of_boxes(30);
// => 10The argument and the return value are 8-bit non-negative integers.
The box height is always at least 2, so the result fits in 8 bits.
There is a checklist in the new warehouse with the number of items still unaccounted for each product. For each new box moved there, you need to calculate the new value in the checklist for each product in the box.
Define a function items_to_be_moved that returns how many items remain to be moved to the new warehouse for a given product.
This function takes as parameters, in this order:
Example:
items_to_be_moved(76532, 120);
// => 76412The arguments are 32-bit non-negative integers. The return value is a 32-bit integer. In case of an error in the process, it is possible that the result is a negative number.
Your payment is based on how many boxes were moved and how many truck trips were necessary.
For each box, you will be paid 5 dollars and for each trip, you will be paid 220 dollars.
Constants PAY_PER_BOX and PAY_PER_TRUCK_TRIP are defined at the top of the solution file.
Note that you may have received part of this payment up front to cover initial costs, and this up-front payment should be subtracted from the final pay. In addition, some products are not covered by insurance and your payment will also be reduced by the value of any of those items broken or missing. It is possible that you end up owing money if you are not careful!
This means the net amount you are owed, or owe, is:
net = boxes * PAY_PER_BOX + trips * PAY_PER_TRUCK_TRIP - up_front - broken_items * item_valueThis payment, or debt, will be divided equally between you and a number of workers you hired.
Any remaining money, or debt, is yours.
For example, if the net amount of money is 100 being shared amongst 6 people (you and 5 workers), you get 20 (100/(5 + 1) = 16 plus the remaining 4).
Define a function calculate_payment that returns how much you should be paid, or pay, at the end.
This function takes as parameters, in this order:
Example:
calculate_payment(2000, 1000, 5, 21, 2, 1);
// => 2029The return value is a 64-bit integer.