Created
December 6, 2020 06:43
-
-
Save bogren/a88a3586f48f5d0b92f0f227fb674998 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
let lines = try | |
String(contentsOfFile: "input", encoding: .utf8) | |
.components(separatedBy: .newlines) | |
.split(separator: "") | |
var part1 = 0 | |
for line in lines { | |
var set = Set<Character>() | |
let answers = line.joined(separator: "") | |
let yes = answers.filter { set.insert($0).inserted }.count | |
part1 += yes | |
} | |
print("Part 1: \(part1)") | |
var part2 = 0 | |
for line in lines { | |
let first = line.first ?? "" | |
let yes = line.reduce(Set(first)) { (result, list) in | |
return result.intersection(list) | |
}.count | |
part2 += yes | |
} | |
print("Part 2: \(part2)") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment