Compare commits

..

2 Commits

Author SHA1 Message Date
d751bc64f8
10b 2021-12-10 07:33:51 +01:00
b2afe310b4
10a 2021-12-10 07:08:34 +01:00
5 changed files with 229 additions and 1 deletions

View File

@ -18,7 +18,8 @@ let package = Package(
.executable(name: "06", targets: ["06"]), .executable(name: "06", targets: ["06"]),
.executable(name: "07", targets: ["07"]), .executable(name: "07", targets: ["07"]),
.executable(name: "08", targets: ["08"]), .executable(name: "08", targets: ["08"]),
.executable(name: "09", targets: ["09"]) .executable(name: "09", targets: ["09"]),
.executable(name: "10", targets: ["10"])
], ],
dependencies: [ dependencies: [
// Dependencies declare other packages that this package depends on. // Dependencies declare other packages that this package depends on.
@ -80,6 +81,13 @@ let package = Package(
.executableTarget( .executableTarget(
name: "09", name: "09",
dependencies: [.targetItem(name: "Runner", condition: nil)] dependencies: [.targetItem(name: "Runner", condition: nil)]
),
.executableTarget(
name: "10",
dependencies: [
.targetItem(name: "Runner", condition: nil),
.product(name: "Collections", package: "swift-collections"),
]
) )
] ]
) )

108
Sources/10/10.swift Normal file
View File

@ -0,0 +1,108 @@
//
// 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)
//.map { line in line.map { $0.wholeNumberValue! } }
var s = Deque<String.Element>()
var illegalCharacters = [String.Element]()
var autocompleteScores = [Int]()
for (lineNumber, line) in lines.enumerated() {
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")
illegalCharacters.append(char)
isIllegalLine = true
break
}
s.prepend(char)
}
if !isIllegalLine {
var lineScore = 0
s.reverse()
while let last = s.popLast() {
let charValue = last.closedBy!.autocompleteScore!
lineScore = lineScore * 5 + charValue
}
autocompleteScores.append(lineScore)
}
}
print(illegalCharacters.map{$0.score!}.reduce(0,+))
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
}
}
}

12
Sources/10/main.swift Normal file
View File

@ -0,0 +1,12 @@
//
// File.swift
//
//
// Created by Max Nuding on 05.12.21.
//
import Foundation
import Runner
//Runner(target: Day10.self, day: "10", isTest: true).run()
Runner(target: Day10.self, day: "10", isTest: false).run()

View File

