Last active
December 14, 2019 11:06
-
-
Save Andrewp2/826cb02a432ba5c5531bd95e48335ff4 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| static class Quantity { | |
| public int amount; | |
| public String name; | |
| public int id; | |
| public Quantity(int amount, String name, int id) { | |
| this.amount = amount; | |
| this.name = name; | |
| this.id = id; | |
| } | |
| @Override | |
| public String toString() { | |
| return amount + " " + name; | |
| } | |
| } | |
| static class Recipe { | |
| public ArrayList<Quantity> ingredients = new ArrayList<>(); | |
| public Quantity output; | |
| public Recipe(Quantity output) { | |
| this.output = output; | |
| } | |
| @Override | |
| public String toString() { | |
| StringBuilder sb = new StringBuilder(); | |
| sb.append(ingredients.stream().map(x -> x.toString()).collect(Collectors.joining(", "))); | |
| sb.append(" => "); | |
| sb.append(output.toString()); | |
| return sb.toString(); | |
| } | |
| } | |
| private static void day14Part1() { | |
| try { | |
| BufferedReader br = getBufferedReader("14.txt"); | |
| String line; | |
| int x = 0; | |
| HashMap<String, Integer> map = new HashMap<>(); | |
| HashMap<Integer, Recipe> recipeMap = new HashMap<>(); | |
| while((line = br.readLine()) != null) { | |
| line = line.replaceAll(",", ""); | |
| String[] split = line.split(" "); | |
| ArrayList<Quantity> ingredients = new ArrayList<>(); | |
| for(int i = 0; i < split.length-3;i+=2) { | |
| int amount = Integer.parseInt(split[i]); | |
| if (!map.containsKey(split[i + 1])) { | |
| map.put(split[i+1], x); | |
| x++; | |
| } | |
| int ingredient = map.get(split[i+1]); | |
| ingredients.add(new Quantity(amount, split[i+1], ingredient)); | |
| } | |
| String outputName = split[split.length-1]; | |
| if(!map.containsKey(outputName)) { | |
| map.put(outputName, x); | |
| x++; | |
| } | |
| int outputAmount = Integer.parseInt(split[split.length-2]); | |
| Recipe r = new Recipe(new Quantity(outputAmount, outputName, map.get(outputName))); | |
| r.ingredients = ingredients; | |
| recipeMap.put(r.output.id, r); | |
| } | |
| long[] excess = new long[x]; | |
| long[] currentlyWant = new long[x]; | |
| int m = 0; | |
| long count = 1; | |
| while(currentlyWant[0] <= 1_000_000_000_000L) { | |
| currentlyWant[map.get("FUEL")] = count; | |
| m += count; | |
| //System.out.println(m); | |
| while(!allOre(currentlyWant)) { | |
| int i = 1; | |
| while(currentlyWant[i] <= 0) { | |
| i++; | |
| } | |
| Recipe iReducer = recipeMap.get(i); | |
| ArrayList<Quantity> prerequisites = iReducer.ingredients; | |
| long outputToProduce = currentlyWant[i] - excess[i]; | |
| long productionMultiplier = outputToProduce / iReducer.output.amount + | |
| (outputToProduce % iReducer.output.amount == 0 ? 0 : 1); | |
| for (Quantity q : prerequisites) { | |
| long ingredientNeeded = q.amount * productionMultiplier; | |
| currentlyWant[q.id] += ingredientNeeded; | |
| } | |
| currentlyWant[i] = 0; | |
| excess[i] = productionMultiplier * iReducer.output.amount - outputToProduce; | |
| for(int j = 0; j < excess.length; j++) { | |
| long leftOver = excess[j] - currentlyWant[j]; | |
| if(leftOver >= 0) { | |
| excess[j] = leftOver; | |
| currentlyWant[j] = 0; | |
| } else { | |
| currentlyWant[j] = leftOver * -1; | |
| excess[j] = 0; | |
| } | |
| } | |
| } | |
| } | |
| System.out.println(m); | |
| System.out.println(currentlyWant[0]); | |
| } catch (IOException e) { | |
| e.printStackTrace(); | |
| } | |
| } | |
| private static boolean allOre(long[] want) { | |
| for(int i = 1; i < want.length; i++) { | |
| if(want[i] > 0) { | |
| return false; | |
| } | |
| } | |
| return true; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment