advent-of-code

Perserverance, or the lack thereof

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

day-09.jl (2887B)

    1 function computer(program, input)
    2     program = Dict(enumerate(copy(program)))
    3     output = []
    4     pc = 1
    5     relative_base = 0
    6     function get_param(loc, mode)
    7         # Loc is gauranteed to exist.
    8         if mode == 0
    9             # Position mode.
   10             get(program, program[loc] + 1, 0)
   11         elseif mode == 1
   12             # Immediate mode.
   13             program[loc]
   14         else
   15             # Relative mode.
   16             get(program, relative_base + program[loc] + 1, 0)
   17         end
   18     end
   19     function get_res_loc(loc, mode)
   20         # Loc is gauranteed to exist.
   21         if mode == 0
   22             # Position mode.
   23             program[loc] + 1
   24         else
   25             # Relative mode.
   26             relative_base + program[loc] + 1
   27         end
   28     end
   29     while true
   30         op_code = program[pc] % 100
   31         mode_1 = (program[pc] % 1000) ÷ 100
   32         mode_2 = (program[pc] % 10000) ÷ 1000
   33         mode_3 = (program[pc] % 100000) ÷ 10000
   34         if op_code == 1
   35             program[get_res_loc(pc + 3, mode_3)] = (
   36                 get_param(pc + 1, mode_1)
   37                 + get_param(pc + 2, mode_2)
   38             )
   39             pc += 4
   40         elseif op_code == 2
   41             program[get_res_loc(pc + 3, mode_3)] = (
   42                 get_param(pc + 1, mode_1)
   43                 * get_param(pc + 2, mode_2)
   44             )
   45             pc += 4
   46         elseif op_code == 3
   47             program[get_res_loc(pc + 1, mode_1)] = input
   48             pc += 2
   49         elseif op_code == 4
   50             push!(output, get_param(pc + 1, mode_1))
   51             pc += 2
   52         elseif op_code == 5
   53             if get_param(pc + 1, mode_1) != 0
   54                 pc = get_param(pc + 2, mode_2) + 1
   55             else
   56                 pc += 3
   57             end
   58         elseif op_code == 6
   59             if get_param(pc + 1, mode_1) == 0
   60                 pc = get_param(pc + 2, mode_2) + 1
   61             else
   62                 pc += 3
   63             end
   64         elseif op_code == 7
   65             program[get_res_loc(pc + 3, mode_3)] = (
   66                 get_param(pc + 1, mode_1)
   67                 < get_param(pc + 2, mode_2)
   68             ) ? 1 : 0
   69             pc += 4
   70         elseif op_code == 8
   71             program[get_res_loc(pc + 3, mode_3)] = (
   72                 get_param(pc + 1, mode_1)
   73                 == get_param(pc + 2, mode_2)
   74             ) ? 1 : 0
   75             pc += 4
   76         elseif op_code == 9
   77             relative_base += get_param(pc + 1, mode_1)
   78             pc += 2
   79         elseif op_code == 99
   80             break
   81         else
   82             println("Unknown op code: ", op_code)
   83             break
   84         end
   85     end
   86     output
   87 end
   88 
   89 function part_1(input)
   90     computer(input, 1)[end]
   91 end
   92 
   93 function part_2(input)
   94     computer(input, 2)[end]
   95 end
   96 
   97 input = map(x -> parse(Int128, x), split(readlines(open("input.txt"))[1], ','))
   98 
   99 println("Julia:")
  100 println("Part 1: ", part_1(input))
  101 println("Part 2: ", part_2(input))