Skip to content

Commit d07379b

Browse files
committed
2016 day 7 part 2
1 parent b39fb59 commit d07379b

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed

aoc16.playground/Pages/Day07.xcplaygroundpage/Contents.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,6 @@
33
import Foundation
44

55
part1()
6+
part2()
67

78
//: [Next](@next)

aoc16.playground/Pages/Day07.xcplaygroundpage/Sources/Day07.swift

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,39 @@ public func part1() -> Int {
3535
}
3636
return result
3737
}
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+
}

0 commit comments

Comments
 (0)