Add 2018 solutions

This commit is contained in:
Sven Weidauer 2022-11-23 16:46:40 +01:00
parent 6f9b51146c
commit 43a2ee8414
8 changed files with 2027 additions and 0 deletions

84
2018/AoC/algorithms.swift Normal file
View file

@ -0,0 +1,84 @@
import Foundation
func readInput() -> AnySequence<String> {
return AnySequence {
AnyIterator {
readLine()
}
}
}
extension Sequence {
func fold(initial: Element, operation: @escaping (Element, Element) -> Element) -> AnySequence<Element> {
return AnySequence { () -> AnyIterator<Element> in
var iterator: Iterator? = nil
var runningSum = initial
return AnyIterator {
if iterator == nil {
iterator = self.makeIterator()
return runningSum
}
guard let next = iterator?.next() else {
return nil
}
runningSum = operation(runningSum, next)
return runningSum
}
}
}
func repeated() -> AnySequence<Element> {
return AnySequence(sequence(state: nil as Iterator?) { iterator in
if let next = iterator?.next() {
return next
}
iterator = self.makeIterator()
return iterator?.next()
})
}
}
extension Sequence where Element: Hashable {
func firstDuplicate() -> Element? {
var items: Set<Element> = []
return first { !items.insert($0).inserted }
}
}
extension Sequence where Element: Hashable {
func frequencies() -> [Element: Int] {
return Dictionary(self.map { ($0, 1) }, uniquingKeysWith: { first, second in
first + second
})
}
}
extension Sequence {
func allPairs() -> AnySequence<(Element, Element)> {
return AnySequence { () -> AnyIterator<(Element, Element)> in
var pairIterator: Array<(Element, Element)>.Iterator? = nil
var outerIterator = self.makeIterator()
var drop = 1
return AnyIterator { () -> (Element, Element)? in
if let result = pairIterator?.next() {
return result
}
guard let value = outerIterator.next() else {
return nil
}
pairIterator = self.dropFirst(drop).lazy.map { (value, $0) }.makeIterator()
drop += 1
return pairIterator?.next()
}
}
}
}

1411
2018/AoC/day3input.swift Normal file

File diff suppressed because it is too large Load diff

111
2018/AoC/main.swift Normal file
View file

@ -0,0 +1,111 @@
import Foundation
struct Claim {
let id: Int
let left: Int
let top: Int
let width: Int
let height: Int
var right: Int { return left + width }
var bottom: Int { return top + height }
}
func overlap(_ first: Claim, _ second: Claim) -> (Int, Int, Int, Int)? {
let overlapRight: Int = min(first.right, second.right)
let overlapLeft: Int = max(first.left, second.left)
let widthOverlap = overlapRight - overlapLeft
let overlapBottom: Int = min(first.bottom, second.bottom)
let overlapTop: Int = max(first.top, second.top)
let heightOverlap = overlapBottom - overlapTop
guard widthOverlap > 0 && heightOverlap > 0 else { return nil }
return (overlapLeft, overlapTop, widthOverlap, heightOverlap)
}
extension Scanner {
func scan() -> Claim? {
guard
scan(string: "#"),
let id: Int = scan(),
scan(string: "@"),
let left: Int = scan(),
scan(string: ","),
let top: Int = scan(),
scan(string: ":"),
let width: Int = scan(),
scan(string: "x"),
let height: Int = scan()
else {
return nil
}
return Claim(id: id, left: left, top: top, width: width, height: height)
}
func readClaims() -> [Claim] {
var result: [Claim] = []
while !isAtEnd, let claim: Claim = scan() {
result.append(claim)
}
return result
}
}
let claims = Scanner(string:day3Input)
.readClaims()
.allPairs()
.compactMap(overlap)
//.reduce(0) { $0 + $1.0 * $1.1 }
print(claims)
//func removeDifferingCharacter(_ a: String, _ b: String) -> String? {
// var differingIndex: Int? = nil
// for (index, (first, second)) in zip(a,b).enumerated() {
// if first == second {
// continue
// }
//
// if differingIndex != nil {
// return nil
// }
//
// differingIndex = index
// }
//
// guard let offset = differingIndex, let index = a.index(a.startIndex, offsetBy: offset, limitedBy: a.endIndex) else { return nil }
//
// return String(a[a.startIndex..<index]) + a[a.index(after: index)..<a.endIndex]
//}
//
//print(Array(readInput())
// .allPairs()
// .compactMap(removeDifferingCharacter))
//let frequencies = readInput()
// .map { $0.frequencies() }
// .reduce(into: (0, 0)) { result, frequencies in
// print(frequencies);
// if frequencies.contains(where: { _, count in count == 2 }) {
// print("double")
// result.0 += 1
// }
//
// if frequencies.contains(where: { _, count in count == 3 }) {
// print("triple")
// result.1 += 1
// }
// }
//let firstDuplicate = readInput()
// .compactMap(Int.init)
// .repeated()
// .fold(initial: 0, operation: +)
// .firstDuplicate()
//
//if let x = firstDuplicate { print(x) }

14
2018/AoC/scanner.swift Normal file
View file

@ -0,0 +1,14 @@
import Foundation
extension Scanner {
func scan(string: String) -> Bool {
return scanString(string, into: nil)
}
func scan() -> Int? {
var result: Int = 0
guard scanInt(&result) else { return nil }
return result
}
}