-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaoc2024_day_3.swift
61 lines (48 loc) · 1.38 KB
/
aoc2024_day_3.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
import Foundation
let input =
"""
"""
let strings = input.split(separator: "\n", omittingEmptySubsequences: true).map({ String($0) })
let string = strings.first ?? ""
// PART 1
let regex = try! Regex(#"mul\(\d{1,3},\d{1,3}\)"#)
let result1: Int = string.ranges(of: regex)
.map({
String(string.substring(with: $0))
.replacingOccurrences(of: "mul(", with: "")
.replacingOccurrences(of: ")", with: "")
})
.map({ args in
let split = args.split(separator: ",").compactMap({ Int(String($0)) })
return split[0] * split[1]
})
.reduce(0, { $0 + $1 })
print(result1)
// PART 2
let regex2 = try! Regex(#"mul\(\d{1,3},\d{1,3}\)|don't\(\)|do\(\)"#)
let filtered = string.ranges(of: regex2)
.map({
String(string.substring(with: $0))
.replacingOccurrences(of: "mul(", with: "")
.replacingOccurrences(of: ")", with: "")
})
.map({ args in
if args.contains(",") {
let split = args.split(separator: ",").compactMap({ Int(String($0)) })
return "\(split[0] * split[1])"
} else {
return args
}
})
var result2 = 0
var include = true
for op in filtered {
if op.starts(with: "don") {
include = false
} else if op.starts(with: "do") {
include = true
} else if include {
result2 += (Int(op) ?? 0)
}
}
print(result2)