advent-of-code

Perserverance, or the lack thereof

git clone git://git.shimmy1996.com/advent-of-code.git

day-05.jl (2167B)

    1 function computer(program, input)
    2     program = copy(program)
    3     pc = 1
    4     output = []
    5     get_param(loc, im_mode) = (im_mode ? program[loc] : program[program[loc] + 1])
    6     while true
    7         op_code = program[pc] % 100
    8         im_mode_1 = (program[pc] % 1000) ÷ 100 > 0;
    9         im_mode_2 = (program[pc] % 10000) ÷ 1000 > 0;
   10         if op_code == 1
   11             program[program[pc + 3] + 1] = (
   12                 get_param(pc + 1, im_mode_1)
   13                 + get_param(pc + 2, im_mode_2)
   14             )
   15             pc += 4
   16         elseif op_code == 2
   17             program[program[pc + 3] + 1] = (
   18                 get_param(pc + 1, im_mode_1)
   19                 * get_param(pc + 2, im_mode_2)
   20             )
   21             pc += 4
   22         elseif op_code == 3
   23             program[program[pc + 1] + 1] = input
   24             pc += 2
   25         elseif op_code == 4
   26             push!(output, get_param(pc + 1, im_mode_1))
   27             pc += 2
   28         elseif op_code == 5
   29             if get_param(pc + 1, im_mode_1) != 0
   30                 pc = get_param(pc + 2, im_mode_2) + 1
   31             else
   32                 pc += 3;
   33             end
   34         elseif op_code == 6
   35             if get_param(pc + 1, im_mode_1) == 0
   36                 pc = get_param(pc + 2, im_mode_2) + 1
   37             else
   38                 pc += 3;
   39             end
   40         elseif op_code == 7
   41             program[program[pc + 3] + 1] = (
   42                 get_param(pc + 1, im_mode_1)
   43                 < get_param(pc + 2, im_mode_2)
   44             ) ? 1 : 0;
   45             pc += 4;
   46         elseif op_code == 8
   47             program[program[pc + 3] + 1] = (
   48                 get_param(pc + 1, im_mode_1)
   49                 == get_param(pc + 2, im_mode_2)
   50             ) ? 1 : 0;
   51             pc += 4;
   52         elseif op_code == 99
   53             break
   54         else
   55             println("Unknown op code: ", op_code)
   56             break
   57         end
   58     end
   59     output
   60 end
   61 
   62 function part_1(input)
   63     computer(input, 1)[end]
   64 end
   65 
   66 function part_2(input)
   67     computer(input, 5)[end]
   68 end
   69 
   70 input = map(x -> parse(Int32, x), split(readlines(open("input.txt"))[1], ','))
   71 
   72 println("Julia:")
   73 println("Part 1: ", part_1(input))
   74 println("Part 2: ", part_2(input))