-
Notifications
You must be signed in to change notification settings - Fork 0
/
Solution_2019_02.swift
45 lines (36 loc) · 1.14 KB
/
Solution_2019_02.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import Foundation
struct Solution_2019_02: Solution {
var input: Input
func run() throws {
let values = try input.get()
.split(separator: ",")
.compactMap({ Int($0) })
// ------- Part 1 -------
func run(program: [Int], _ noun: Int, _ verb: Int) -> Int {
var program = program
program[1] = noun
program[2] = verb
let intcode = Intcode(program: program)
intcode.run()
return intcode.readMemory(at: 0)
}
let part1 = run(program: values, 12, 2)
print(part1)
// ------- Part 2 -------
let desiredOutput = 19690720
var result = (0, 0)
findSolution: for noun in 0...99 {
for verb in 0...99 {
if run(program: values, noun, verb) == desiredOutput {
result = (noun, verb)
break findSolution
}
}
}
let part2 = result.0 * 100 + result.1
print(part2)
// ------- Test -------
assert(part1 == 2890696, "WA")
assert(part2 == 8226, "WA")
}
}