Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

3d printing python solution uploaded - Apprentice 2023a #420

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions solutions/3d-printing/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# 3D Printing

You are part of the executive committee of the Database Design Day festivities. You are in charge of promotions and want to print three D's to create a logo of the contest. You can choose any color you want to print them, but all three have to be printed in the same color.

You were given three printers and will use each one to print one of the D's. All printers use ink from 4 individual cartridges of different colors (cyan, magenta, yellow, and black) to form any color. For these printers, a color is uniquely defined by 4 non-negative integers c, m, y, and k, which indicate the number of ink units of cyan, magenta, yellow, and black ink (respectively) needed to make the color.

The total amount of ink needed to print a single D is exactly 1000000 units. For example, printing a D in pure yellow would use 1000000 units of yellow ink and 0 from all others. Printing a D in the Code Jam red uses 0 units of cyan ink, 500000 units of magenta ink, 450000 units of yellow ink, and 50000 units of black ink.

To print a color, a printer must have at least the required amount of ink for each of its 4 color cartridges. Given the number of units of ink each printer has in each cartridge, output any color, defined as 4 non-negative integers that add up to 106, such that all three printers have enough ink to print it.

More details: https://codingcompetitions.withgoogle.com/codejam/round/0000000000876ff1/0000000000a4672b
69 changes: 69 additions & 0 deletions solutions/3d-printing/python/approach1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
"""3d printing solution

This is a solution for the Google Code Jam '3d printing' problem
The first line of the input gives the number of test cases, T.
T test cases follow. Each test case consists of 3 lines.
The i-th line of a test case contains 4 integers
Ci, Mi, Yi, and Ki, representing the number of ink units
in the i-th printer's cartridge for the colors
cyan, magenta, yellow, and black, respectively.
More information:
codingcompetitions.withgoogle.com/
codejam/round/0000000000876ff1/0000000000a4672b
"""

def solve(printers):
"""Finds a 4-digit color solution

Finds a solution consisting of 4 numbers
representing cyan, yellow, magenta, and black ink units

Args:
printers: list of (list of int)
inner lists represent ink units available in a printer,
outer list represents the printers
[[c, y, m, b], [c, y, m, b], ...]

Returns:
A string containing 4 space separated numbers
representing a color made up of
cyan, yellow, magenta and black ink units
"""

# Initialize the "available ink units" per ink type
# C Y M B
maxInkPerColor = [1000000, 1000000, 1000000, 1000000]

# finds the min value per ink type in the printers
for printer in printers:
for i in range(0, 4):
maxInkPerColor[i] = min(maxInkPerColor[i], printer[i])

inkUnits = sum(maxInkPerColor)

if (inkUnits < 1000000):
# no combination can be used in EVERY printer
# to print with 1000000 units
return "IMPOSSIBLE"

for i in range(0, 4):
inkUnits = sum(maxInkPerColor)
diff = inkUnits - 1000000
if diff == 0:
# return solution in string format
return " ".join(str(item) for item in maxInkPerColor)
# if current sum is not 1000000 we need to reduce ink units
if maxInkPerColor[i] >= diff:
# just need to reduce the diff
# and at next iteration diff value will be 0
maxInkPerColor[i] -= diff
else:
maxInkPerColor[i] = 0

t = int(input()) # read number of test cases
for i in range(1, t+1):
printer1 = [int(s) for s in input().split(' ')]
printer2 = [int(s) for s in input().split(' ')]
printer3 = [int(s) for s in input().split(' ')]
print(f"Case #{i}: {solve([printer1, printer2, printer3])}")