Skip to content

Commit

Permalink
package problem solved
Browse files Browse the repository at this point in the history
  • Loading branch information
fmt-Println-MKO committed Jul 14, 2015
1 parent a412789 commit d85db07
Show file tree
Hide file tree
Showing 5 changed files with 122 additions and 144 deletions.
90 changes: 33 additions & 57 deletions src/de/sunbits/codeeval/packageproblem/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
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;

Expand All @@ -17,37 +19,16 @@
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 +
'}';
}

// @Override
// public int compareTo(final Item o) {
// if (weight == o.weight) {
// return cost > o.cost ? 1 : -1;
// } else {
// return weight > o.weight ? 1 : -1;
// }
// }
/**
* @param args the command line arguments
*/
public static void main(String[] args) {

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

Expand All @@ -64,8 +45,6 @@ private final void solve(final String path) {
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 @@ -79,8 +58,6 @@ private final void solve(final String path) {
System.out.println("-");
continue;
}
// System.out.println(line);

line = line.replace(space, empty);
String[] packline = line.split(colon);
packageMaxWeight = Integer.valueOf(packline[0]);
Expand All @@ -89,7 +66,6 @@ private final void solve(final String path) {
}
String[] sItems = packline[1].split(braces);


int sLength = sItems.length;

List<Item> items = new ArrayList<>(sLength);
Expand All @@ -110,13 +86,11 @@ private final void solve(final String path) {
}
}
Collections.sort(items);
// System.out.println("Items: " + items);

iSize = items.size();
int maxCost = 0;
float maxWeight = packageMaxWeight;

// boolean[] cPack = emptyBolAr;
int[] cPack = emptyIntAr;
int cPacked = 0;
for (int i = 0; i < iSize; i++) {
Expand All @@ -128,34 +102,24 @@ private final void solve(final String path) {
int packed = 0;

final Item item1 = items.get(j);
// System.out.println("----" + (currentWeight + item1.weight) +"--- "+ packageMaxWeight + " ----> " + currentCost);
if ((currentWeight + item1.weight) <= packageMaxWeight) {
pack[item1.index] = 1;
currentCost += item1.cost;
currentWeight += item1.weight;
packed++;
} else {
// break;
}


for (int k = i; k < iSize; k++) {
// System.out.println(j + "-" + k);
if (k != j) {
final Item item = items.get(k);
// System.out.println((currentWeight + item.weight) +"--- "+ packageMaxWeight + " ----> " + currentCost);
if ((currentWeight + item.weight) <= packageMaxWeight) {
pack[item.index] = 1;
currentCost += item.cost;
currentWeight += item.weight;
packed++;
} else {
// break;
}
}
}
// System.out.println(aPackage +" -- " + aPackage.getItemListAsString());
// System.out.println(packed +"-- "+ currentCost +" -- "+ maxCost +" :--------: " + currentWeight +" :-: "+ maxWeight);
if (packed > 0 && (currentCost > maxCost || (currentCost == maxCost && currentWeight < maxWeight))) {
maxCost = currentCost;
maxWeight = currentWeight;
Expand Down Expand Up @@ -188,16 +152,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.

2 changes: 1 addition & 1 deletion src/de/sunbits/codeeval/packageproblem/results.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
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
46 changes: 45 additions & 1 deletion src/packageproblem.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,27 @@
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)
94 : (1,81.61,$91) (2,12.39,$30) (3,95.37,$91) (4,19.22,$54) (5,61.29,$39) (6,25.25,$37) (7,76.30,$75)
85 : (1,52.82,$84) (2,57.98,$68) (3,73.66,$47) (4,96.33,$71) (5,1.32,$34) (6,84.62,$84) (7,94.43,$65) (8,66.54,$48) (9,92.62,$91) (10,4.21,$50) (11,77.06,$77) (12,38.19,$41)
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)
100 : (1,76.11,$3) (2,96.96,$42) (3,68.75,$81) (4,15.41,$27) (5,48.42,$43) (6,32.85,$15) (7,71.33,$28) (8,47.80,$72) (9,87.23,$64) (10,37.17,$47)
97 : (1,60.69,$16) (2,7.20,$73) (3,24.08,$97) (4,10.23,$50) (5,31.00,$53) (6,94.35,$73) (7,35.56,$53) (8,4.94,$36) (9,63.67,$39)
67 : (1,27.84,$100) (2,12.17,$21) (3,6.40,$50) (4,92.06,$34) (5,86.94,$48)
2 : (1,2.5,$15)
71 : (1,10.28,$72) (2,98.12,$67) (3,29.12,$58) (4,91.42,$74) (5,93.54,$38) (6,14.37,$21) (7,46.89,$79)
81 : (1,38.03,$67) (2,19.94,$66) (3,52.68,$24) (4,22.77,$26) (5,28.33,$67)
74 : (1,71.71,$44) (2,29.47,$50) (3,96.58,$8) (4,41.09,$55) (5,52.18,$55) (6,72.24,$91) (7,4.35,$76)
50 : (1,52.62,$88) (2,66.96,$48) (3,84.65,$77) (4,48.26,$20) (5,12.31,$62) (6,47.27,$36) (7,40.42,$93) (8,51.65,$36) (9,74.53,$7) (10,95.69,$5)
89 : (1,31.41,$58) (2,94.19,$35) (3,68.93,$82) (4,94.52,$39) (5,55.13,$7) (6,85.15,$73) (7,51.95,$78) (8,27.88,$81)
73 : (1,17.09,$69) (2,44.54,$68) (3,78.30,$21) (4,87.44,$46) (5,12.44,$4) (6,24.42,$21) (7,42.35,$53) (8,33.05,$69) (9,89.77,$8)
82 : (1,49.76,$27) (2,47.31,$84) (3,23.36,$80) (4,51.42,$18) (5,62.82,$69) (6,59.88,$48)
70 : (1,75.42,$97) (2,36.78,$45) (3,41.30,$79) (4,23.60,$48) (5,68.48,$27) (6,23.53,$71) (7,1.64,$4) (8,15.77,$45) (9,72.93,$47)
95 : (1,3.63,$97) (2,53.83,$63) (3,96.20,$51) (4,61.13,$64) (5,49.04,$5) (6,62.20,$9) (7,32.53,$49) (8,77.59,$66) (9,96.28,$45) (10,67.48,$39) (11,43.39,$11)
68 : (1,6.02,$16) (2,14.31,$60) (3,5.72,$61) (4,72.61,$100) (5,68.38,$71)
66 : (1,56.47,$54) (2,37.88,$33) (3,97.80,$28) (4,25.30,$37) (5,18.03,$93) (6,15.50,$86) (7,72.65,$2) (8,76.70,$73) (9,60.99,$16) (10,15.65,$27) (11,8.80,$46) (12,43.89,$13) (13,71.56,$56)
60 : (1,67.14,$82) (2,67.91,$77) (3,75.20,$79) (4,76.81,$16) (5,68.42,$13) (6,56.08,$77)
94 : (1,58.26,$29) (2,91.26,$34) (3,80.19,$83) (4,29.80,$38) (5,26.94,$54) (6,20.15,$63) (7,48.39,$91) (8,33.69,$61) (9,50.46,$73) (10,47.16,$6)
55 : (1,65.75,$8) (2,50.26,$72) (3,8.58,$24) (4,62.06,$74) (5,95.60,$5) (6,50.57,$67) (7,78.11,$96)
72 : (1,38.18,$58) (2,32.16,$77) (3,9.28,$69) (4,32.07,$26) (5,54.08,$8) (6,83.99,$92) (7,55.04,$40) (8,86.75,$91) (9,64.20,$15) (10,33.62,$78)
55 : (1,13.96,$41) (2,8.81,$55) (3,65.83,$17) (4,40.31,$62) (5,90.98,$5) (6,79.40,$42) (7,72.83,$3) (8,89.79,$81) (9,12.50,$17) (10,87.63,$5)
Expand All @@ -17,4 +41,24 @@
66 : (1,56.47,$54) (2,37.88,$33) (3,97.80,$28) (4,25.30,$37) (5,18.03,$93) (6,15.50,$86) (7,72.65,$2) (8,76.70,$73) (9,60.99,$16) (10,15.65,$27) (11,8.80,$46) (12,43.89,$13) (13,71.56,$56)
65 : (1,84.80,$36) (2,65.28,$21) (3,77.00,$43) (4,30.51,$85) (5,81.95,$33) (6,64.61,$23) (7,93.06,$40) (8,88.49,$82) (9,37.96,$99) (10,27.46,$44)
88 : (1,35.45,$84) (2,1.80,$71) (3,24.54,$53) (4,33.54,$5) (5,96.13,$6) (6,96.32,$92)
94 : (1,49.49,$64) (2,60.57,$38) (3,10.38,$13) (4,96.28,$5) (5,81.08,$98) (6,93.46,$39) (7,58.56,$34) (8,42.02,$14)
94 : (1,49.49,$64) (2,60.57,$38) (3,10.38,$13) (4,96.28,$5) (5,81.08,$98) (6,93.46,$39) (7,58.56,$34) (8,42.02,$14)
78 : (1,11.08,$61) (2,24.71,$7) (3,90.55,$42) (4,34.26,$34) (5,35.61,$90) (6,41.10,$98) (7,59.55,$29) (8,47.82,$72) (9,73.52,$61) (10,88.72,$93) (11,42.50,$85)
81 : (1,38.03,$67) (2,19.94,$66) (3,52.68,$24) (4,22.77,$26) (5,28.33,$67)
71 : (1,10.28,$72) (2,98.12,$67) (3,29.12,$58) (4,91.42,$74) (5,93.54,$38) (6,14.37,$21) (7,46.89,$79)
88 : (1,84.22,$29) (2,27.39,$58) (3,38.03,$80) (4,45.94,$69) (5,69.96,$26)
2 : (1,2.5,$15)
65 : (1,28.11,$16) (2,14.11,$43) (3,22.81,$35) (4,13.55,$58) (5,76.14,$34) (6,52.57,$97) (7,8.46,$76) (8,17.02,$8) (9,79.71,$62)
55 : (1,47.10,$76) (2,9.45,$75) (3,36.11,$82) (4,16.33,$34) (5,90.79,$75)
70 : (1,81.11,$98) (2,19.11,$26) (3,66.68,$25) (4,63.37,$17) (5,54.15,$26) (6,88.49,$94)
68 : (1,67.12,$88) (2,63.85,$97) (3,36.80,$59) (4,35.34,$62) (5,31.35,$100) (6,67.26,$32) (7,84.10,$55) (8,21.50,$43) (9,48.89,$27) (10,53.67,$2) (11,57.14,$100)
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)
66 : (1,56.47,$54) (2,37.88,$33) (3,97.80,$28) (4,25.30,$37) (5,18.03,$93) (6,15.50,$86) (7,72.65,$2) (8,76.70,$73) (9,60.99,$16) (10,15.65,$27) (11,8.80,$46) (12,43.89,$13) (13,71.56,$56)
98 : (1,56.54,$99) (2,17.47,$85) (3,54.23,$45) (4,47.80,$77) (5,66.90,$6) (6,11.95,$83) (7,71.67,$38) (8,12.81,$12) (9,37.63,$11)
97 : (1,79.16,$68) (2,46.56,$79) (3,82.45,$11) (4,66.83,$41) (5,16.74,$27) (6,61.20,$12) (7,27.71,$45) (8,22.83,$6) (9,64.77,$87) (10,24.61,$32) (11,59.31,$99) (12,49.72,$66) (13,26.13,$42)
70 : (1,83.39,$15) (2,53.05,$54) (3,40.69,$2) (4,44.64,$2) (5,69.52,$2) (6,75.73,$56) (7,85.78,$65)
63 : (1,72.69,$21) (2,56.98,$1) (3,99.98,$95) (4,52.66,$89) (5,90.79,$98) (6,10.66,$26) (7,25.11,$20) (8,85.89,$46) (9,14.12,$24) (10,64.80,$87) (11,82.36,$64)
97 : (1,60.69,$16) (2,7.20,$73) (3,24.08,$97) (4,10.23,$50) (5,31.00,$53) (6,94.35,$73) (7,35.56,$53) (8,4.94,$36) (9,63.67,$39)
70 : (1,77.24,$69) (2,16.26,$3) (3,47.31,$11) (4,12.29,$93) (5,23.49,$80) (6,43.08,$81) (7,97.06,$97) (8,79.28,$83) (9,42.65,$57)
75 : (1,53.59,$16) (2,37.78,$61) (3,85.42,$38) (4,80.37,$5) (5,98.41,$94)
74 : (1,71.71,$44) (2,29.47,$50) (3,96.58,$8) (4,41.09,$55) (5,52.18,$55) (6,72.24,$91) (7,4.35,$76)
73 : (1,32.06,$17) (2,34.86,$16) (3,46.12,$41) (4,73.48,$75) (5,36.66,$61) (6,51.49,$82) (7,21.95,$62)
Loading

0 comments on commit d85db07

Please sign in to comment.