Skip to content

Instantly share code, notes, and snippets.

@SaschaMann
Created December 2, 2019 07:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SaschaMann/27f184b4105663b9dbca6ee226d8af7e to your computer and use it in GitHub Desktop.
Save SaschaMann/27f184b4105663b9dbca6ee226d8af7e to your computer and use it in GitHub Desktop.
using OffsetArrays
using Test
const input = parse.(Int, split(read("2_input", String), ','))
const instructions = Dict(
1 => +,
2 => *,
)
function main(input)
intcode = OffsetVector(copy(input), 0:length(input) - 1)
# 1202 program alarm
intcode[1] = 12
intcode[2] = 2
i = 0
while intcode[i] != 99
intcode[intcode[i + 3]] = instructions[intcode[i]](intcode[intcode[i + 1]], intcode[intcode[i + 2]])
i += 4
end
return intcode
end
function tests()
@test main([1,0,0,0,99]) == [2,0,0,0,99]
@test main([2,3,0,3,99]) == [2,3,0,6,99]
@test main([2,4,4,5,99,0]) == [2,4,4,5,99,9801]
@test main([1,1,1,4,99,5,6,0,99]) == [30,1,1,4,2,5,6,0,99]
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment