commit 2b264a46cf3d0f6d1da8424a4ceb8f64602b2134
parent 45ade4d2a26c7d068bd599c6adbcf1e9aec96a8a
Author: Shimmy Xu <shimmy.xu@shimmy1996.com>
Date: Thu, 5 Dec 2019 09:58:10 -0500
Add Julia solution for day 05
Diffstat:
A | day-05/day-05.jl | | | 74 | ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
1 file changed, 74 insertions(+), 0 deletions(-)
diff --git a/day-05/day-05.jl b/day-05/day-05.jl
@@ -0,0 +1,74 @@
+function computer(program, input)
+ program = copy(program)
+ pc = 1
+ output = []
+ get_param(loc, im_mode) = (im_mode ? program[loc] : program[program[loc] + 1])
+ while true
+ op_code = program[pc] % 100
+ im_mode_1 = (program[pc] % 1000) ÷ 100 > 0;
+ im_mode_2 = (program[pc] % 10000) ÷ 1000 > 0;
+ if op_code == 1
+ program[program[pc + 3] + 1] = (
+ get_param(pc + 1, im_mode_1)
+ + get_param(pc + 2, im_mode_2)
+ )
+ pc += 4
+ elseif op_code == 2
+ program[program[pc + 3] + 1] = (
+ get_param(pc + 1, im_mode_1)
+ * get_param(pc + 2, im_mode_2)
+ )
+ pc += 4
+ elseif op_code == 3
+ program[program[pc + 1] + 1] = input
+ pc += 2
+ elseif op_code == 4
+ push!(output, get_param(pc + 1, im_mode_1))
+ pc += 2
+ elseif op_code == 5
+ if get_param(pc + 1, im_mode_1) != 0
+ pc = get_param(pc + 2, im_mode_2) + 1
+ else
+ pc += 3;
+ end
+ elseif op_code == 6
+ if get_param(pc + 1, im_mode_1) == 0
+ pc = get_param(pc + 2, im_mode_2) + 1
+ else
+ pc += 3;
+ end
+ elseif op_code == 7
+ program[program[pc + 3] + 1] = (
+ get_param(pc + 1, im_mode_1)
+ < get_param(pc + 2, im_mode_2)
+ ) ? 1 : 0;
+ pc += 4;
+ elseif op_code == 8
+ program[program[pc + 3] + 1] = (
+ get_param(pc + 1, im_mode_1)
+ == get_param(pc + 2, im_mode_2)
+ ) ? 1 : 0;
+ pc += 4;
+ 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, 5)[end]
+end
+
+input = map(x -> parse(Int32, x), split(readlines(open("input.txt"))[1], ','))
+
+println("Julia:")
+println("Part 1: ", part_1(input))
+println("Part 2: ", part_2(input))