Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
fmt-Println-MKO committed Aug 3, 2015
2 parents 11ed8c7 + 72b1a86 commit 9c98f5c
Show file tree
Hide file tree
Showing 8 changed files with 153 additions and 212 deletions.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Codeeval

my solutions for codeeval:
https://www.codeeval.com/profile/fmt-Println-MKO/

in **Go** and **Java**

Solutions are developed / optimized just from current thoughts, and **not** followed by problem solution description.
So they might not be the easiest, fastest, smallest or less memory used, but they solve the problem.
73 changes: 0 additions & 73 deletions build.xml

This file was deleted.

3 changes: 0 additions & 3 deletions manifest.mf

This file was deleted.

103 changes: 54 additions & 49 deletions src/de/sunbits/codeeval/packageproblem/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.*;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.regex.Pattern;

/**
* Created by matthiaskoch on 09.07.15.
Expand All @@ -17,46 +20,36 @@
public class Main {


final class Item implements Comparable<Item> {
int index;
float weight;
int cost;


@Override
public String toString() {
return "Item{" +
"index=" + index +
", weight=" + weight +
", cost=" + cost +
'}';
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {

@Override
public int compareTo(final Item o) {
if (weight == o.weight) {
return cost > o.cost ? 1 : -1;
} else {
return weight > o.weight ? 1 : -1;
}
Main pp = new Main();
if (args.length != 1) {
System.err.println("no file specified!!");
} else {
pp.solve(args[0]);
}
}

private final void solve(final String path) {

final String colon = ":";
final String braces = "\\)\\(";
final String comma = ",";

final Pattern patColon = Pattern.compile(":");
final Pattern patBraces = Pattern.compile("\\)\\(");
final Pattern patComma = Pattern.compile(comma);

final String oBrace = "(";
final String cBrace = ")";

final String empty = "";
final String space = " ";
final String com = ",";
final String dollar = "$";
final String minus = "-";

// final private static boolean[] emptyBolAr = new boolean[0];
// final private static byte[] emptyByteAr = new byte[0];

final int[] emptyIntAr = new int[0];

try {
Expand All @@ -67,19 +60,15 @@ private final void solve(final String path) {

for (String line : lines) {
if (line.length() == 0) {
System.out.println("-");
continue;
}

line = line.replace(space, empty);
// System.out.println(line);
String[] packline = line.split(colon);
String[] packline = patColon.split(line, 0);
packageMaxWeight = Integer.valueOf(packline[0]);
if (packageMaxWeight > 100) {
continue;
}
String[] sItems = packline[1].split(braces);

String[] sItems = patBraces.split(packline[1], 0);

int sLength = sItems.length;

Expand All @@ -88,7 +77,7 @@ private final void solve(final String path) {
for (int i = 0; i < sLength; i++) {
String sItem = sItems[i].replace(oBrace, empty);
sItem = sItem.replace(cBrace, empty);
String[] itemData = sItem.split(com);
String[] itemData = patComma.split(sItem, 0);

final float weight = Float.valueOf(itemData[1]);

Expand All @@ -106,7 +95,6 @@ private final void solve(final String path) {
int maxCost = 0;
float maxWeight = packageMaxWeight;

// boolean[] cPack = emptyBolAr;
int[] cPack = emptyIntAr;
int cPacked = 0;
for (int i = 0; i < iSize; i++) {
Expand All @@ -116,21 +104,26 @@ private final void solve(final String path) {
float currentWeight = 0;
int[] pack = new int[15];
int packed = 0;

final Item item1 = items.get(j);
if ((currentWeight + item1.weight) <= packageMaxWeight) {
pack[item1.index] = 1;
currentCost += item1.cost;
currentWeight += item1.weight;
packed++;
}

for (int k = i; k < iSize; k++) {
//System.out.println(j + "-" + k);
if (k != j) {
final Item item = items.get(k);
if ((currentWeight + item.weight) <= packageMaxWeight) {
pack[item.index] = 1;
currentCost += item.cost;
currentWeight += item.weight;
packed++;
} else {
break;
}
}
}
// System.out.println(aPackage +" -- " + aPackage.getItemListAsString());
if (packed > 0 && (currentCost > maxCost || (currentCost == maxCost && currentWeight < maxWeight))) {
maxCost = currentCost;
maxWeight = currentWeight;
Expand All @@ -147,7 +140,7 @@ private final void solve(final String path) {
sb.append(i);
j++;
if (j < cPacked) {
sb.append(com);
sb.append(comma);
} else {
break;
}
Expand All @@ -163,16 +156,28 @@ private final void solve(final String path) {
}
}

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
final class Item implements Comparable<Item> {
int index;
float weight;
int cost;

Main pp = new Main();
if (args.length != 1) {
System.err.println("no file specified!!");
} else {
pp.solve(args[0]);

@Override
public String toString() {
return "Item{" +
"index=" + index +
", weight=" + weight +
", cost=" + cost +
'}';
}

@Override
public int compareTo(final Item o) {
if (cost == o.cost) {
return weight > o.weight ? 1 : -1;
} else {
return cost < o.cost ? 1 : -1;
}
}
}
}
64 changes: 0 additions & 64 deletions src/de/sunbits/codeeval/packageproblem/main.rb

This file was deleted.

3 changes: 2 additions & 1 deletion src/de/sunbits/codeeval/packageproblem/results.txt
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
1 Java Jul 09, 2015 Partially 90 672 5898240 yes 74.319 TRY AGAIN DELETE
1 Java Jul 14, 2015 Solved 100 771 6164480 yes 81.444 Share on LinkedIn DELETE
2 Java Jul 14, 2015 Solved 100 811 5632000 yes 82.513 Share on LinkedIn DELETE
Loading

0 comments on commit 9c98f5c

Please sign in to comment.