Skip to content

Instantly share code, notes, and snippets.

@felix-larsen
Created December 16, 2020 09:10
Show Gist options
  • Save felix-larsen/28191b46c28cb850de484a13e4ad7afa to your computer and use it in GitHub Desktop.
Save felix-larsen/28191b46c28cb850de484a13e4ad7afa to your computer and use it in GitHub Desktop.
16th December solution - Advent of Code 2020 - swift
struct Rule {
let name: String
let range1: ClosedRange<Int>
let range2: ClosedRange<Int>
func contains(value: Int) -> Bool {
return range1.contains(value) || range2.contains(value)
}
}
let filename = "/Users/felix/xCodeProjects/AdventOfCode2020.playground/Resources/december16.txt"
let contents = try! String(contentsOfFile: filename)
var lines = contents.components(separatedBy: CharacterSet.newlines).reversed() as [String]
var rules = [Rule]()
var myTicket = [Int]()
var tickets = [[Int]]()
var line = lines.popLast()
while line?.isEmpty == false {
let parts = line!.components(separatedBy: ": ")
let name = parts[0]
let ranges = parts[1].components(separatedBy: " or ")
let range1 = ranges[0].components(separatedBy: "-")
let range2 = ranges[1].components(separatedBy: "-")
rules.append(Rule(name: name, range1: Int(range1[0])!...Int(range1[1])!, range2: Int(range2[0])!...Int(range2[1])!))
line = lines.popLast()
}
line = lines.popLast()
while line?.isEmpty == false {
if (line != "your ticket:") {
myTicket = line!.components(separatedBy: ",").compactMap { Int($0) }
}
line = lines.popLast()
}
line = lines.popLast()
while line?.isEmpty == false {
if (line != "nearby tickets:") {
tickets.append(line!.components(separatedBy: ",").compactMap { Int($0) })
}
line = lines.popLast()
}
var ticketScanningErrorRate = 0
var validTickets = [[Int]]()
tickets: for ticket in tickets {
entries: for entry in ticket {
for rule in rules {
if rule.contains(value: entry) {
continue entries
}
}
ticketScanningErrorRate += entry
continue tickets
}
validTickets.append(ticket)
}
print(ticketScanningErrorRate)
var possiblePositionsOfRules = [String: [Int]]()
for rule in rules {
var possiblePositions = [Int]()
for i in 0..<20 {
if validTickets.allSatisfy({ rule.contains(value: $0[i]) }) {
possiblePositions.append(i)
}
}
possiblePositionsOfRules[rule.name] = possiblePositions
}
var positionOfRules = [String: Int]()
var rule = possiblePositionsOfRules.first { $0.value.count == 1 }
while !possiblePositionsOfRules.allSatisfy({ $0.value.count == 1 }) {
let entry = possiblePositionsOfRules.first { $0.value.count == 1 }!
let position = entry.value.first!
possiblePositionsOfRules.removeValue(forKey: entry.key)
positionOfRules[entry.key] = position
possiblePositionsOfRules.forEach { (key, value) in
if value.count != 1 {
var mutableValue = value
mutableValue.removeAll(where: { $0 == position })
possiblePositionsOfRules[key] = mutableValue
}
}
}
print(positionOfRules)
print(positionOfRules.filter { $0.key.contains("departure")}.map { myTicket[$0.value] }.reduce(1, *))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment