Skip to content

Instantly share code, notes, and snippets.

@macshome
Created December 5, 2019 00:58
Show Gist options
  • Save macshome/54b721435c27c37caa242d73f4307df1 to your computer and use it in GitHub Desktop.
Save macshome/54b721435c27c37caa242d73f4307df1 to your computer and use it in GitHub Desktop.
Advent of Code 2019 Day 2 - The Ugliness
import Cocoa
typealias IntcodeProgram = [Int]
func loadInput() throws -> IntcodeProgram {
do {
let inputFile = URL(fileReferenceLiteralResourceName: "input.txt")
let rawInput = try String(contentsOf: inputFile)
let splitFile = rawInput.components(separatedBy: .punctuationCharacters)
return splitFile.compactMap { Int($0) }
} catch {
throw error
}
}
var code = try! loadInput()
code[1] = 12
code[2] = 2
var idx = 0
func run(_ code: inout IntcodeProgram) {
let opc = code[idx]
let ebp = code[code[idx + 1]]
let ebx = code[code[idx + 2]]
let edi = code[idx + 3]
switch opc {
case 1:
code[edi] = ebp + ebx
case 2:
code[edi] = ebp * ebx
case 99:
break
default:
return
}
idx += 4
}
while idx <= code.count - 5 {
run(&code)
}
print(code.first!)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment