advent-of-code

Perserverance, or the lack thereof

git clone git://git.shimmy1996.com/advent-of-code.git
commit 5ade4df40362d4d4ccfb5b11e34cdea3bc8a230c
parent 2c5197e02c9b7431c42a39368b0d5fc7822ec901
Author: Shimmy Xu <shimmy.xu@shimmy1996.com>
Date:   Sat, 14 Dec 2019 10:42:10 -0500

Add Julia solution to day 14

Diffstat:
Aday-14/Makefile | 9+++++++++
Aday-14/day-14.jl | 70++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Aday-14/input.txt | 60++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
3 files changed, 139 insertions(+), 0 deletions(-)
diff --git a/day-14/Makefile b/day-14/Makefile
@@ -0,0 +1,9 @@
+CC := g++
+CCFLAGS := --std=c++17 -Wall
+
+all: day-14-rust day-14.jl
+	julia day-14.jl
+	./day-14-rust
+
+day-14-rust: day-14.rs
+	rustc $^ -o $@
diff --git a/day-14/day-14.jl b/day-14/day-14.jl
@@ -0,0 +1,70 @@
+function calc_fuel_cost(recipes, fuel_count)
+    chemicals = Dict("FUEL" => fuel_count)
+    leftovers = Dict()
+    ore_count = 0
+    while length(chemicals) != 0
+        target, amount = pop!(chemicals)
+        if target == "ORE"
+            ore_count += amount
+            continue
+        end
+        # Check leftovers before commiting to reaction.
+        leftover_amount = get!(leftovers, target, 0)
+        leftovers[target] -= min(amount, leftover_amount)
+        amount -= min(amount, leftover_amount)
+        if amount == 0
+            continue
+        end
+        # Initiate new reaction.
+        recipe = recipes[target]
+        reaction_count = Int(ceil(amount / recipe[target]))
+        for (k, v) in pairs(recipe)
+            if k != target
+                chemicals[k] = get(chemicals, k, 0) + v * reaction_count
+            else
+                # Place excess product in leftovers.
+                leftovers[target] = (
+                    get(leftovers, target, 0)
+                    + v * reaction_count - amount
+                )
+            end
+        end
+    end
+    ore_count
+end
+
+function part_1(input)
+    calc_fuel_cost(input, 1)
+end
+
+function part_2(input)
+    # Binary search.
+    ore_cargo = 1000000000000
+    lo = 1
+    hi = ore_cargo
+    while lo + 1 < hi
+        mi = lo + (hi - lo) ÷ 2
+        if calc_fuel_cost(input, mi) > ore_cargo
+            hi = mi
+        else
+            lo = mi
+        end
+    end
+    lo
+end
+
+input = map(readlines(open("input.txt"))) do line
+    material, product = match(r"(.*) => (.*)", line).captures
+    reaction = Dict()
+    material = map(split(material, ", ")) do term
+        num, name = split(term, " ")
+        reaction[name] = parse(Int, num)
+    end
+    num, name = split(product, " ")
+    reaction[name] = parse(Int, num)
+    name => reaction
+end |> Dict
+
+println("Julia:")
+println("Part 1: ", part_1(input))
+println("Part 2: ", part_2(input))
diff --git a/day-14/input.txt b/day-14/input.txt
@@ -0,0 +1,60 @@
+2 LFPRM, 4 GPNQ => 2 VGZVD
+1 KXFHM, 14 SJLP => 8 MGRTM
+2 HBXVT, 3 HNHC, 5 BDLV => 1 DKTW
+2 MGRTM, 8 RVTB => 4 DFMW
+2 SJLP => 9 PXTS
+1 NXBG => 6 FXBXZ
+32 LPSQ => 9 GSDXD
+13 LZGTR => 4 ZRMJ
+1 FTPQ, 16 CPCS => 5 HNHC
+2 THQH, 2 NDJG, 5 MSKT => 4 LRZV
+2 BDLV, 9 HBXVT, 21 NXBG => 7 PLRK
+16 LNSKQ, 41 KXFHM, 1 DKTW, 1 NCPSZ, 3 ZCSB, 11 MGRTM, 19 WNJWP, 11 KRBG => 1 FUEL
+5 FTPQ, 1 HBXVT => 4 BDLV
+15 LSDX, 1 GFJW, 1 QDHJT => 4 NKHQV
+9 CZHTP, 1 FRPTK => 6 SNBS
+17 LFLVS, 2 WCFT => 8 KGJQ
+6 CMHLP => 1 SJLP
+144 ORE => 3 KQKXZ
+3 GFJW, 1 RVTB, 1 GPNQ => 2 NXBG
+4 BDLV => 5 CMHLP
+2 LSDX => 1 LZGTR
+156 ORE => 3 NDJG
+136 ORE => 8 MSKT
+4 BDLV, 1 NKHQV, 1 RVTB => 7 LNSKQ
+1 LRZV, 3 WCFT => 2 HBXVT
+5 KGJQ, 1 SWBSN => 7 QH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