2
2
3
3
import java .io .IOException ;
4
4
import java .util .Arrays ;
5
- import java .util .Map .Entry ;
6
5
7
6
import com .sbaars .adventofcode2019 .common .Day ;
8
7
import com .sbaars .adventofcode2019 .util .CountMap ;
@@ -15,7 +14,9 @@ public static void main(String[] args) throws IOException {
15
14
@ Override
16
15
public Object part1 () throws IOException {
17
16
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 ;
19
20
//return Arrays.stream(trades).map(e -> e.output.item).distinct().count();
20
21
/*CountMap<String> items = new CountMap<>();
21
22
for(int i = 13311; i<15000; i++) {
@@ -35,37 +36,34 @@ private Trade getTrade(Trade[] trades, String key) {
35
36
return Arrays .stream (trades ).filter (e -> e .output .item .equals (key )).findAny ().get ();
36
37
}
37
38
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 );
57
44
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 );
63
60
}
64
- if(canMakeFuel(trades, newItems))
65
- return true;
66
61
}
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 ;
69
67
}
70
68
71
69
@ Override
@@ -74,25 +72,18 @@ public Object part2() throws IOException {
74
72
}
75
73
76
74
class Trade {
77
- CountMap < String > input = new CountMap <>() ;
78
- Item output ;
75
+ final Item [] input ;
76
+ final Item output ;
79
77
80
78
public Trade (String trade ) {
81
79
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 );
83
81
output = new Item (inputOutput [1 ]);
84
82
}
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 + "]" ;
96
87
}
97
88
}
98
89
@@ -110,5 +101,10 @@ public Item(int i, String string) {
110
101
amount = i ;
111
102
item = string ;
112
103
}
104
+
105
+ @ Override
106
+ public String toString () {
107
+ return "Item [amount=" + amount + ", item=" + item + "]" ;
108
+ }
113
109
}
114
110
}
0 commit comments