-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaoc2024_day_17.swift
124 lines (97 loc) · 2.81 KB
/
aoc2024_day_17.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
import Foundation
import UIKit
class ViewController: UIViewController {
let input =
"""
Register A: 24847151
Register B: 0
Register C: 0
Program: 2,4,1,5,7,5,1,6,0,3,4,0,5,5,3,0
"""
let testInput =
"""
Register A: 2024
Register B: 0
Register C: 0
Program: 0,3,5,4,3,0
"""
override func viewDidLoad() {
super.viewDidLoad()
parse()
part1()
part2()
}
var strings: [String] = []
var A = 0
var B = 0
var C = 0
var program: [Int] = []
func parse() {
// strings = testInput.split(separator: "\n", omittingEmptySubsequences: true)
strings = input.split(separator: "\n", omittingEmptySubsequences: true)
.map({ String($0) })
A = Int(strings[0].split(separator: "Register A: ")[0])!
B = Int(strings[1].split(separator: "Register B: ")[0])!
C = Int(strings[2].split(separator: "Register C: ")[0])!
program = strings[3].split(separator: "Program: ")[0].split(separator: ",").map({ Int($0)! })
}
func comboOperand(code: Int) -> Int {
switch code {
case 0, 1, 2, 3:
return code
case 4:
return A
case 5:
return B
case 6:
return C
default:
return 0 // error
}
}
var instructionPointer = 0
var output: [Int] = []
func operation(operand: Int, code: Int) {
switch code {
case 0: // adv
let result = Int(Double(A) / Double( 2 << (comboOperand(code: operand) - 1)))
A = result
case 1: // bxl (xor)
let result = B ^ operand
B = result
case 2: // bst
let result = comboOperand(code: operand) % 8
B = result
case 3: // jnz
if A == 0 { break }
instructionPointer = operand
return
case 4: // bxc
let result = B ^ C
B = result
case 5: // out
let result = comboOperand(code: operand) % 8
output.append(result)
case 6: // bdv
let result = Int(Double(A) / Double( 2 << (comboOperand(code: operand) - 1)))
B = result
case 7: // cdv
let result = Int(Double(A) / Double( 2 << (comboOperand(code: operand) - 1)))
C = result
default:
return
}
instructionPointer += 2
}
func part1() {
while instructionPointer < program.count - 1 {
let code = program[instructionPointer]
let operand = program[instructionPointer + 1]
operation(operand: operand, code: code)
}
print(output.map({ String($0) }).joined(separator: ","))
}
func part2() {
// sequences and observations
}
}