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

Apprentice 2023B solutions #429

Open
wants to merge 2 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
Binary file modified .DS_Store
Binary file not shown.
52 changes: 29 additions & 23 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,23 +1,29 @@
# google-code-jam

This repository shows reference solutions for several Google Code Jam problems.

## Table of Contents

1. [Alien language](solutions/alien-language)
2. [Bathroom stalls](solutions/bathroom-stalls)
3. [Cubic UFO](solutions/cubic-ufo)
4. [Dijkstra](solutions/dijkstra)
5. [Fashion show](solutions/fashion-show)
6. [Go gopher](solutions/go-gopher)
7. [Infinite house of pancakes](solutions/infinite-house-of-pancakes)
8. [Minimum scalar product](solutions/minimum-scalar-product)
9. [Ominous omino](solutions/ominous-omino)
10. [Oversized pancake flipper](solutions/oversized-pancake-flipper)
11. [Reverse words](solutions/reverse-words)
12. [Save the universe](solutions/save-the-universe)
13. [Standing ovation](solutions/standing-ovation)
14. [Store credit](solutions/store-credit)
15. [T9 spelling](solutions/t9-spelling)
16. [Tidy numbers](solutions/tidy-numbers)
17. [Trouble sort](solutions/trouble-sort)
# google-code-jam

This repository shows reference solutions for several Google Code Jam problems.

## Table of Contents

1. [Alien language](solutions/alien-language)
2. [Bathroom stalls](solutions/bathroom-stalls)
3. [Cubic UFO](solutions/cubic-ufo)
4. [Dijkstra](solutions/dijkstra)
5. [Fashion show](solutions/fashion-show)
6. [Go gopher](solutions/go-gopher)
7. [Infinite house of pancakes](solutions/infinite-house-of-pancakes)
8. [Minimum scalar product](solutions/minimum-scalar-product)
9. [Ominous omino](solutions/ominous-omino)
10. [Oversized pancake flipper](solutions/oversized-pancake-flipper)
11. [Reverse words](solutions/reverse-words)
12. [Save the universe](solutions/save-the-universe)
13. [Standing ovation](solutions/standing-ovation)
14. [Store credit](solutions/store-credit)
15. [T9 spelling](solutions/t9-spelling)
16. [Tidy numbers](solutions/tidy-numbers)
17. [Trouble sort](solutions/trouble-sort)
18. [Punched cards](solutions/punched_cards/)
19. [3D Printing](solutions/3DPrinting/)
20. [d1000000](solutions/d1000000/)
21. [Chain reactions](solutions/chain-reactions/)
22. [Twisty Little Passages](solutions/twisty-little-passages/)
23. [Double or One thing](solutions/double-or-one-thing/)
Binary file modified solutions/.DS_Store
Binary file not shown.
Binary file added solutions/3DPrinting/.DS_Store
Binary file not shown.
100 changes: 100 additions & 0 deletions solutions/3DPrinting/Dart/3dprinting.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
// Google Code Jam doesn't allow `import 'dart:io';`. Now what? :C
import 'dart:io';
import 'dart:math';

int get_input_int()
{
var my_input = stdin.readLineSync();
if (my_input != null)
return int.parse(my_input);
else
return 0;
}
String get_input_string()
{
var my_input = stdin.readLineSync();
if (my_input != null)
return my_input;
else
return '';
}


List<List<int>> get_printers_input()
{
List<List<int>> printers = [];
for (int j = 0; j < 3; j++)
{
String input_line = get_input_string();
List<String> input_split = input_line.split(' ');
List<int> input_ints = [
int.parse(input_split[0]),
int.parse(input_split[1]),
int.parse(input_split[2]),
int.parse(input_split[3])
];
printers.add(input_ints);
}
return printers;
}


List<dynamic> give_color_x(List<List<int>> printers, int color_pos)
{
return <dynamic>[
printers[0][color_pos],
printers[1][color_pos],
printers[2][color_pos]
];
}

List<int> return_solution(List<List<int>> printers)
{
List<int> my_colors = [0, 0, 0, 0];
for (int i = 0; i < 4; i++)
{
num min_units = give_color_x(printers, i).cast<num>().reduce(min);
my_colors[i] = min_units.toInt();
int sum_of_units = my_colors.reduce((a, b) => a + b);

if (sum_of_units >= 1000000)
{
int difference = sum_of_units - 1000000;
my_colors[i] = my_colors[i] - difference;
return my_colors;
}
}
return my_colors;
}


void print_solution(int test_case, List<int> result)
{
stdout.write('Case #${test_case}: ');
int sum_of_units = result.reduce((a, b) => a + b);
if (sum_of_units == 1000000)
{
for (int i = 0; i < 4; i++)
{
stdout.write(result[i]);
stdout.write(' ');
}
stdout.write('\n');
}
else
{
print("IMPOSSIBLE");
}
}

