107 lines
2.7 KiB
Swift
107 lines
2.7 KiB
Swift
//
|
|
// File.swift
|
|
//
|
|
//
|
|
// Created by Max Nuding on 05.12.21.
|
|
//
|
|
|
|
import Foundation
|
|
import Runner
|
|
import Collections
|
|
|
|
|
|
struct Day10: Runnable {
|
|
let inputPath: String
|
|
|
|
public func run() {
|
|
let input = try! String(contentsOfFile: inputPath)
|
|
let lines = input
|
|
.trimmingCharacters(in: .newlines)
|
|
.components(separatedBy: .newlines)
|
|
var s = Deque<String.Element>()
|
|
var illegalCharacters = [String.Element]()
|
|
var syntaxScore = 0
|
|
var autocompleteScores = [Int]()
|
|
for line in lines {
|
|
s = Deque<String.Element>()
|
|
var isIllegalLine = false
|
|
for char in line {
|
|
if char.isClosing {
|
|
guard let popped = s.popFirst() else {
|
|
fatalError()
|
|
}
|
|
guard let shouldBeClosedBy = popped.closedBy else {
|
|
fatalError()
|
|
}
|
|
if shouldBeClosedBy == char {
|
|
continue // Valid chunk
|
|
}
|
|
//print("Line: \(lineNumber): Expected \(shouldBeClosedBy), but found \(char) instead")
|
|
syntaxScore += char.score!
|
|
isIllegalLine = true
|
|
break
|
|
}
|
|
s.prepend(char)
|
|
}
|
|
if !isIllegalLine {
|
|
var lineScore = 0
|
|
while let last = s.popFirst() {
|
|
let charValue = last.closedBy!.autocompleteScore!
|
|
lineScore = lineScore * 5 + charValue
|
|
}
|
|
autocompleteScores.append(lineScore)
|
|
}
|
|
}
|
|
print(syntaxScore)
|
|
print(autocompleteScores.sorted()[autocompleteScores.count / 2])
|
|
}
|
|
}
|
|
|
|
extension String.Element {
|
|
var isClosing: Bool { self == "]" || self == ")" || self == ">" || self == "}" }
|
|
var closedBy: String.Element? {
|
|
switch self {
|
|
case "[":
|
|
return "]"
|
|
case "(":
|
|
return ")"
|
|
case "{":
|
|
return "}"
|
|
case "<":
|
|
return ">"
|
|
default:
|
|
return nil
|
|
}
|
|
}
|
|
|
|
var score: Int? {
|
|
switch self {
|
|
case ")":
|
|
return 3
|
|
case "]":
|
|
return 57
|
|
case "}":
|
|
return 1197
|
|
case ">":
|
|
return 25137
|
|
default:
|
|
return nil
|
|
}
|
|
}
|
|
|
|
var autocompleteScore: Int? {
|
|
switch self {
|
|
case ")":
|
|
return 1
|
|
case "]":
|
|
return 2
|
|
case "}":
|
|
return 3
|
|
case ">":
|
|
return 4
|
|
default:
|
|
return nil
|
|
}
|
|
}
|
|
}
|