@ -0,0 +1,90 @@
([<([{{<<(<<{[<><>)}<([][])<<>{}>>>{[{(){}}(()[])]<{()()}<()()>>}>([<<{}<>>(<><>)><[{}[]](<>())>][
({{<<(<<<<{{[({}{})<[]()>]<([]{}){[]<>}>}{([{}[]]}}}>({(((()()))<{<>()}[<>()]>)[{(<>{})(()[])}[{[]
[{{[([<({[([<{[]}({}<>)>]{(([]())[()[]]){<[]<>>[<><>]}}>{<[(()<>){[]<>}][[<>{}]<()()>]>[[[<>[]][()
{(<<{([([<(<{(<>[])<()<>>}>){[[<<>()>[<>[]]]{[{}{}]{[]<>}}]{<{{}<>}{{}()}>{[()()]<[]<>>})}>[{({[{}
<{<<<<<{{{({<<[][]>[()[]]><<(){}>([]{})>}{{<[]{}><{}[]>}{<{}{}>({}<>)}}){([(()[]){[]()}][<[]()><[]{}>])
<{({{<{{{<(({(<>)({}[])}{<()>{(){}}})[[(<><>)[[]]]<[{}[]]{<>()}>])>{<<([{}()]{[][]})({{}{}}{()})><
<([[<<[{[{({{({}<>)[(){}]}{[{}<>]{[]{}}}}({[()()]<<><>>}(<{}[]><[]{}>)))}<({<<[]<>>({}[])>
[[{<{{[{<{<[{[()[]]<()()>}][<(<>[]){<>()}>[{<><>}(<>())]]><[{{{}<>}<{}{}>}{[{}[]][[][]]}]<[([]<>){[]{}}]<(
[({[<([[{<[{{(()[])<[]{}>}<[{}<>>{[][]}>}(<<[]{}>([]<>)>[([]){()[]}])]<[[[[]()][<>{}]]<(<>{})((
<{[[{{{[([([{[<>()]{[]{}}}<{[][]}[<>()]>](<<<><>>{()()}>))<<{{<><>}[{}()]}{(<><>)[<>{}]}>>])]{[<<[<([]
[[(<<<({[(((([<>()]([]{}))((<>){[]()})})[[<[[]<>]<{}<>>>[<()()>[()<>]]][[[<>{}]{[][]}]]])<<(({{}[
{{{[[<<{{[{<[[{}()]]>[{[<>[]]([][])}<{{}()}{{}[]}>]}([[{()}{(){}}]([<>{}]<{}<>>)][{{[]<>}}[{{}{}}[{
{[{[(<{{([{<<<[][]><()<>>>[{{}()}{[]<>}]>({<{}()>[()[]]}<<()[]>([][])>)}{[([[]{}][<><>]){{{}[]}<
{[({<{[<{{[(({()<>}({}[]))([()]{<>[]}))({{[]{}}<<>[]>>)]{((<()[]>{(){}})[{[]<>}])}}}<[[<<<(){}><()
[[[[[{{[{<<[<<{}<>>>{<()()><{}[]>}]>(<([()<>}<<>[]>)(((){})<()[]>)><({{}{}}<[]()>)<{{}<>}(
{{<<[<{(<[([(<{}<>>[[][]])[(()())]]{[([][])({}<>)]<(<>{}){[][]}>})([{(<>{})<()<>>}])]>(([[[(<>{})](<[]
<<([{<(<{({<{({}<>)<()()>}{<<>[]>[{}{}]}>(<((){})>(<[]()>[<><>]))}<<(([])[()])<([][])(()<>)>>>)([{([<>{}]([]<
{[[{{[[({{[([{<>[]}[{}[]]]){(<[]{}>[()()]){[<>{}][<>]>}]}{<{<<{}()>({}())>}{[[<>[]]{<>{}}]<[{}[]]{{}{}}>}
{<[[{({<{<[<(<(){}><<>{}>)>{[[(){}]]{({}())({}[])}}]{[{{(){}}}]}>}(<{{{([]{})[{}<>]}(<<>{}>)}(
<[[<(<([[({(<{<>()}([][])><(<>[])[<>{}]>)((<<>()>[[]()])<{()<>}<()()>>)}>[[({{{}<>}[[]<>]}
<{([{({[[({([[{}()]<()()>][<()[]>[[]<>]]}([<(){}>{<><>}][<{}<>><<>{}>])}[(([<>[]][<>[]])){
<{[<(<(<[[[{[{<>{}}]((())({}()))}][(([{}<>]{{}{}}))[{[[]()]((){})}<[<>[]]([][])>]]]][[((({
({[<<([[[<[[<(<>{}){{}[]}>[((){})[<><>]]][{{<>{}}{(){}}}(<[]{}}({}<>))]]{((<{}[]>({}<>))<<<>{}>[[]{}]>)[((
{[<<[([[([{<<[{}{}]({}[])><<{}<>)(<>[])>>[([()()]{<><>}){[[]{}]{(){}}}]}{<([<>]([]())){<(){}>{()<>}}><<{
[<[[[[({{([{{(<>[])<<><>>}<{[]{}}<<>>>}(<{[]{}}<<>()>>)][{(<<><>><()[]>){[{}()]>}<[{{}{}}{<>[]}]>]
{<{[<(([{{({[<<>[]><{}[]>]<{<>{}}{()()}>}([<{}{}>{[]<>}]{<{}<>><[]>}))<([([]<>){[]{}}]<{[]<>}
[<<(({<<[{[([[<>]<()[]>]{[[]<>]{()<>}}){{<<><>>{[]<>]}[({}<>){()<>}]}](<{{()[]}}({{}[]}<{}[
({(<{<{<([([[((){})<{}<>>]<[{}[]](<>{})>])])<<[{<<<>[]>>}<[(<><>){<>{}}][(()[])(<>{})]>]<(<{<>{}}
([<{<<[([({<<<()()>(())><[[]<>]{<>[]}>>([(()())]{[[]()]<{}<>>})})[<[[(()<>){<>{}}]][<(<><>){<>[]}>]><<([{}
([[<[[[({[<<[[{}()](()<>)]<{()[]}>>{<[{}<>]<()[]>><<[][]>>}>]<(<<[{}<>){<>}>{<[]()>[[][]]}>)>}({
(<[(<[([<{<{<[(){}][<>()]><(<>())(()<>)>}{{[{}>([]<>)}[<<>{}>(()())]}>[{([<><>]<()()>)}{([()]{()<
({{{{<[{{<{<<(()[])>{[(){}]{[]{}}}>}{{([<><>][<><>])<(()<>)[[][]]>}<<(()<>)><{<>()}>>}>}}][(<{(<[{<><>}[{}[
<{[{((([({{({<()()>(())}){([()<>]((){}))<(()<>)[(){}]>}}{[{([]())((){})}[{[]<>>{<>{}}]]<<<()[]><()<>>>((()()
{([({[(<[<([{[<>[]][()<>]}{[(){}]{{}<>}}][<<{}()>>[<<>()>[()}]])<((({}{})[[]<>]))<{[[]{}]([]<>)}>>><{
([[[{<[<([{(<<<>()>({}<>)><[<>{}]>)[[<<><>><()[]>](<()<>>(<>[]))]}([({()()}([]())){<[]{}>[[]<>]}][<
<<((<({{(<((<({}[])(<>())>{({}[])({}<>)}))<<<([]<>)[()[]]>{<()<>>[[]<>]}>>)({(<[<>{}]((){})><{()()}[{}(
{({([(({{<{{{<[]()>{{}[]})[[(){}](()<>)]}(([[]{}]{()<>}))}>{(<[{()()}]{[<>[]][()()]}>{{<()()
{[(<{<[<<[{<<<<>{}>>[[[]{}]<<>()>]>((<[]{}>{{}()}))}{({[{}()](()<>)}<<[][]>[{}<>]>)({[{}[]]}<<{}[]
{{{<{<{[[{([{{[]()}}({()[]}(<>))]<((<><>)({}{}))>)}]]{[({<(<{}[]>{<>{}})((()<>)([]()))>})[<[[{<>{}}<<><>>]<{<
[[<{<<[(<<<([<{}()>[{}()]](<{}[]>[{}()]))[([[]<>][{}{}])[<[]()><[]()>]]><[[[[]<>]({}{})][{[]}{<>[]}]][<[<>
(<<<<(<<[<[(({[]()}[[]<>]))({(())(()[])}[<<>[]>{()[]}])]{<({{}{}}(<>{}))><[[<>[]]<[]()>]>}>{({{(<>()){
<<(<[(<<<{({[<()>{()()}][([]{})([][])]}{({{}}{<>{}})<(())[{}()]>})}[[{<[[][]]{()}){[{}<>]<{}(
<<{<{{[<[(({({[][]}<<><>>)}<((<><>)[{}[]])<<[]<>>>>)[{{[()[]][[]<>]}{({}())<(){}>}}])({[([[]<>](<><>))<
<<{<[(<(<([[{{()()}{<>()}}(<<>()>({}[]))]<<{{}{}}{()<>}>(({})(()()))>])[[([(<>[])(<>)](<(){
({[((({[([<{[{<><>}[()[]]][{[][]}{(){}}]>>(<{{[]{}}<()[]>}<{()<>}<[]()>>>[{({}<>)<{}()>}])])]<(<{{{<[]()
<[{<<{<<{<(({[<><>]{{}<>}})){<[{<>[]}({}{})]([{}()]{<>()})>[<{<><>}<<>{}>}([<>[]]<[]()>)]}>(
(<<(({<<(<(({[<>[]]<[][]>}({<>()}[[]()])){((<>[])){([]{}){[][]}}}){{[[[]{}][{}[]]][<{}[]>([]<>)]}[(({
<(<(<<<[<{({([<>()])[[[][]]{<>[]}]}[<[()]<{}<>>><[(){}]<()()>>])((<(()[])<{}{})>)((([][]))))}>]<[<([[<<>()
{{<[<<<{((([[([]{})][[<>[]]]])(<[[(){}]<<>[]>]>{[({}())([]<>)][<<>{}><[]()>]}))){{<[[[[][]
{([<{[<<[{({({<>{}}[[][]])[{(){}}{{}()}]}[[[{}()]<()()>]])({<[{}{}]>({{}{}}[[]{}])}{{({}())<()()>}{{<><>}<[]
[<{[{<{{<{<[{{{}{}}[[]()]}([{}[]]<{}()>)](<{()()}{<>[]}><(<>[])>)>}{{{((()<>)[<><>])[[()()]
([{[<[({([<<<([][])<{}{}>>[[()<>](<>{})]>{{({}<>)}([<><>]<()<>>)}>](<(<<[]<>>({}[])>[<<>{}>[()()]])(<<()[]>((
(<[{<({<([<({<<>()><{}()>}(<[]{}>({}<>)))[([[]()][<><>>){[<>[]]{{}<>}}]>({[{[]{}}({}[])]<{[
<<[<(<({<(<({<()><[]()>})>{[{[[]()]<{}>}<(()<>)(<>())>)(((()())[<><>])([(){}]))})([(<{<>()}([]())>{[{}{
[{<{<{<[<((({[()[]]{{}}}[[<><>]<<>{}>])<({[][]}{()[]})<([]())([]{})>>))((<([{}<>]{(){}})>[
[[[[([<{[[(<{{{}[]}[<>()]}<{[]}{<><>}>><{({}<>){()<>}}[{[]{}}{<>[]}]>)[([({}<>}([]<>)]{{{}{}
<{{[[<{[(({({<{}>[(){}]}(<[]()>))}<<<[{}{}]>(<[]{}>(<>[]))>([{{}<>}(()())]<{<><>}{<>()}>)>)<[<<
{[{{{(<<([([((()[])[[]<>])({{}{}}({}<>))]<({{}[]}{[][]})<{[][]}>>){<<({})[[]<>]>(<[][]>(<>{}))>}]<[
<[<(<{<[([[[{[()[]][<>{}]}[{(){}}{<><>}]]{<([]{}){()<>}>[({}<>)<<>[]>]}][<[({}[]){()[]}][<<>{}><()<>>]>]]<(
([<{(([[<<{([([]{})([]())]<{(){}}{[]<>}>)([<<>{}>[()()]]{[[]{}]<[]<>>})}[({{()()}<<>{}>>){[<()
([({{{[[[([{<{<><>}{()()}>(<[]())[<>])}{([<><>]<(){}>){(<>())[{}()]}}]<{[[<><>]{{}[]}]([{}<>]{[]()
[[<<<{{({[{[({{}{}}<(){}>)<[[]{}]({}<>)>]}][(<{<<>{}>}{{[]{}}{()[]}}>[{[<>{}]<[][]>}<{<><>
[([{(([{(([{[{[]()}[{}]][[[]<>]{<>{}}]}{[<{}{}>({}[])][<<><>>(()<>)]}]{<({[]{}}{<>{}}}>}){
(<<<<<[{<{({({[]{}}({}())){([][])[{}{}]}}([{<><>}{{}{}}][[{}[]]({}{})]))[[[{{}()}[<>{}]][{{}<>}{()}]
(([{<(<[(<{{{[[]<>](<>[])}{[()<>]<()()>}}}>)<{{({<{}<>>[{}{}]}({<>[]}[()()]))}([([<>[]][[]{}])(<<><>
((<{[(([{{([<[<>()]<<>[]>>{((){})([])}][<{[]()}<<>()>>(<{}()>(<>))])}[{{{{[]()}}{(<>[])[()<
<[[{{[<<({[<<{(){}}{(){}}>>(<{[]<>}<<>()>>({()<>}{{}<>}))]{[([[][]][{}<>])][((<>{})(()[]))[<()[]>{<>()}]]}
{<{<<[{[([{{{{{}()}<()<>>}<[()[]]>}<[<[]<>><()<>>](({}())<{}>)>}<<({[]()}(()()))({{}<>}{{}<>
{{[<{[<{[[{{({[]<>}<{}[]>)((<>{})({}{}))}([[[]{}]<{}[]>]<({}()){()<>}>)}[<([{}<>}<<><>>)><{[()[]][<>[]]}{
([{{{[{(<<<[<{<>[]}{()[]}>(([]())<<>{}>)]>[[(<{}()><[]<>>)][{[[][]]<(){}>}<<()()>({}[])>)]>>)<<[[(<(<>())((
<({[{{(({(<<<{[]()}(<>{})>{(()<>)[[]<>]}><[[<>()]]{([]{})[[]()]}>>)})({[{<(<[]<>>{{}[]])[<<>{}>[(){}]]><(({}(
((<(([{([{<[{<[]()>(()[])}<({})[<>[]]>]>}]{[<<{[[]()]<<>()>)<<{}[]>{<><>}>>>[[([(){}](<>[]))]{{<
[([<<([(<[<<([[]{}]<(){}>)([()[]][{}{}])>([([]<>)<(){}>]([[][]]{[]<>}))>{{[{[]<>}[{}[]]]{([]
[<{<[[<{<({<[<()<>><[]<>>][<{}<>><<>()>]><{((){})(()())}>}{[([{}<>]({}{})){(<>{})}]([[<><>](<>)])})><{<{
[(<[[<({{<<([<(){}><<><>}]<<{}()>({}<>)>){[(<>())[(){}]]<<<><>>[{}<>]>}><{[{(){}}][<{}{}>[()(
<[<{[{{<([{<[{()<>}{()<>}]]{<[{}<>](<>{})>([{}<>]{{}()})}}<[([(){}](()[]))<{{}{}}>]{{{<>}<[
(<{[[{[[{[[(<[[][]][<>{}]>(<()<>>(<>{})))[{([]{}}<{}[]>}[[[]{}]{()()}]]]{<[<<>{}>(()<>)]<{{}<>}<[
(<<<[[(<{<({{(<>())<(){}>}<(()())<()[]>>}<[({}{})[()[]]]>)>}{<{[<[()<>][<>[]]>[([]{})(()())]][({{}{}}
({({[[[(<<[{<<<>[]][{}()]>(({}())([]()))}]{<{[()[]]({}())}>({<()[]><[]{}>}[{<>()}<[]{}>])}>>)]]]}[({[[{({(((
((([[<{[(<({<{<><>}[[]]>[{(){}}>}((<()<>>{()()})<<<>()>({}())>))((([<>()]{[][]})[[(){}]<{}
<[{([(<[({[[([{}()]({}{}))([<><>]({}()))](({(){}}{()[]}><[[]()]<<><>>>)](<([(){}][{}{}])[{
{<((({<((<[[<{()[]}<{}{}>>](<<[]>{<>}><[[]{}]({})>)]<<{[{}{}]}[{<>{}}}>>><(([<<>>(()())]<(
{[[[<<(<{(<(<[{}<>]<[]()>>[{{}{}}{()<>}])[<{{}()}{{}{}}><{[]<>}({}())>]>)<<(<<()<>><<><>>>)>>}[(
[<{<[([({([[[{{}()}{{}()}]([[]()])]])}<<<[<<<>{}>[<><>)><{[]<>}(()<>)>](({{}<>}[()<>]){{<>[]}((){})})>({(
([[{{[[[<(<({(()())<{}[]>}<({}<>){[][]}>)[<{()()}[[]<>]>]>)([[({<>[]}[()()])[{[]<>}[{}{}]]][[<
({[(<<<{((<{<[()()][[]<>]>{<{}<>><()()>}}<{[()[]]([]<>)}<<{}()><{}{}>>>>[<<{(){}}[()[]]>[([]<
([({{{<[{[{{[([]{})<[]{}>][<<>[]>[{}[]]]}}[{[<<><>>{[][]}]((<>()){<>{}})}]]]][[(<({[[]()][[]{}]
(((<{{<<<([[{(<><>)<{}>}<{{}{}}[{}<>]>]{[(()())[{}<>}]}]{<(<[]()>)>([<()[]>{{}()}]<({}()){(){}}>)})<[({(<>()
[{(([[<(<<(<{<[]()>(<>{})}[{()[]}[()()]]><[<[]()>[[]<>]]>)<{<[[]<>]<()<>>>{[()[]]<{}[]>}}>>([((<[][
{<{<{[([(<<[{<()<>>(<>[])}[(<><>)[[]<>]]]<<<[]<>><(){}>>{{{}<>}<<><>>}>>(<{{<>()}[()<>]}><[

View File

@ -0,0 +1,10 @@
[({(<(())[]>[[{[]{<()<>>
[(()[<>])]({[<{<<[]>>(
{([(<{}[<>[]}>{[]{[(<()>
(((({<>}<{<{<>}{[]{[]{}
[[<[([]))<([[{}[[()]]]
[{[{({}]{}}([{[{{{}}([]
{<[[]]>}<{[{[{[]{()[[[]
[<(<(<(<{}))><([]([]()
<{([([[(<>()){}]>(<<{{
<{([{{}}[<[[[<>{}]]]>[]]