void main() {
int Times = get_input_int();

for (int i = 1; i <= Times; i++)
{
List<List<int>> my_printers = get_printers_input();

List<int> result = return_solution(my_printers);
print_solution(i, result);
}
}
44 changes: 44 additions & 0 deletions solutions/3DPrinting/Kotlin/3dPrinting.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/**
Name: punchedCard.dart
Last update: 18/04/2023
*/

fun main(args: Array<String>) {
val testCases = readln().toInt()

for (i in 0..testCases) {

var minC = Int.MAX_VALUE
var minM = Int.MAX_VALUE
var minY = Int.MAX_VALUE
var minK = Int.MAX_VALUE

for (j in 1..3) {
// Getting the ink amount of each color in the current printer.
val (c, m, y, k) = readln().split(' ').map(String::toInt)

// Choosing the smallest option.
minC = if (c < minC) c else minC
minM = if (m < minM) m else minM
minY = if (y < minY) y else minY
minK = if (k < minK) k else minK
}

var totalInk = arrayOf(minC, minM, minY, minK)

if (totalInk.sum() < 1e6) println("Case #$i: IMPOSSIBLE")
else {
// Using a greedy algorithm to get a combination that fulfills the needed ink.
var neededInk = 1_000_000
var inkCombination = "Case #$i: "

for (currentInkAmount in totalInk) {
if(currentInkAmount <= neededInk) inkCombination += "$currentInkAmount "
else if (currentInkAmount > neededInk && neededInk > 0) inkCombination += "$neededInk "
else inkCombination += "0 "
neededInk -= currentInkAmount
}
println(inkCombination)
}
}
}
53 changes: 53 additions & 0 deletions solutions/3DPrinting/Kotlin/main.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// kotlinc main.kt -include-runtime -d main.jar && java -jar main.jar 3 300000 200000 300000 500000 300000 200000 500000 300000 300000 500000 300000 200000 1000000 1000000 0 0 0 1000000 1000000 1000000 999999 999999 999999 999999 768763 148041 178147 984173 699508 515362 534729 714381 949704 625054 946212 951187

fun main(args: Array<String>) {
val T: Int = args[0].toInt()
var index = 1
for (test in 0..T - 1) {
var printers: MutableList<MutableList<Int>> = mutableListOf()
for (printer in 0..2) {
var colors: MutableList<Int> = mutableListOf()
for (inkValue in 0..3) {
colors.add(args[index].toInt())
index++
}
printers.add(colors)
}
print("Case #" + (test + 1) + ":")
solution(printers)
}
}

fun solution(printers: MutableList<MutableList<Int>>) {
var necessaryInk = 1000000
var totalInk = 0
var mins: MutableList<Int> = mutableListOf()
for (color in 0..3) {
val arr = arrayOf(
printers[0][color],
printers[1][color],
printers[2][color],
)
val ints = arr.toList()
totalInk += ints.min()
mins.add(ints.min())
}

if (totalInk < necessaryInk) {
println("IMPOSSIBLE")
} else {
var remaining = totalInk - necessaryInk
for (index in 3 downTo 0) {
if (remaining > 0) {
if (mins[index] <= remaining) {
remaining -= mins[index]
mins[index] = 0
} else {
mins[index] = mins[index] - remaining
remaining = 0
}
}
}
println(mins)
}
}
38 changes: 38 additions & 0 deletions solutions/3DPrinting/Python/Python3DPrinting.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
def find_color(test_cases, printers):
results = []
for t in range(test_cases):
min_units = 0
my_colors = [0, 0, 0, 0]
for i in range(4):
min_units = min(printers[t][0][i], printers[t][1][i], printers[t][2][i])

my_colors[i] = min_units
if sum(my_colors) < 10**6:
my_colors[i] = min_units
elif sum(my_colors) >= 10**6:
a = sum(my_colors) - 10**6
my_colors[i] = my_colors[i] - a
break

if sum(my_colors) < 10**6:
results.append(["IMPOSSIBLE"])
else:
results.append(my_colors)
return results


def main():
test_cases = int(input().strip())
printers = []
for _ in range(test_cases):
printer_info = [list(map(int, input().split())) for _ in range(3)]
printers.append(printer_info)

results = find_color(test_cases, printers)
for i, result in enumerate(results):
print(f"Case #{i+1}: ", end='')
print(*result)


if __name__ == "__main__":
main()
16 changes: 16 additions & 0 deletions solutions/3DPrinting/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# 3D Printing

## Problem:
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.

![](https://codejam.googleapis.com/dashboard/get_file/AQj_6U1yXmbP6Nf5PONAMbqVd5eyM5BBSbjggzDn9H6vS3ATQiqbGVrfZ0ABoAbBkn8IWocYoj1rdJim6VkTTOP4/3d_printing.png)

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 10^6
units. For example, printing a D in pure yellow would use 10^6 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 [here](https://codingcompetitions.withgoogle.com/codejam/round/0000000000876ff1/0000000000a4672b)
Loading