Skip to content

Commit e145e10

Browse files
committed
Skip on leftover
1 parent 2f1fc48 commit e145e10

File tree

2 files changed

+52
-44
lines changed

2 files changed

+52
-44
lines changed

src/main/java/com/sbaars/adventofcode2019/days/Day14.java

Lines changed: 40 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import java.io.IOException;
44
import java.util.Arrays;
5-
import java.util.Map.Entry;
65

76
import com.sbaars.adventofcode2019.common.Day;
87
import com.sbaars.adventofcode2019.util.CountMap;
@@ -15,7 +14,9 @@ public static void main(String[] args) throws IOException {
1514
@Override
1615
public Object part1() throws IOException {
1716
Trade[] trades = Arrays.stream(readDay(15).split(System.lineSeparator())).map(Trade::new).toArray(Trade[]::new);
18-
return findCost(trades, getTrade(trades, "FUEL"), 1);
17+
int cost = findCost(trades, new Item(1, "FUEL"), new CountMap<>());
18+
System.out.println(created);
19+
return cost;
1920
//return Arrays.stream(trades).map(e -> e.output.item).distinct().count();
2021
/*CountMap<String> items = new CountMap<>();
2122
for(int i = 13311; i<15000; i++) {
@@ -35,37 +36,34 @@ private Trade getTrade(Trade[] trades, String key) {
3536
return Arrays.stream(trades).filter(e -> e.output.item.equals(key)).findAny().get();
3637
}
3738

38-
private int findCost(Trade[] trades, Trade fuelTrade, int nNeeded) {
39-
int timesApplied = (int)Math.ceil((double)nNeeded/(double)fuelTrade.output.amount);
40-
int totalCost = 0;
41-
for(Entry<String, Integer> cost : fuelTrade.input.entrySet()) {
42-
if(cost.getKey().equals("ORE")) {
43-
totalCost+=cost.getValue();
44-
} else {
45-
totalCost+=findCost(trades, getTrade(trades, cost.getKey()), cost.getValue());
46-
}
47-
}
48-
System.out.println(fuelTrade.output.item+" costs "+totalCost+" times "+timesApplied+" needed "+nNeeded);
49-
return totalCost * timesApplied;
50-
}
51-
52-
private boolean canMakeFuel(Trade[] trades, CountMap<String> items) {
53-
//System.out.println(items);
54-
//System.out.println("----");
55-
//Trade fuelTrade = Arrays.stream(trades).filter(e -> e.output.item.equals("FUEL")).findAny().get();
56-
//findCost
39+
CountMap<String> created = new CountMap<>();
40+
private int findCost(Trade[] trades, Item buyingItem, CountMap<String> leftOver) {
41+
Trade fuelTrade = getTrade(trades, buyingItem.item);
42+
int timesApplied = (int)Math.ceil((double)buyingItem.amount/(double)fuelTrade.output.amount);
43+
leftOver.increment(fuelTrade.output.item, buyingItem.amount % fuelTrade.output.amount);
5744

58-
/*for(Trade trade : trades) {
59-
CountMap<String> newItems = new CountMap<>(items);
60-
if(trade.perform(newItems)) {
61-
if(trade.output.item.equals("FUEL")) {
62-
return true;
45+
//System.out.println(fuelTrade.output.item+" nLeftOver "+leftOver.get(fuelTrade.output.item));
46+
int totalCost = 0;
47+
for(int i = 0; i<timesApplied; i++) {
48+
for(Item cost : fuelTrade.input) {
49+
if(leftOver.get(cost.item) >= cost.amount) {
50+
leftOver.increment(fuelTrade.output.item, -fuelTrade.output.amount);
51+
//skip = fuelTrade.output.item;
52+
System.out.println("Enough "+fuelTrade.output.item+" LEFTOVER!");
53+
continue;
54+
}
55+
if(cost.item.equals("ORE")) {
56+
totalCost+=cost.amount;
57+
System.out.println("Spend "+cost.amount+" ORE to get "+fuelTrade.output.amount+" "+fuelTrade.output.item);
58+
} else {
59+
totalCost+=findCost(trades, new Item(cost.amount, cost.item), leftOver);
6360
}
64-
if(canMakeFuel(trades, newItems))
65-
return true;
6661
}
67-
}*/
68-
return false;
62+
created.increment(buyingItem.item, fuelTrade.output.amount);
63+
}
64+
System.out.println("Bought "+(timesApplied*fuelTrade.output.amount)+" "+buyingItem.item+" for "+totalCost);
65+
//System.out.println(fuelTrade.output.item+" costs "+totalCost+" times "+timesApplied);
66+
return totalCost;
6967
}
7068

7169
@Override
@@ -74,25 +72,18 @@ public Object part2() throws IOException {
7472
}
7573

7674
class Trade {
77-
CountMap<String> input = new CountMap<>();
78-
Item output;
75+
final Item[] input;
76+
final Item output;
7977

8078
public Trade(String trade) {
8179
String[] inputOutput = trade.split(" => ");
82-
Arrays.stream(inputOutput[0].split(", ")).map(Item::new).forEach(e -> input.increment(e.item, e.amount));
80+
input = Arrays.stream(inputOutput[0].split(", ")).map(Item::new).toArray(Item[]::new);
8381
output = new Item(inputOutput[1]);
8482
}
85-
86-
public boolean perform(CountMap<String> items) {
87-
for(Entry<String, Integer> item : input.entrySet()) {
88-
if(!items.containsKey(item.getKey()) || items.get(item.getKey()) < item.getValue())
89-
return false;
90-
else {
91-
items.increment(item.getKey(), -item.getValue());
92-
}
93-
}
94-
items.increment(output.item, output.amount);
95-
return true;
83+
84+
@Override
85+
public String toString() {
86+
return "Trade [input=" + Arrays.toString(input) + ", output=" + output + "]";
9687
}
9788
}
9889

@@ -110,5 +101,10 @@ public Item(int i, String string) {
110101
amount = i;
111102
item = string;
112103
}
104+
105+
@Override
106+
public String toString() {
107+
return "Item [amount=" + amount + ", item=" + item + "]";
108+
}
113109
}
114110
}

src/main/resources/day16.txt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
2 VPVL, 7 FWMGM, 2 CXFTF, 11 MNCFX => 1 STKFG
2+
17 NVRVD, 3 JNWZP => 8 VPVL
3+
53 STKFG, 6 MNCFX, 46 VJHF, 81 HVMC, 68 CXFTF, 25 GNMV => 1 FUEL
4+
22 VJHF, 37 MNCFX => 5 FWMGM
5+
139 ORE => 4 NVRVD
6+
144 ORE => 7 JNWZP
7+
5 MNCFX, 7 RFSQX, 2 FWMGM, 2 VPVL, 19 CXFTF => 3 HVMC
8+
5 VJHF, 7 MNCFX, 9 VPVL, 37 CXFTF => 6 GNMV
9+
145 ORE => 6 MNCFX
10+
1 NVRVD => 8 CXFTF
11+
1 VJHF, 6 MNCFX => 4 RFSQX
12+
176 ORE => 6 VJHF

0 commit comments

Comments
 (0)