File tree 2 files changed +37
-0
lines changed
aoc16.playground/Pages/Day07.xcplaygroundpage 2 files changed +37
-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 @@ -35,3 +35,39 @@ public func part1() -> Int {
35
35
}
36
36
return result
37
37
}
38
+
39
+ public func part2( ) -> Int {
40
+ var result = 0
41
+ for line in stringsFromFile ( ) where line != " " {
42
+ var square = false
43
+ var aba = Set < String > ( )
44
+ var bab = Set < String > ( )
45
+ var buffer = [ Character] ( )
46
+
47
+ for ch in line {
48
+ if square && ch == " ] " {
49
+ buffer = [ ]
50
+ square = false
51
+ } else if !square && ch == " [ " {
52
+ buffer = [ ]
53
+ square = true
54
+ } else {
55
+ buffer. append ( ch)
56
+ if buffer. count == 3 {
57
+ if buffer [ 0 ] != buffer [ 1 ] && buffer [ 0 ] == buffer [ 2 ] {
58
+ if square {
59
+ aba. insert ( String ( buffer. dropFirst ( 1 ) ) )
60
+ } else {
61
+ bab. insert ( String ( buffer. dropLast ( 1 ) ) )
62
+ }
63
+ }
64
+ buffer. remove ( at: 0 )
65
+ }
66
+ }
67
+ }
68
+ if !aba. isEmpty && !aba. intersection ( bab) . isEmpty {
69
+ result += 1
70
+ }
71
+ }
72
+ return result
73
+ }
You can’t perform that action at this time.
0 commit comments