From 61f262ef50e8663f25921f2fd3363c1500e8a98e Mon Sep 17 00:00:00 2001 From: Aldo Santiago Date: Fri, 10 Feb 2023 17:10:17 -0600 Subject: [PATCH 1/5] 3d printing python solution --- solutions/3d-printing/python/approach1.py | 28 +++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 solutions/3d-printing/python/approach1.py diff --git a/solutions/3d-printing/python/approach1.py b/solutions/3d-printing/python/approach1.py new file mode 100644 index 00000000..bef18d1c --- /dev/null +++ b/solutions/3d-printing/python/approach1.py @@ -0,0 +1,28 @@ +def solve(printers): + maxInkPerColor = [1000000, 1000000, 1000000, 1000000] + + for printer in printers: + for i in range(0, len(printer)): + maxInkPerColor[i] = min(maxInkPerColor[i], printer[i]) + + inkUnits = sum(maxInkPerColor) + + if (inkUnits < 1000000): + return "IMPOSSIBLE" + + for i in range(0, len(maxInkPerColor)): + inkUnits = sum(maxInkPerColor) + diff = inkUnits - 1000000 + if diff == 0: + return " ".join(str(item) for item in maxInkPerColor) + if maxInkPerColor[i] >= diff: + maxInkPerColor[i] -= diff + else: + maxInkPerColor[i] = 0 + +t = int(input()) +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])}") \ No newline at end of file From ca21a94d17add10d65e3b5a2cb7820690f0ab073 Mon Sep 17 00:00:00 2001 From: Aldo Santiago Date: Fri, 10 Feb 2023 17:14:13 -0600 Subject: [PATCH 2/5] 3d printing: added readme file --- solutions/3d-printing/README.md | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 solutions/3d-printing/README.md diff --git a/solutions/3d-printing/README.md b/solutions/3d-printing/README.md new file mode 100644 index 00000000..2601eca9 --- /dev/null +++ b/solutions/3d-printing/README.md @@ -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 From 4f2d491deaa0a6b293449bcf0a1bde0bcdd85ea7 Mon Sep 17 00:00:00 2001 From: Aldo Santiago Date: Fri, 10 Feb 2023 18:51:43 -0600 Subject: [PATCH 3/5] docs: added comments and method doc --- solutions/3d-printing/python/approach1.py | 25 ++++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-) diff --git a/solutions/3d-printing/python/approach1.py b/solutions/3d-printing/python/approach1.py index bef18d1c..fc1b5a74 100644 --- a/solutions/3d-printing/python/approach1.py +++ b/solutions/3d-printing/python/approach1.py @@ -1,28 +1,43 @@ -def solve(printers): +def solve(printers: list) -> str: + """ + finds a solution consisting of 4 numbers representing cyan, yellow, magenta, and black ink units + 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], ...] + """ + + # 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, len(printer)): + 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, len(maxInkPerColor)): + + 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 maxInkPerColor[i] >= diff: + # just need to reduce the diff and next iteration diff will be 0 maxInkPerColor[i] -= diff else: maxInkPerColor[i] = 0 +# read number of test cases t = int(input()) 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])}") \ No newline at end of file + print(f"Case #{i}: {solve([printer1, printer2, printer3])}") + solve() \ No newline at end of file From 9a20127574a1b6138f2b308b0cc98a26b7b39880 Mon Sep 17 00:00:00 2001 From: Aldo Santiago Date: Sun, 12 Feb 2023 12:54:32 -0600 Subject: [PATCH 4/5] doc: updated documentation --- solutions/3d-printing/python/approach1.py | 46 ++++++++++++++++++----- 1 file changed, 36 insertions(+), 10 deletions(-) diff --git a/solutions/3d-printing/python/approach1.py b/solutions/3d-printing/python/approach1.py index fc1b5a74..b10911ee 100644 --- a/solutions/3d-printing/python/approach1.py +++ b/solutions/3d-printing/python/approach1.py @@ -1,9 +1,33 @@ -def solve(printers: list) -> str: - """ - finds a solution consisting of 4 numbers representing cyan, yellow, magenta, and black ink units - 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], ...] +"""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 @@ -18,7 +42,8 @@ def solve(printers: list) -> str: inkUnits = sum(maxInkPerColor) if (inkUnits < 1000000): - # no combination can be used in EVERY printer to print with 1000000 units + # no combination can be used in EVERY printer + # to print with 1000000 units return "IMPOSSIBLE" for i in range(0, 4): @@ -27,14 +52,15 @@ def solve(printers: list) -> str: 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 next iteration diff will be 0 + # just need to reduce the diff + # and at next iteration diff value will be 0 maxInkPerColor[i] -= diff else: maxInkPerColor[i] = 0 -# read number of test cases -t = int(input()) +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(' ')] From c67ec3e85f9d202d2496495a13af8aa34c42feb2 Mon Sep 17 00:00:00 2001 From: Aldo Santiago Date: Mon, 13 Feb 2023 21:29:27 -0600 Subject: [PATCH 5/5] fix: unkown call to "solve" --- solutions/3d-printing/python/approach1.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/solutions/3d-printing/python/approach1.py b/solutions/3d-printing/python/approach1.py index b10911ee..5d37dd1d 100644 --- a/solutions/3d-printing/python/approach1.py +++ b/solutions/3d-printing/python/approach1.py @@ -66,4 +66,4 @@ def solve(printers): printer2 = [int(s) for s in input().split(' ')] printer3 = [int(s) for s in input().split(' ')] print(f"Case #{i}: {solve([printer1, printer2, printer3])}") - solve() \ No newline at end of file + \ No newline at end of file