advent-of-code

Perserverance, or the lack thereof

git clone git://git.shimmy1996.com/advent-of-code.git
commit 5ffcef53935f6e2a70deb9079ba98475de33c812
parent 4452b2b72d241473adeab7773476091032cbee92
Author: Shimmy Xu <shimmy.xu@shimmy1996.com>
Date:   Mon,  9 Dec 2019 09:36:08 -0500

Add Julia solution for day 09

Diffstat:
Aday-09/day-09.jl | 101+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 101 insertions(+), 0 deletions(-)
diff --git a/day-09/day-09.jl b/day-09/day-09.jl
@@ -0,0 +1,101 @@
+function computer(program, input)
+    program = Dict(enumerate(copy(program)))
+    output = []
+    pc = 1
+    relative_base = 0
+    function get_param(loc, mode)
+        # Loc is gauranteed to exist.
+        if mode == 0
+            # Position mode.
+            get(program, program[loc] + 1, 0)
+        elseif mode == 1
+            # Immediate mode.
+            program[loc]
+        else
+            # Relative mode.
+            get(program, relative_base + program[loc] + 1, 0)
+        end
+    end
+    function get_res_loc(loc, mode)
+        # Loc is gauranteed to exist.
+        if mode == 0
+            # Position mode.
+            program[loc] + 1
+        else
+            # Relative mode.
+            relative_base + program[loc] + 1
+        end
+    end
+    while true
+        op_code = program[pc] % 100
+        mode_1 = (program[pc] % 1000) ÷ 100
+        mode_2 = (program[pc] % 10000) ÷ 1000
+        mode_3 = (program[pc] % 100000) ÷ 10000
+        if op_code == 1
+            program[get_res_loc(pc + 3, mode_3)] = (
+                get_param(pc + 1, mode_1)
+                + get_param(pc + 2, mode_2)
+            )
+            pc += 4
+        elseif op_code == 2
+            program[get_res_loc(pc + 3, mode_3)] = (
+                get_param(pc + 1, mode_1)
+                * get_param(pc + 2, mode_2)
+            )
+            pc += 4
+        elseif op_code == 3
+            program[get_res_loc(pc + 1, mode_1)] = input
+            pc += 2
+        elseif op_code == 4
+            push!(output, get_param(pc + 1, mode_1))
+            pc += 2
+        elseif op_code == 5
+            if get_param(pc + 1, mode_1) != 0
+                pc = get_param(pc + 2, mode_2) + 1
+            else
+                pc += 3
+            end
+        elseif op_code == 6
+            if get_param(pc + 1, mode_1) == 0
+                pc = get_param(pc + 2, mode_2) + 1
+            else
+                pc += 3
+            end
+        elseif op_code == 7
+            program[get_res_loc(pc + 3, mode_3)] = (
+                get_param(pc + 1, mode_1)
+                < get_param(pc + 2, mode_2)
+            ) ? 1 : 0
+            pc += 4
+        elseif op_code == 8
+            program[get_res_loc(pc + 3, mode_3)] = (
+                get_param(pc + 1, mode_1)
+                == get_param(pc + 2, mode_2)
+            ) ? 1 : 0
+            pc += 4
+        elseif op_code == 9
+            relative_base += get_param(pc + 1, mode_1)
+            pc += 2
+        elseif op_code == 99
+            break
+        else
+            println("Unknown op code: ", op_code)
+            break
+        end
+    end
+    output
+end
+
+function part_1(input)
+    computer(input, 1)[end]
+end
+
+function part_2(input)
+    computer(input, 2)[end]
+end
+
+input = map(x -> parse(Int128, x), split(readlines(open("input.txt"))[1], ','))
+
+println("Julia:")
+println("Part 1: ", part_1(input))
+println("Part 2: ", part_2(input))