Created
December 2, 2019 07:58
-
-
Save SaschaMann/27f184b4105663b9dbca6ee226d8af7e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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