Skip to content

Instantly share code, notes, and snippets.

@bogren
Created December 6, 2020 06:43
Show Gist options
  • Save bogren/a88a3586f48f5d0b92f0f227fb674998 to your computer and use it in GitHub Desktop.
Save bogren/a88a3586f48f5d0b92f0f227fb674998 to your computer and use it in GitHub Desktop.
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