-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 6da4cc2
Showing
5 changed files
with
334 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
*.iml | ||
out |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<!-- You may freely edit this file. See commented blocks below for --> | ||
<!-- some examples of how to customize the build. --> | ||
<!-- (If you delete it and reopen the project it will be recreated.) --> | ||
<!-- By default, only the Clean and Build commands use this build script. --> | ||
<!-- Commands such as Run, Debug, and Test only use this build script if --> | ||
<!-- the Compile on Save feature is turned off for the project. --> | ||
<!-- You can turn off the Compile on Save (or Deploy on Save) setting --> | ||
<!-- in the project's Project Properties dialog box.--> | ||
<project name="Codeeval" default="default" basedir="."> | ||
<description>Builds, tests, and runs the project Codeeval.</description> | ||
<import file="nbproject/build-impl.xml"/> | ||
<!-- | ||
There exist several targets which are by default empty and which can be | ||
used for execution of your tasks. These targets are usually executed | ||
before and after some main targets. They are: | ||
-pre-init: called before initialization of project properties | ||
-post-init: called after initialization of project properties | ||
-pre-compile: called before javac compilation | ||
-post-compile: called after javac compilation | ||
-pre-compile-single: called before javac compilation of single file | ||
-post-compile-single: called after javac compilation of single file | ||
-pre-compile-test: called before javac compilation of JUnit tests | ||
-post-compile-test: called after javac compilation of JUnit tests | ||
-pre-compile-test-single: called before javac compilation of single JUnit test | ||
-post-compile-test-single: called after javac compilation of single JUunit test | ||
-pre-jar: called before JAR building | ||
-post-jar: called after JAR building | ||
-post-clean: called after cleaning build products | ||
(Targets beginning with '-' are not intended to be called on their own.) | ||
Example of inserting an obfuscator after compilation could look like this: | ||
<target name="-post-compile"> | ||
<obfuscate> | ||
<fileset dir="${build.classes.dir}"/> | ||
</obfuscate> | ||
</target> | ||
For list of available properties check the imported | ||
nbproject/build-impl.xml file. | ||
Another way to customize the build is by overriding existing main targets. | ||
The targets of interest are: | ||
-init-macrodef-javac: defines macro for javac compilation | ||
-init-macrodef-junit: defines macro for junit execution | ||
-init-macrodef-debug: defines macro for class debugging | ||
-init-macrodef-java: defines macro for class execution | ||
-do-jar: JAR building | ||
run: execution of project | ||
-javadoc-build: Javadoc generation | ||
test-report: JUnit report generation | ||
An example of overriding the target for project execution could look like this: | ||
<target name="run" depends="Codeeval-impl.jar"> | ||
<exec dir="bin" executable="launcher.exe"> | ||
<arg file="${dist.jar}"/> | ||
</exec> | ||
</target> | ||
Notice that the overridden target depends on the jar target and not only on | ||
the compile target as the regular run target does. Again, for a list of available | ||
properties which you can use, check the target you are overriding in the | ||
nbproject/build-impl.xml file. | ||
--> | ||
</project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
Manifest-Version: 1.0 | ||
X-COMMENT: Main-Class will be added automatically by build | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,252 @@ | ||
package de.sunbits.codeeval.packageproblem; | ||
|
||
|
||
import java.io.IOException; | ||
import java.nio.charset.StandardCharsets; | ||
import java.nio.file.Files; | ||
import java.nio.file.Paths; | ||
import java.util.*; | ||
import java.util.logging.Level; | ||
import java.util.logging.Logger; | ||
|
||
/** | ||
* Created by matthiaskoch on 09.07.15. | ||
* | ||
* @author matthiaskoch | ||
*/ | ||
public class Main { | ||
|
||
|
||
final public static PackageCostComparator PACKAGE_COST_COMPARATOR = new PackageCostComparator(); | ||
final public static ItemCostComparator ITEM_COST_COMPARATOR = new ItemCostComparator(); | ||
final public static ItemWeightComparator ITEM_WEIGHT_COMPARATOR = new ItemWeightComparator(); | ||
|
||
final public static String colon = ":"; | ||
final public static String braces = "\\)\\("; | ||
final public static String oBrace = "("; | ||
final public static String cBrace = ")"; | ||
|
||
final public static String empty = ""; | ||
final public static String space = " "; | ||
final public static String com = ","; | ||
final public static String dollar = "$"; | ||
final public static String minus = "-"; | ||
|
||
|
||
final class Item { | ||
int index; | ||
float weight; | ||
int cost; | ||
|
||
|
||
@Override | ||
public String toString() { | ||
return "Item{" + | ||
"index=" + index + | ||
", weight=" + weight + | ||
", cost=" + cost + | ||
'}'; | ||
} | ||
} | ||
|
||
final class Package { | ||
final List<Integer> items = new ArrayList<Integer>(15); | ||
float weight; | ||
int cost; | ||
|
||
public final String getItemListAsString() { | ||
Collections.sort(items); | ||
return listToString(items); | ||
} | ||
|
||
// @Override | ||
// public boolean equals(Object o) { | ||
// if (this == o) return true; | ||
// if (o == null || getClass() != o.getClass()) return false; | ||
// | ||
// Package aPackage = (Package) o; | ||
// | ||
// if (Float.compare(aPackage.weight, weight) != 0) return false; | ||
// if (cost != aPackage.cost) return false; | ||
// Collections.sort(items); | ||
// return items.equals(aPackage.items); | ||
// | ||
// } | ||
// | ||
// @Override | ||
// public int hashCode() { | ||
// Collections.sort(items); | ||
// int result = items.hashCode(); | ||
// result = 31 * result + (weight != +0.0f ? Float.floatToIntBits(weight) : 0); | ||
// result = 31 * result + cost; | ||
// return result; | ||
// } | ||
|
||
@Override | ||
public String toString() { | ||
return "Package{" + | ||
"items=" + items + | ||
", weight=" + weight + | ||
", cost=" + cost + | ||
'}'; | ||
} | ||
} | ||
|
||
public static final String listToString(List<Integer> items) { | ||
final StringBuilder sb = new StringBuilder(); | ||
final int size = items.size(); | ||
for (int i = 0; i < size; i++) { | ||
if (i > 0) { | ||
sb.append(com); | ||
} | ||
sb.append(items.get(i)); | ||
} | ||
return sb.toString(); | ||
} | ||
|
||
final public static class ItemCostComparator implements Comparator<Item> { | ||
|
||
@Override | ||
public int compare(final Item o1, final Item o2) { | ||
return o1.cost > o2.cost ? 1 : -1; | ||
} | ||
} | ||
|
||
final public static class ItemWeightComparator implements Comparator<Item> { | ||
|
||
@Override | ||
public int compare(final Item o1, final Item o2) { | ||
if (o1.weight == o2.weight) { | ||
return o1.cost > o2.cost ? 1 : -1; | ||
} else { | ||
return o1.weight > o2.weight ? 1 : -1; | ||
} | ||
} | ||
} | ||
|
||
final public static class PackageCostComparator implements Comparator<Package> { | ||
|
||
@Override | ||
public int compare(final Package o1, final Package o2) { | ||
if (o1.cost == o2.cost) { | ||
return o1.weight < o2.weight ? 1 : -1; | ||
} else { | ||
return o1.cost > o2.cost ? 1 : -1; | ||
} | ||
} | ||
} | ||
|
||
|
||
public final void solve(final String path) { | ||
try { | ||
int iSize; | ||
int packageMaxWeight; | ||
|
||
List<String> lines = Files.readAllLines(Paths.get(path), StandardCharsets.UTF_8); | ||
|
||
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); | ||
packageMaxWeight = Integer.valueOf(packline[0]); | ||
if (packageMaxWeight > 100) { | ||
continue; | ||
} | ||
String[] sitems = packline[1].split(braces); | ||
|
||
List<Item> items = new ArrayList<Item>(15); | ||
|
||
int sLenght = sitems.length; | ||
|
||
for (int i = 0; i < sLenght; i++) { | ||
String sitem = sitems[i].replace(oBrace, empty); | ||
sitem = sitem.replace(cBrace, empty); | ||
String[] itemData = sitem.split(com); | ||
|
||
final float weight = Float.valueOf(itemData[1]); | ||
if (weight <= packageMaxWeight) { | ||
Item item = new Item(); | ||
item.index = Integer.valueOf(itemData[0]); | ||
item.weight = weight; | ||
item.cost = Integer.valueOf(itemData[2].replace(dollar, empty)); | ||
items.add(item); | ||
} | ||
} | ||
|
||
// Map<String, Package> packageMap = new HashMap<>(sLenght); | ||
// final Set<Package> packageSet = new HashSet<>(); | ||
|
||
Collections.sort(items, ITEM_WEIGHT_COMPARATOR); | ||
|
||
iSize = items.size(); | ||
int maxCost = 0; | ||
float maxWeight = packageMaxWeight; | ||
String max = minus; | ||
for (int i = 0; i < iSize; i++) { | ||
for (int j = i; j < iSize; j++) { | ||
|
||
Package aPackage = new Package(); | ||
for (int k = i; k < iSize; k++) { | ||
|
||
if (k != j) { | ||
final Item item = items.get(k); | ||
if ((aPackage.weight + item.weight) <= packageMaxWeight) { | ||
aPackage.items.add(item.index); | ||
aPackage.cost += item.cost; | ||
aPackage.weight += item.weight; | ||
} else { | ||
break; | ||
} | ||
} | ||
} | ||
// System.out.println(aPackage +" -- " + aPackage.getItemListAsString()); | ||
if (aPackage.items.size() > 0 && (aPackage.cost > maxCost || (aPackage.cost == maxCost && aPackage.weight < maxWeight))) { | ||
// packageMap.put(aPackage.getItemListAsString(), aPackage); | ||
maxCost = aPackage.cost; | ||
maxWeight = aPackage.weight; | ||
max = aPackage.getItemListAsString(); | ||
} | ||
// packageSet.add(aPackage); | ||
} | ||
} | ||
// final List<Package> packages = new ArrayList<>(packageMap.size()); | ||
// final List<Package> packages = new ArrayList<>(packageSet.size()); | ||
|
||
// packages.addAll(packageMap.values()); | ||
// packages.addAll(packageSet); | ||
|
||
// String max = minus; | ||
// int maxCost = 0; | ||
// for (String k : packageMap.keySet()) { | ||
// Package p = packageMap.get(k); | ||
// if (p.cost > maxCost) { | ||
// maxCost = p.cost; | ||
// max = k; | ||
// } | ||
// } | ||
|
||
System.out.println(max); | ||
} | ||
} catch (IOException ex) { | ||
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex); | ||
} | ||
} | ||
|
||
/** | ||
* @param args the command line arguments | ||
*/ | ||
public static void main(String[] args) { | ||
|
||
Main pp = new Main(); | ||
if (args.length != 1) { | ||
System.err.println("no file specified!!"); | ||
} else { | ||
pp.solve(args[0]); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
81 : (1,53.38,$45) (2,88.62,$98) (3,78.48,$3) (4,72.30,$76) (5,30.18,$9) (6,46.34,$48) | ||
8 : (1,15.3,$34) | ||
75 : (1,85.31,$29) (2,14.55,$74) (3,3.98,$16) (4,26.24,$55) (5,63.69,$52) (6,76.25,$75) (7,60.02,$74) (8,93.18,$35) (9,89.95,$78) | ||
56 : (1,90.72,$13) (2,33.80,$40) (3,43.15,$10) (4,37.97,$16) (5,46.81,$36) (6,48.77,$79) (7,81.80,$45) (8,19.36,$79) (9,6.76,$64) |