Created
December 5, 2019 00:58
-
-
Save macshome/54b721435c27c37caa242d73f4307df1 to your computer and use it in GitHub Desktop.
Advent of Code 2019 Day 2 - The Ugliness
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
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