From: Jack Kinsey Date: Sun, 22 Dec 2019 06:34:59 +0000 (-0500) Subject: Add extremely broken day14pt2 X-Git-Url: http://git.jkinsey.net/?a=commitdiff_plain;h=refs%2Fheads%2Fmaster;p=adventofcode2019.git Add extremely broken day14pt2 I'm sorry I just gave up --- diff --git a/src/adventofcode2019/day14.clj b/src/adventofcode2019/day14.clj index e2aae3e..8310b5e 100644 --- a/src/adventofcode2019/day14.clj +++ b/src/adventofcode2019/day14.clj @@ -16,10 +16,11 @@ (map parse-reaction input))) (defn reduce-reaction [graph producing consuming] - (let [[inputs moles] (graph producing) + (let [consuming (into #{} (if (coll? consuming) consuming [consuming])) + [inputs moles] (graph producing) in-chems (keys inputs) replace-term (fn [chem] - (if (or (= chem consuming) + (if (or (contains? consuming chem) (neg? (second (graph chem)))) {chem (inputs chem)} (let [ch-req (inputs chem) @@ -27,7 +28,7 @@ conversion (int (Math/ceil (/ ch-req ch-mo)))] (assoc (mmap (partial * conversion) ch-in) chem (- ch-req (* ch-mo conversion)))))) - check-reduced (fn [[ch ct]] (and (not= ch consuming) (pos? ct))) + check-reduced (fn [[ch ct]] (and (not (contains? consuming ch)) (pos? ct))) reduced-inputs (reduce (partial merge-with +) (map replace-term in-chems)) new-graph (assoc graph producing [reduced-inputs moles])] @@ -39,7 +40,31 @@ (get-in (reduce-reaction graph producing consuming) [producing 0 consuming])) +(defn produce-with [graph producing consuming cargo] + (println graph producing consuming cargo) + (let [cheapest-prod (get-in graph [producing 0]) + exchange-rate (cheapest-prod consuming) + base-times (quot cargo exchange-rate) + extra-cargo (mod cargo exchange-rate) + extra-resources (assoc (->> cheapest-prod + (filter (comp neg? second)) + (map (fn [[chem mole]] + [chem (* -1 base-times mole)])) + (into {})) consuming extra-cargo) + rewrite-with (into #{} (keys extra-resources)) + second-chance (reduce-reaction graph producing rewrite-with) + recursive-times (map (fn [[chem ct]] + (let [er (- (get-in second-chance [producing 0 chem]))] + (println chem ct er) + (quot ct er))) + extra-resources)] + recursive-times)) + +(defn maximize-output [graph producing consuming cargo] + (produce-with (reduce-reaction graph producing consuming) + producing consuming cargo)) + (defn day14 [] (let [graphed (make-graph (get-list-from-file (input-file)))] (part1 (find-lowest-exchange-rate graphed "FUEL" "ORE")) - #_(part2))) + (part2 (maximize-output graphed "FUEL" "ORE" 1000000000000)))) diff --git a/test/adventofcode2019/day14_test.clj b/test/adventofcode2019/day14_test.clj index 7e99dcc..c791164 100644 --- a/test/adventofcode2019/day14_test.clj +++ b/test/adventofcode2019/day14_test.clj @@ -23,12 +23,9 @@ "AB" [{"A" 3, "B" 4} 1] "BC" [{"B" 5, "C" 7} 1] "CA" [{"C" 4, "A" 1} 1] - "FUEL" [{"ORE" 165, "A" 0 - "B" -1, "AB" 0 - "C" -3, "BC" 0 - "CA" 0} 1]}))) + "FUEL" [{"ORE" 165, "A" 0 "B" -1, "AB" 0 "C" -3, "BC" 0 "CA" 0} 1]}))) -(deftest examples +(deftest examples-part1 (is (= 31 (find-lowest-exchange-rate (make-graph ["10 ORE => 10 A" "1 ORE => 1 B" "7 A, 1 B => 1 C" @@ -85,3 +82,46 @@ "7 XCVML => 6 RJRHP" "5 BHXH, 4 VRPVC => 5 LTCX"]) "FUEL", "ORE")))) + +(deftest examples-part2 + (is (= 82892753 (maximize-output (make-graph ["157 ORE => 5 NZVS" + "165 ORE => 6 DCFZ" + "44 XJWVT, 5 KHKGT, 1 QDVJ, 29 NZVS, 9 GPVTF, 48 HKGWZ => 1 FUEL" + "12 HKGWZ, 1 GPVTF, 8 PSHF => 9 QDVJ" + "179 ORE => 7 PSHF" + "177 ORE => 5 HKGWZ" + "7 DCFZ, 7 PSHF => 2 XJWVT" + "165 ORE => 2 GPVTF" + "3 DCFZ, 7 NZVS, 5 HKGWZ, 10 PSHF => 8 KHKGT"]) + "FUEL" "ORE" 1000000000000))) + (is (= 5586022 (maximize-output (make-graph ["2 VPVL, 7 FWMGM, 2 CXFTF, 11 MNCFX => 1 STKFG" + "17 NVRVD, 3 JNWZP => 8 VPVL" + "53 STKFG, 6 MNCFX, 46 VJHF, 81 HVMC, 68 CXFTF, 25 GNMV => 1 FUEL" + "22 VJHF, 37 MNCFX => 5 FWMGM" + "139 ORE => 4 NVRVD" + "144 ORE => 7 JNWZP" + "5 MNCFX, 7 RFSQX, 2 FWMGM, 2 VPVL, 19 CXFTF => 3 HVMC" + "5 VJHF, 7 MNCFX, 9 VPVL, 37 CXFTF => 6 GNMV" + "145 ORE => 6 MNCFX" + "1 NVRVD => 8 CXFTF" + "1 VJHF, 6 MNCFX => 4 RFSQX" + "176 ORE => 6 VJHF"]) + "FUEL", "ORE" 1000000000000))) + (is (= 460664 (maximize-output (make-graph ["171 ORE => 8 CNZTR" + "7 ZLQW, 3 BMBT, 9 XCVML, 26 XMNCP, 1 WPTQ, 2 MZWV, 1 RJRHP => 4 PLWSL" + "114 ORE => 4 BHXH" + "14 VRPVC => 6 BMBT" + "6 BHXH, 18 KTJDG, 12 WPTQ, 7 PLWSL, 31 FHTLT, 37 ZDVW => 1 FUEL" + "6 WPTQ, 2 BMBT, 8 ZLQW, 18 KTJDG, 1 XMNCP, 6 MZWV, 1 RJRHP => 6 FHTLT" + "15 XDBXC, 2 LTCX, 1 VRPVC => 6 ZLQW" + "13 WPTQ, 10 LTCX, 3 RJRHP, 14 XMNCP, 2 MZWV, 1 ZLQW => 1 ZDVW" + "5 BMBT => 4 WPTQ" + "189 ORE => 9 KTJDG" + "1 MZWV, 17 XDBXC, 3 XCVML => 2 XMNCP" + "12 VRPVC, 27 CNZTR => 2 XDBXC" + "15 KTJDG, 12 BHXH => 5 XCVML" + "3 BHXH, 2 VRPVC => 7 MZWV" + "121 ORE => 7 VRPVC" + "7 XCVML => 6 RJRHP" + "5 BHXH, 4 VRPVC => 5 LTCX"]) + "FUEL" "ORE" 1000000000000))))