File tree Expand file tree Collapse file tree 2 files changed +28
-0
lines changed
aoc16.playground/Pages/Day09.xcplaygroundpage Expand file tree Collapse file tree 2 files changed +28
-0
lines changed Original file line number Diff line number Diff line change 3
3
import Foundation
4
4
5
5
part1 ( )
6
+ part2 ( )
6
7
7
8
//: [Next](@next)
Original file line number Diff line number Diff line change @@ -32,3 +32,30 @@ public func part1() -> Int {
32
32
}
33
33
return output. count
34
34
}
35
+
36
+ public func part2( ) -> Int {
37
+ let input = Array ( stringsFromFile ( ) [ 0 ] )
38
+ var result = 0
39
+ var weights = Array ( repeating: 1 , count: input. count)
40
+ var marker : [ Character ] ? = nil
41
+ for (i, ch) in input. enumerated ( ) {
42
+ if let buffer = marker {
43
+ if ch == " ) " {
44
+ let parts = String ( buffer) . split ( separator: " x " )
45
+ marker = nil
46
+ let length = Int ( parts [ 0 ] ) !
47
+ let count = Int ( parts [ 1 ] ) !
48
+ for j in ( i+ 1 ) ... ( i+ length) where j < weights. count {
49
+ weights [ j] *= count
50
+ }
51
+ } else {
52
+ marker? . append ( ch)
53
+ }
54
+ } else if ch == " ( " {
55
+ marker = [ ]
56
+ } else {
57
+ result += weights [ i]
58
+ }
59
+ }
60
+ return result
61
+ }
You can’t perform that action at this time.
0 commit comments