Merge remote-tracking branch 'twentyone/main'

This commit is contained in:
Sven Weidauer 2022-11-23 16:50:19 +01:00
commit 55c236e336
29 changed files with 11688 additions and 0 deletions

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<Workspace
version = "1.0">
<FileRef
location = "self:">
</FileRef>
</Workspace>

View file

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>IDEDidComputeMac32BitWarning</key>
<true/>
</dict>
</plist>

27
2021/common.swift Normal file
View file

@ -0,0 +1,27 @@
import Foundation
protocol Puzzle {
mutating func run()
init()
}
extension Puzzle {
static func main() {
let start = Date()
var instance = Self()
instance.run()
let duration = Date().timeIntervalSince(start)
if duration > 1 {
print(String(format: "Took %.2f s", duration))
} else {
print(String(format: "Took %.2f ms", 1000 * duration))
}
}
}
extension RangeReplaceableCollection {
mutating func removeFirst(where predicate: (Element) -> Bool) -> Element? {
guard let index = firstIndex(where: predicate) else { return nil }
return remove(at: index)
}
}

32
2021/day1.hs Normal file

File diff suppressed because one or more lines are too long

170
2021/day10.swift Normal file
View file

@ -0,0 +1,170 @@
@main
struct Day10: Puzzle {
func run() {
var rest = input[...]
var score = 0
var completeionScores: [Int] = []
loop: while true {
if let complete = parse(&rest) {
completeionScores.append(complete)
}
switch rest.first {
case "\n": break
case ")": score += 3
case "]": score += 57
case "}": score += 1197
case ">": score += 25137
case let ch?: print("Unknown char", ch)
case nil: break loop
}
rest = rest.drop { !$0.isNewline }.dropFirst()
}
print(score)
completeionScores.sort()
let completeScore = completeionScores[completeionScores.count / 2]
print("Part 2:", completeScore)
}
func end(for start: Character) -> Character? {
switch start {
case "(": return ")"
case "[": return "]"
case "{": return "}"
case "<": return ">"
default: return nil
}
}
func parse(_ string: inout Substring) -> Int? {
var stack: [Character] = []
while true {
guard let current = string.first else { return nil }
if current == stack.last {
stack.removeLast()
} else if let closing = end(for: current) {
stack.append(closing)
} else if current == "\n" {
return stack.reversed().reduce(0) { partialResult, ch -> Int in
let characterScore: Int
switch (ch) {
case ")": characterScore = 1
case "]": characterScore = 2
case "}": characterScore = 3
case ">": characterScore = 4
default: fatalError()
}
return partialResult * 5 + characterScore
}
} else { return nil }
string = string.dropFirst()
}
}
let input = """
{<[[<<<[[{[[<<()()>>[([]<>){{}{}}]]]<(<{{}<>}{{}{}}><{<>[]}[{}{}]>)({(())}<[(){}][(){}]>)>}]<<<(<<{}
{[({({([({(((<[]()>[()<>]){[<>[]](<>[])})<<<[]()}>(<()()>)>)}[{((<{}[]>{<>()}){[<>{}]<<><>>})(({[]()}<<>()>)<
(((([{{{<{<[[<()<>><<>{}>]]{<(<>())>}>{<[([]<>)<{}{}>][[<><>][<>[]]]>[{(<>{})[<>()]}<<()[]>[[]<
<<[<([[{{{[({<<>()>}<<<>[]>([]())>){{{{}[]}<(){}>)}]{{[(()())[(){}]]<<[]{}>({}[])>}({<<><>>}<([]<>){()[]}>)}
<[(({[({<<[[[{<>{}}(<><>))<(<>[])[{}[]]>]<[([][])[(){}]]>]([<[()()](<>[])>]<(<{}()>[{}<>])({<>[
(<[[{[[[(<((<<{}{}><[]<>>><[{}<>][<>[]]>)<(<{}<>><{}{}>)[{()[]}<<>()>]>)>[(<{({}{})}[{()[]}{[]<>}]>){<(([]
(([[({{{(<([({[]{}}){{[][]]}])>)[<([[[{}]{(){}}][([]())]]([{[][]}{<>()}](({}()){<>{}})))[{({
{(<<([([(<<[{((){})([]<>)}]<{{{}{}}{{}[]}}<{(){}}{()[]})>><([<[]{}>([]<>)](<()<>>)){<([]<>
{[<<[<{{((({<[[]{}]{{}}>{<{}<>>[<>[]]}})<{[(<><>><{}<>>][{[]{}}]}[[[[][]]<<>{}>]{[[]()][[]<>]}]>)[(
({<(<([{<{<[(((){}){(){}})]>}[{{{{()()}<[]()>}{{<>()}<{}{}>}}[[{{}{}}[<><>]]{({}[]>[[]{}]}]}({{{()()
{[[{([{<<{<(<<{}()>[()]>)<{{()()}}[<[][]>(<>{})]>>{[<{<>()){()<>}>{{()()}[<>()]}][[[{}[]]]<{{}[]}<()(
{{<<<<<[[([{<[{}[]]{()<>}>{<<>()>{{}<>}}}<<{{}()}{()()}>{[()<>]({}[])]>]{[(<{}{}>)<({}[])>]<{({}<>)}{({}())
({(<<{[<<{<(<[<>()][[][]]><[{}<>]({}[])>)[[[<>[]][[]()]]<{[]{}}[{}{}]>]>{[(<[]()>{<>{}})][<{{}}<{
(<<<[[({<[[(<<[]{}>>[(()<>)[<>{}]])<{(<>[])}(<<>()>[[]{}])>]<[({[]()}{{}{}})<{{}()}<{}<>>>]>][[({<<>()>{[]()}
{{{[<[([<(<{[([][])[{}[]]]{[()()]{{}()}}}><[<([][])(<>())><{()[]}({}<>)>]<<<(){}><<><>>>((()()){
[(((([{({<(<(<[]<>>{()[]})><{(<><>){{}[]}}{(()<>)([][])}>)>})}][({{[[{<(<><>){{}()}>((<>()){<>[]
([{<{<({(<[(<({}<>)><{[]}[{}[]]>)(<[[][]]{[]{}}>{{[]}((){})})]{(((()){{}()})(([][])({}{})))[(<()[]><(
[([{{<{(<[{{((()())<[]{}>){{{}<>}<<><>>}}[<([]())>[(()[])<{}{}>]]}{[({(){}}{(){}])<<[]()>{{}()}>
(<[<{{[<<({<<{<><>}><<()[]>[<><>]>>}{[(((){}){()<>}}<<[]()>{(){}}>]{{{{}[]}[{}<>]}[{{}{}}<(){}>]}}){{({
<([([[<[({<<{{[][]}<{}[]>}<<()[]>[()<>]>>>})((<(<[[]]((){})>{([]()){<><>}})>)(<<<<[]<>>[[]()])>>([[
[<<{<(<<{{[({<<>()><{}[]>})<{[<>()](<><>)}[{{}()}{[]()}]>]}}({<[{<()<>>}[<<>>]]((<[][]>{[][
{{(<({[((<([({()<>})[{{}[]}(()<>))]{[{[]<>}][{()<>}]})([({<><>}{{}()})])>)({<(((()<>)(<><>)))>((
[(<{({[<{<[<([{}()](<>{}))>]([([[]<>]<()<>>){<()>}]{{[[]()]{{}{}}}[{[]}{{}<>}]})>}<([<[<<>[]>]{<(){}>{
[[[[<(<({<[[<{()}{{}[]}><[<><>]<()[]>>](<([]{})[{}]>)]>}<[{[[{(){}}]{(<><>)}][<<[][]>{(){}
{(({({[{{((<[([]())((){})]>){[{(<>[])}<([]{})[[]<>]>]({<()<>>[[]<>]}[<()>{<>[]}])})({<([[][]]((){}))([<>{}])
<(<(<<{[<<{<<[()[]][[]()]><([][])<{}()>>}}>[<<<<{}<>>>{[{}][{}{}]}>[<[()()]{{}[]}>[{[][]}([])]]>[[<<[][]>(
<<{{({<<(([{{([][])(()<>)}}(<<[]{}>>)])([{[(()[])]}<{{<><>}<[]<>>}[<{}<>><[]<>)]>]{{<[{}[]]
({[[[[(([[[{<({})([]())>{{<>{}}}}][[<<[]>[<>()]>]{[[[]<>][<>{}]]{[<>[]]}})][[<(<(){}>{()[]})><[[()[
(((((([{{{(<<(<>{}){<>}><(()[])<()[]>>>{[<{}<>>(<>())}<{{}()}>})((([(){}]{<>})[{[]{}}{{}[]
(([([(((<[(({[<>()]{[][]}})[{{[]()>[[]{}]}(<<>{}><{}<>>)]){[({[]}(<>[]))([()()][<>{}])]({({}
[(<({{([[<(((<<>()>[[]<>])({{}()}<()<>>))[[{[]()}<()<>>][{()()}<{}[]>]])>({(<({}{})<()[]}>[{()
{{{[([<{{{([<<<>{}>{()<>}>{<{}()><<><>>}]([{()}]))<{[<()()>{()[]}]{({}[])[<>()]}}>}}}({<(({({
[([([<[<(<[{{{<>()}{()[]}}({()[]}[{}<>])}{[{<>}]([[][]]<(){}>)}]>[[{<[<>()][{}()]>(((){}>[<>[]])}{{{<>(
{{[{{[[<{(<([[[][]]([]<>)])>)<{<<{<>()}[<>()]><((){})<<><>>>>([<<><>>(()<>)]([(){}}{[]<>}))}>}(({{{{[]}{
[[[[({[<({{[[(<><>)]{<<>[]>([]{})}]<[{[]}{()()}]([<>()]([]<>>)>}})>{{<([{<{}{}>{()}}])({<[<>()](<><>)>
{[({<[({[{[({[{}()][[]<>]}((()<>)<{}<>>))<[{{}}({}{})]>]{[<<<>[]>[[][]]>(({}[])(()[]))>{(([]{})({}[]))([
[<<([{{(<<[<[[{}[]]](<[]{}>[<>{}])>[[<{}[]>(<>[])]{([][])[()[]]}]][[{{()[]}[<>{}]}(<<>{}>[[]
<[(([{([([<{[[(){}]<<>{}>]}[[[()()]{{}<>}]]>[[{({}{})[()()]}([()()][<>[]])]{<[[]<>][[][]]>}]]{{<{[<>
[<[[[[([[{[[<[{}{}]([]<>)>[({}[])<[]<>>]][<{()()}[<>[]]>([[]]<<><>>)]]}([<{{{}[]}}{(()<>>({}[
<[<{[(<{({[<[[[][]]([][])]<([]){{}<>}>>[({<>{}}<[]>)]]([[([]{})[[]()]][[[]{}]({}{})]](<[{}<>
({[<([(<[[<([[(){}]<<>{}>])({[[]{}]<()[]>}<<[]<>>{[][]}>)>{<[<[]()><{}[]>]([<>()]({}[]))><(
({<<<{<({[[{[{<>{}}[<><>]]}{(({})[()()])<<()<>>([]{})>}]]]{<{{([[]<>](<>[]))[[{}()]({}())]}}>((<([{}<>])>{{<[
[{([{[[{{<{<[{[]()}<[]{}>]>({<{}[]>{<>[]}})}[(({{}<>}<{}<>]){[[]{}]({}[])})]>}}]({([[([<()()>{[]
<[{([([{[{<(([<>[]]{<>()})<{{}<>}>){([<><>])<(()[])<<>{}>>}><{{[{}<>]{<>}}[(())[()<>]]}<{[{}{}]{{}{}}}<{{}<>
[{<<([{{(((<{<<>()>[{}()]}>{<<<>[]><[]()>>}){[(([][])([]()))<{{}[]}[[]()]>][[{{}<>}({}{})]{({}{})(<
{({[((<([[[{{([])[()()]}({{}{}}))<(<[]<>>[()[]])>]<<([[]<>])<[<>()][[]<>]>>({[<><>]{{}[]}}({()[]
{[(<(<{[({[(<<[]>{<>{}}>[{<><>}([]())])(<[[][]]<()<>>>[(()[])])]<{[{()[]}[<>{}]]}[{<(){}>(<>{})}<<<>()>>]
(<{<[<({{{{([({}{})[[]{}]]({{}<>}<()[]}))}(<<{<>()}{()<>}>{[<>{}][()[]]}>{<<[][]>{<>()}><(<>{})<{}[]>>})}
{(<<{[({<<[(<{{}{}}<[]<>>>((<>())([]<>)))]>><<({<{[]<>}([]<>)>{[()()]}}<<{{}<>}<()()>>(<()()
<([([<(<[({[[({}()){()<>}](({}{})(<><>))][{({}<>)<{}{}>}{([]){(){}}}]})]>)<[[{<{{(()){<>[]}}({{}()}
<<<([{[({{{((<{}()>{()()})[[<>()]])[[[{}()]<{}()>]{{[]<>}{{}())}]}<<{<<>><<><>>}[{{}[]}(<><>)
<([<[<{[[[{[{[<><>][{}<>]}(<[]{}>)]}([{{<>()}<{}[]>}{([]<>)[(){}]}]<{((){})<[]()>}[<[]()><()[]>]>
[([[[[[({{[(<[{}<>]<{}()>><[<><>]{<>()}>)]}<{(<(()<>)<{}{}>>{{()<>}(()()}})}>})(<[<([<<>[]>((){})]([[]<>]<()
{(<[[{(((([[([{}()]<{}()))[({}{}){[]}]](<<<>[]>>([<>{}]))][(<({}{})<()[]>><<[]{}><{}[]>>)])(<([([][])[
({(([{{<{{{<[<<>[]>{()<>}][([][]){()<>}]>([<()>(<><>)]<{<><>}<[]>>)}<{[[<>[]][<>{}]]<{<>()}{[][]
{[([[[(([<{<{[[]<>]<<>()>}<<()()>([]{})>>[<<()[]>(<>())>[(()<>)]]}>]{{({{{{}[]}({})}<{{}<>}[[]()]>}((
[<([[<[{{{[[[<<>{}>]{<()[]>{[]<>}}]{([[]{}]({}()))}]}}}(<{<[[({}{})<()()>]]({({}<>)[[]]}{[<><>]{<>{}}})>(<[
<<(<<{{(({<[<<[]{}>{()()}}[[()[]]]]{[(<>{}){<>}]{[{}](<>)}}><{<(()())<[]<>>>[<(){}>(<>[])]
{<(<[<[(((<{((()[])([]<>))([[]<>]({}[]))}><<([<>())<[]<>>)(<{}[]>({}<>))>[[([]())]({()()}{{}<>})
[[((<[[[{{<{<{{}()}[{}()]>{{[]{}}[<><>]}}>}<{[<[(){}]<<><>>]([{}<>]<<><>>)]}(<<[{}<>]<<>{}>
<[{{[[{<[{(({[(){}]({}{})})[([{}<>][()()])[<[][]><{}<>>]])}](<({(([]<>){()[]})[([]())((){})]}{{{<>{}}<[]()>}<
<{((<([[{<<[(<{}()><{}[]>)]>[([{<>()}<()()>]<{()[]><()[]>>){<[{}[]]>[{[][]}(<><>)]}]>{{[{{{}<>}{<>}}[[(){}]<(
{<[({(<{{[([{{[]{}}((){})}[[{}[]]<{}()>]]((<[]<>>)<([]()){{}[]}>))(<<{()[]}{[]})>[[(()())]{[()<>][<>(
<<{[[[[{<({<[[{}()]]{{[]<>}{[]}}><{{{}()}({}{})}>}{([({}[])({}())](({}[])))[<{[]<>}[<>()]><<[]<>>(<>[])>]}
[(([<<[{[{<({{()[]}<<><>>})[[([]())[[]{}])(<<>{}>)]>(<{{[][]}[{}()]}([{}{}][(){}])>((<<>[]>(()
[({<<[[<{[<<({[]()}<()<>>)>]]{{<[[[]][{}<>]]>}{([[<>{}][{}{}]]<((){})(()[])>)}}}([{((<{}[]>)[<{}[]><{}()>])}
{[[((<[{<<([{<<><>>([]{})}<(<><>)<<>()>>][{[[]{}]{()()}}<({}{})([][])>])><<{({<>{}}[<>()])((
[[[<{{(<((([{<[]{}>({}<>)}[<<>()>]]<<(()[]){{}[]}>{(()<>){{}<>}}>){{{(()())[<><>]}[[[]<>]{<>[]}]}}))[
(<({[({[[({{<{<>{}}<()[]>>[{(){}}<<>[]>]}[[<[][]><()<>>]({<>{}}{[]()})]}{(([[]<>][{}[]])(<[]<>>([
{([<{[<({{{({[[][]][()()])[<<>()><[][]>])}<{{{[]()}[{}()]}{{<>()}{<>}}}((({}())<{}{}>){(<>{})[<>{}]})>}})(((
([[[[[[{(((((<<>()>[[]<>])<{()()}[()<>]>){<[()[]]<<>[]>>(<<>{}}{{}[]})}))<<[([()()][[]()])[{()[]}([]())]][<([
({{((((<{<[{[(<>{})][<()()>[[]()]]}]{({(<>())[[]()]}{<[][]><[][]>})}>}>{<<{{(({}())[<>])((()[])<[]<>>)}}[
<(<[[[<<{([<<[[]<>]{<>[]}><{()()}([]<>)>>]{<{(<>())((){})}{[{}{}]<<>{}>}>})({([[{}{}]])[(<<>()>[
{[(<{<[[{<{{(<()[]><<>{}>){[()<>]<{}<>>}}[[[<>{}][[]<>]]]}<[[({})<<>[]>]]{{<()<>><<>[]>}<{()[]}((){}
({[{(<<{[<{({[()()]{{}}}<({}{}){()()}>)}<{(({}{})[<>[]]){([]())[[][]]}}{(<[]<>)[()])[[<>()]]}>>
(({<<[<[<<<({<{}[]>({}{})}{<{}[]><{}[]>})>>(<<(<()()>{[]{}})({()<>}[()()])>[{[{}()][[]]}({<>()}}]>(
<<(<(<[{([{<[[<>{}]<{}<>>]>{<(()[])(()())><{<>[]}{()()}>}}([{({}<>){<>{}}}{{<>[]}[{}{}]}]([<[]()>((){})
{[{([({[[<<<{{[]{}}<[]{}>}[<()[]><[]<>>]>([{{}[]][[][]]]{{(){}}({}[])})>(<{[<>{}](()<>)}>{[<[]()>
([[[<(<{[({<({()()}{<>()})((<>[])<[]<>>)>}<{<<{}()><<>>>}({{{}[]}}[(()[])[()<>]])>)<[<((()())[<>{}])(
{<(((<[((((({<[]>[[]{}]}))[(<[[][]][{}{}]>){{[<>()][[]<>]}([()()](<><>))}])){(<[{[<>()][{}()]
({([{<(((<{[<[{}[]]<<>>>({{}[]}[[]()])]([<(){}>])}{<[{<>[]}{[]<>}][<<><>){<>{}}]>[<((){})([]{})>[[{}{}][()
<[{[<[{[({{<(({}<>}[[]()])>({{[][]}}<[(){}](()[])>)}})([[{{<<>()>([]{})}[<<>{}>[<><>]]}]])]
{(<{([{[[({[(<[]{}>)({{}[]}{()()}))<{(()[])}[[()()]{{}[]}]>}<{<(()<>)>{{[]{}}({}())}}({{{}[]}{<
[<<{<{<<(<([<(<>()){[]()}>[({}<>)({}<>)]][(([]{}){()[]})[{{}()}(()())]])>{([{{()[]}<<>[]>}<{<>[
{{[[(([([({([([]<>)(()())][{<>()}{(){}}])})])]([{{[<{{{}<>}(<>())>{[()()]<<>[]>}>[{(()){{}[
[({({({(({{(<[()()][{}<>]>[[{}()]{(){}}])<[<<>[]>][<{}>(<>{})]>}(((({}())({}{}))){{(()[])<[][
<(<(<[([<[{[<(()<>)<()[]>><(<>())([][])>]{<[{}<>]{{}()}>{[[]{}](<>{})}}}<[<([]{})><{{}<>}([]())>]>]>
<<<(<<[({{{<{({}<>)<()[]>}<[()<>]{(){}}>><{[[]()]<[]{}>}[({}{})<(){}>]>}(<[<<>[]>{()[]}]>)}[<{[<{}>[{}{
{(<((<[{{[[[[<{}{}]<{}()>]][{({}{})<<>{}>}[<()[]>(()())]]]{([<[]()><[]{}>](<{}()>))({<<><>>(<>())}{
{({{[[<[{<{{(<<>>)(<[]()>[()])}([({}<>)])}[[{[()<>][()[]]}]({<[]<>>{<>()}})]><<(<<{}()>{<>[]
{{{[{[<<{[[{[{{}<>}<[][]>]{(()[])<<>()>}}[<{()()}[(){}]><<()[]>>]]{<((<>{})({}()))[<(){}>([][])]>{({{}[]}<
<{{[<{({{{[<{{<>()}{{}()}}>]<{<[()[]](<><>)><[<>[]]{(){}}>}<([(){}]<{}{}>){(())}>>}[([{[[]<>][{}<>]}[{()}(
[<<[{{(({({<{{{}{}}{<>[]}}({[][]}{<>[]})>[{[{}<>][<>[]]}[({}[])[[]<>]]]})}))<(<[[<(({}()))[<<>()>{(
{<<([<{({[{{[({}[])<<>{}>]([()<>]<(){}>)}<([{}[]]({}{}))>}[<<<<>{}>[[][]]>{<<>[]><()>}>[<<<>><[][]>
[({(<({[<[{{[<<>[]>[()[]]]<<<>[]>>}}<{([{}[]](<>{}))[{<>}{{}<>}]}(<<[][]>>[{[]<>}])>]>[(<(({<>[]}{<><>}))(<
{<[[{{<<{(<(<([]<>)[[][]]>[([]{})[<>{}]])<[{<>()}{[]<>}]>>>[{(({()()}[<>[]]){({}())<[]<>>})}[(<{<>
{[[{{{<[<[{<(({}{})[{}()])[(<><>)({}())]>}{[<{[]{}}[()[]]>]<{[()()]<[]()>}<[[][]]>>}]>{{({{{()[]}{{}(
<{[<([{{[[<(<{<><>}[{}{}]><((){})>){[[()()][()[]]]{[[]<>]}}>]<<(<({}[])<{}[]>>{(<><>)<[]()>}){(<
"""
}

70
2021/day11.swift Normal file
View file

@ -0,0 +1,70 @@
@main
struct Day11: Puzzle {
mutating func run() {
var count = 0
var currentStep = 0
repeat {
let stepCount = step()
count += stepCount
currentStep += 1
if (currentStep == 100) {
print("Part 1:", count)
}
if stepCount == width * height {
print("Part 2:", currentStep)
break
}
} while true
}
subscript(x: Int, y: Int) -> Int {
get { data[x + width * y] }
set { data[x + width * y] = newValue }
}
mutating func step() -> Int {
var flashes: Set<Point> = []
func up(x: Int, y: Int) {
self[x, y] += 1
if self[x, y] > 9, flashes.insert(Point(x: x, y: y)).inserted {
for i in -1...1 where 0..<height ~= y + i {
for j in -1...1 where (i != 0 || j != 0) && 0..<width ~= x + j {
up(x: x + j, y: y + i)
}
}
}
}
for y in 0..<height {
for x in 0..<width {
up(x: x, y: y)
}
}
for point in flashes {
self[point.x, point.y] = 0
}
return flashes.count
}
struct Point: Hashable { var x: Int, y: Int }
let width = 10
let height = 10
var data = [
2, 5, 2, 4, 2, 5, 5, 3, 3, 1,
1, 1, 3, 5, 6, 2, 5, 8, 8, 1,
2, 8, 3, 8, 3, 5, 3, 8, 6, 3,
1, 6, 6, 2, 3, 1, 2, 3, 6, 5,
6, 8, 4, 7, 8, 3, 5, 8, 2, 5,
2, 1, 8, 5, 6, 8, 4, 3, 6, 7,
6, 8, 7, 4, 2, 1, 2, 8, 3, 1,
5, 3, 8, 7, 2, 4, 7, 8, 1, 1,
2, 2, 5, 5, 4, 8, 2, 8, 7, 5,
8, 5, 2, 8, 5, 5, 7, 1, 3, 1,
]
}

125
2021/day12.swift Normal file
View file

@ -0,0 +1,125 @@
protocol PathProtocol {
func appending(_ room: Int, small: Bool) -> Self?
}
@main
struct Day12: Puzzle {
func run() {
let rooms = input.reduce(into: Set<String>()) { partialResult, pair in
partialResult.insert(pair.0)
partialResult.insert(pair.1)
}.sorted()
let small = Set(rooms.enumerated().compactMap { $0.element.first!.isLowercase ? $0.offset : nil })
let matrix = buildMatrix(rooms, small: small)
let startIndex = rooms.firstIndex(of: "start")!
let endIndex = rooms.firstIndex(of: "end")!
let paths = matrix.findPaths(from: startIndex, to: endIndex, continuing: Path())
print("total paths", paths.count)
let paths2 = matrix.findPaths(from: startIndex, to: endIndex, continuing: PathParth2(start: startIndex))
print("part 2:", paths2.count)
}
struct Path: PathProtocol {
var rooms: [Int] = []
func appending(_ room: Int, small: Bool) -> Path? {
guard !small || !rooms.contains(room) else {
return nil
}
return Path(rooms: rooms + [room])
}
}
struct PathParth2: PathProtocol {
let start: Int
var rooms: [Int] = []
var repeatedSmall: Bool = false
func appending(_ room: Int, small: Bool) -> PathParth2? {
var rs = repeatedSmall
if small && rooms.contains(room) {
if room == start || repeatedSmall {
return nil
}
rs = true
}
return PathParth2(start: start, rooms: rooms + [room], repeatedSmall: rs)
}
}
struct Matrix {
var data: [Bool]
let size: Int
let small: Set<Int>
func neighbors(of index: Int) -> [Int] {
let slice = data[index * size ..< (index + 1) * size]
return slice.enumerated().compactMap { $0.element ? $0.offset : nil }
}
func findPaths<Path: PathProtocol>(from: Int, to: Int, continuing: Path) -> [Path] {
guard from != to else {
return [continuing]
}
guard let current = continuing.appending(from, small: small.contains(from)) else {
return []
}
return neighbors(of: from).reduce(into: []) { partialResult, next in
partialResult += findPaths(from: next, to: to, continuing: current)
}
}
}
func buildMatrix(_ rooms: [String], small: Set<Int>) -> Matrix {
let count = rooms.count
var matrix = [Bool](repeating: false, count: count * count)
for (from, to) in input {
let fromIndex = rooms.firstIndex(of: from)!
let toIndex = rooms.firstIndex(of: to)!
matrix[fromIndex + count * toIndex] = true
matrix[toIndex + count * fromIndex] = true
}
return Matrix(data: matrix, size: count, small: small)
}
let input: [(String, String)] = [
("rf", "RL"),
("rf", "wz"),
("wz", "RL"),
("AV", "mh"),
("end", "wz"),
("end", "dm"),
("wz", "gy"),
("wz", "dm"),
("cg", "AV"),
("rf", "AV"),
("rf", "gy"),
("end", "mh"),
("cg", "gy"),
("cg", "RL"),
("gy", "RL"),
("VI", "gy"),
("AV", "gy"),
("dm", "rf"),
("start", "cg"),
("start", "RL"),
("rf", "mh"),
("AV", "start"),
("qk", "mh"),
("wz", "mh"),
]
}

931
2021/day13.swift Normal file
View file

@ -0,0 +1,931 @@
@main
struct Day13: Puzzle {
var maxX: Int = 0
var maxY: Int = 0
mutating func run() {
maxX = dots.map(\.x).max()!
maxY = dots.map(\.y).max()!
for (pos, fold) in folds.enumerated() {
self.fold(fold)
if (pos == 0) {
print("Part 1:", Set(dots).count)
}
}
print("\n\nPart 2:\n")
for y in 0..<maxY {
let dots = dots.filter { $0.y == y }.map(\.x).sorted()
var x = 0
for dotX in dots where dotX >= x {
print(String(repeating: ".", count: dotX - x) + "#", terminator: "")
x = dotX + 1
}
print(String(repeating: ".", count: maxX - x))
}
}
mutating func fold(_ fold: Fold) {
switch fold {
case .x(let axis):
self.fold(at: axis, keyPath: \.x, max: maxX)
maxX = axis
case .y(let axis):
self.fold(at: axis, keyPath: \.y, max: maxY)
maxY = axis
}
}
mutating func fold(at limit: Int, keyPath: WritableKeyPath<Dot, Int>, max: Int) {
let mid = dots.partition { dot in dot[keyPath: keyPath] > limit }
for index in mid..<dots.endIndex {
dots[index][keyPath: keyPath] = 2 * limit - dots[index][keyPath: keyPath]
}
}
struct Dot: Hashable {
var x: Int
var y: Int
init(_ x: Int, _ y: Int) {
self.x = x
self.y = y
}
}
var dots: [Dot] = [
Dot(609, 754),
Dot(1101, 140),
Dot(1129, 317),
Dot(622, 327),
Dot(967, 218),
Dot(661, 789),
Dot(661, 127),
Dot(1183, 596),
Dot(649, 280),
Dot(932, 693),
Dot(1288, 157),
Dot(1111, 372),
Dot(781, 219),
Dot(994, 236),
Dot(1145, 770),
Dot(1153, 395),
Dot(755, 289),
Dot(502, 406),
Dot(850, 126),
Dot(902, 58),
Dot(907, 5),
Dot(268, 700),
Dot(646, 436),
Dot(1108, 523),
Dot(443, 343),
Dot(932, 201),
Dot(1115, 105),
Dot(1191, 278),
Dot(1241, 203),
Dot(1000, 252),
Dot(1290, 403),
Dot(144, 597),
Dot(927, 448),
Dot(782, 630),
Dot(131, 705),
Dot(333, 367),
Dot(38, 613),
Dot(1285, 294),
Dot(872, 93),
Dot(492, 759),
Dot(986, 203),
Dot(70, 740),
Dot(378, 693),
Dot(1004, 319),
Dot(731, 235),
Dot(479, 875),
Dot(1210, 389),
Dot(1215, 215),
Dot(214, 122),
Dot(599, 660),
Dot(500, 254),
Dot(8, 478),
Dot(120, 29),
Dot(70, 809),
Dot(373, 248),
Dot(1235, 441),
Dot(1293, 345),
Dot(454, 686),
Dot(985, 469),
Dot(1101, 478),
Dot(765, 287),
Dot(1029, 309),
Dot(667, 203),
Dot(1220, 800),
Dot(508, 285),
Dot(65, 675),
Dot(1078, 663),
Dot(592, 677),
Dot(236, 122),
Dot(462, 564),
Dot(1042, 418),
Dot(748, 852),
Dot(810, 254),
Dot(718, 229),
Dot(386, 22),
Dot(1191, 728),
Dot(432, 872),
Dot(392, 28),
Dot(972, 259),
Dot(336, 416),
Dot(1096, 480),
Dot(415, 871),
Dot(1241, 427),
Dot(165, 441),
Dot(510, 673),
Dot(750, 313),
Dot(1255, 779),
Dot(194, 854),
Dot(639, 107),
Dot(281, 585),
Dot(679, 519),
Dot(611, 644),
Dot(497, 171),
Dot(326, 730),
Dot(882, 456),
Dot(403, 453),
Dot(454, 630),
Dot(726, 227),
Dot(383, 502),
Dot(574, 528),
Dot(1120, 593),
Dot(214, 256),
Dot(165, 422),
Dot(425, 115),
Dot(795, 323),
Dot(673, 602),
Dot(5, 827),
Dot(1009, 619),
Dot(364, 194),
Dot(75, 390),
Dot(870, 122),
Dot(654, 449),
Dot(62, 665),
Dot(999, 228),
Dot(611, 508),
Dot(87, 660),
Dot(677, 880),
Dot(656, 449),
Dot(681, 336),
Dot(499, 70),
Dot(870, 638),
Dot(119, 728),
Dot(1185, 498),
Dot(28, 406),
Dot(1, 796),
Dot(334, 210),
Dot(803, 445),
Dot(17, 345),
Dot(58, 194),
Dot(1310, 162),
Dot(661, 827),
Dot(276, 649),
Dot(679, 883),
Dot(164, 506),
Dot(990, 236),
Dot(82, 327),
Dot(999, 485),
Dot(498, 208),
Dot(1228, 119),
Dot(529, 732),
Dot(252, 544),
Dot(579, 403),
Dot(808, 592),
Dot(251, 665),
Dot(912, 602),
Dot(502, 722),
Dot(85, 75),
Dot(440, 122),
Dot(755, 12),
Dot(898, 766),
Dot(878, 22),
Dot(924, 22),
Dot(679, 277),
Dot(1191, 616),
Dot(1278, 775),
Dot(1148, 871),
Dot(229, 37),
Dot(957, 854),
Dot(987, 448),
Dot(1285, 518),
Dot(1252, 476),
Dot(1295, 857),
Dot(822, 285),
Dot(731, 627),
Dot(199, 74),
Dot(82, 775),
Dot(1186, 299),
Dot(954, 488),
Dot(753, 508),
Dot(1232, 593),
Dot(1216, 663),
Dot(529, 284),
Dot(795, 571),
Dot(1118, 226),
Dot(467, 617),
Dot(691, 36),
Dot(181, 317),
Dot(994, 658),
Dot(651, 894),
Dot(1101, 754),
Dot(763, 603),
Dot(1088, 872),
Dot(45, 528),
Dot(937, 224),
Dot(1042, 470),
Dot(1066, 648),
Dot(1131, 95),
Dot(738, 434),
Dot(283, 60),
Dot(994, 302),
Dot(1268, 865),
Dot(795, 287),
Dot(75, 179),
Dot(1011, 425),
Dot(741, 75),
Dot(773, 659),
Dot(373, 700),
Dot(201, 479),
Dot(649, 614),
Dot(328, 893),
Dot(1103, 728),
Dot(1144, 285),
Dot(468, 45),
Dot(857, 591),
Dot(69, 203),
Dot(401, 339),
Dot(579, 659),
Dot(502, 172),
Dot(584, 891),
Dot(802, 609),
Dot(644, 359),
Dot(679, 11),
Dot(1260, 201),
Dot(194, 505),
Dot(507, 485),
Dot(738, 350),
Dot(170, 882),
Dot(84, 511),
Dot(1203, 123),
Dot(1240, 533),
Dot(811, 70),
Dot(736, 687),
Dot(856, 686),
Dot(698, 417),
Dot(726, 592),
Dot(572, 544),
Dot(1144, 609),
Dot(629, 376),
Dot(1039, 840),
Dot(455, 148),
Dot(580, 224),
Dot(848, 778),
Dot(684, 45),
Dot(1265, 304),
Dot(681, 406),
Dot(15, 228),
Dot(172, 814),
Dot(637, 549),
Dot(617, 610),
Dot(1185, 610),
Dot(1250, 275),
Dot(107, 291),
Dot(186, 261),
Dot(1247, 299),
Dot(467, 123),
Dot(350, 582),
Dot(782, 432),
Dot(38, 505),
Dot(843, 617),
Dot(1184, 630),
Dot(304, 712),
Dot(373, 221),
Dot(534, 865),
Dot(743, 402),
Dot(857, 620),
Dot(671, 857),
Dot(70, 361),
Dot(692, 252),
Dot(338, 658),
Dot(1121, 144),
Dot(1258, 266),
Dot(1058, 843),
Dot(773, 211),
Dot(1129, 516),
Dot(1088, 22),
Dot(15, 86),
Dot(343, 676),
Dot(70, 1),
Dot(55, 794),
Dot(691, 91),
Dot(982, 893),
Dot(1300, 628),
Dot(656, 162),
Dot(262, 362),
Dot(850, 854),
Dot(617, 284),
Dot(977, 280),
Dot(373, 640),
Dot(654, 221),
Dot(484, 882),
Dot(251, 235),
Dot(103, 639),
Dot(1293, 292),
Dot(1150, 525),
Dot(758, 432),
Dot(480, 124),
Dot(1235, 504),
Dot(1293, 549),
Dot(60, 275),
Dot(1036, 317),
Dot(512, 231),
Dot(599, 794),
Dot(783, 148),
Dot(350, 518),
Dot(45, 252),
Dot(1265, 140),
Dot(979, 448),
Dot(706, 878),
Dot(1203, 686),
Dot(288, 837),
Dot(180, 138),
Dot(572, 431),
Dot(199, 148),
Dot(530, 408),
Dot(711, 794),
Dot(1302, 682),
Dot(497, 295),
Dot(36, 549),
Dot(201, 415),
Dot(1009, 432),
Dot(94, 663),
Dot(1150, 77),
Dot(1096, 638),
Dot(803, 87),
Dot(211, 775),
Dot(320, 12),
Dot(166, 609),
Dot(629, 558),
Dot(572, 366),
Dot(1226, 766),
Dot(295, 789),
Dot(195, 105),
Dot(99, 712),
Dot(507, 87),
Dot(242, 383),
Dot(55, 354),
Dot(681, 558),
Dot(1135, 313),
Dot(192, 226),
Dot(895, 871),
Dot(508, 609),
Dot(1146, 394),
Dot(281, 588),
Dot(714, 312),
Dot(567, 234),
Dot(283, 834),
Dot(455, 893),
Dot(45, 140),
Dot(691, 673),
Dot(763, 123),
Dot(75, 715),
Dot(820, 506),
Dot(1285, 40),
Dot(242, 712),
Dot(475, 1),
Dot(453, 722),
Dot(965, 793),
Dot(475, 212),
Dot(201, 610),
Dot(333, 280),
Dot(934, 103),
Dot(570, 488),
Dot(1126, 10),
Dot(380, 404),
Dot(256, 332),
Dot(818, 311),
Dot(1179, 257),
Dot(725, 19),
Dot(131, 49),
Dot(947, 283),
Dot(84, 128),
Dot(402, 798),
Dot(544, 68),
Dot(835, 1),
Dot(637, 345),
Dot(846, 77),
Dot(117, 445),
Dot(599, 107),
Dot(755, 371),
Dot(596, 582),
Dot(216, 93),
Dot(32, 775),
Dot(999, 409),
Dot(50, 693),
Dot(184, 231),
Dot(343, 436),
Dot(768, 17),
Dot(507, 409),
Dot(880, 379),
Dot(1000, 700),
Dot(1266, 824),
Dot(529, 675),
Dot(119, 278),
Dot(284, 477),
Dot(1305, 280),
Dot(226, 582),
Dot(353, 854),
Dot(1238, 360),
Dot(1048, 476),
Dot(1079, 887),
Dot(960, 518),
Dot(619, 445),
Dot(530, 75),
Dot(863, 390),
Dot(972, 770),
Dot(759, 191),
Dot(1235, 390),
Dot(813, 218),
Dot(775, 728),
Dot(199, 1),
Dot(1283, 649),
Dot(800, 221),
Dot(1183, 416),
Dot(10, 87),
Dot(199, 809),
Dot(1272, 505),
Dot(1099, 775),
Dot(730, 574),
Dot(803, 436),
Dot(10, 135),
Dot(1118, 330),
Dot(1186, 150),
Dot(1200, 600),
Dot(179, 95),
Dot(1228, 648),
Dot(1218, 673),
Dot(731, 403),
Dot(780, 744),
Dot(763, 238),
Dot(1258, 765),
Dot(209, 140),
Dot(572, 618),
Dot(631, 211),
Dot(1081, 115),
Dot(584, 592),
Dot(432, 868),
Dot(787, 413),
Dot(700, 518),
Dot(555, 12),
Dot(1265, 670),
Dot(350, 312),
Dot(599, 659),
Dot(90, 823),
Dot(678, 312),
Dot(99, 523),
Dot(455, 1),
Dot(851, 49),
Dot(743, 660),
Dot(714, 694),
Dot(102, 14),
Dot(1183, 484),
Dot(870, 548),
Dot(242, 800),
Dot(759, 255),
Dot(200, 712),
Dot(110, 404),
Dot(1190, 865),
Dot(441, 553),
Dot(427, 289),
Dot(577, 819),
Dot(1198, 521),
Dot(1022, 57),
Dot(946, 252),
Dot(852, 117),
Dot(535, 166),
Dot(1146, 871),
Dot(937, 130),
Dot(1009, 294),
Dot(855, 746),
Dot(301, 619),
Dot(908, 798),
Dot(1255, 100),
Dot(1047, 236),
Dot(1004, 127),
Dot(683, 649),
Dot(596, 312),
Dot(189, 592),
Dot(584, 85),
Dot(107, 603),
Dot(734, 705),
Dot(1255, 794),
Dot(671, 37),
Dot(316, 816),
Dot(1183, 148),
Dot(599, 802),
Dot(999, 555),
Dot(131, 845),
Dot(842, 630),
Dot(1094, 435),
Dot(684, 493),
Dot(1038, 257),
Dot(17, 292),
Dot(1208, 14),
Dot(58, 582),
Dot(503, 691),
Dot(338, 684),
Dot(1225, 75),
Dot(818, 759),
Dot(227, 289),
Dot(649, 815),
Dot(835, 95),
Dot(447, 838),
Dot(468, 630),
Dot(196, 119),
Dot(1228, 366),
Dot(155, 70),
Dot(927, 18),
Dot(145, 129),
Dot(488, 285),
Dot(1255, 660),
Dot(338, 236),
Dot(684, 401),
Dot(1123, 501),
Dot(244, 210),
Dot(44, 70),
Dot(972, 322),
Dot(964, 333),
Dot(190, 301),
Dot(1260, 878),
Dot(555, 289),
Dot(738, 17),
Dot(574, 687),
Dot(821, 322),
Dot(294, 119),
Dot(1124, 261),
Dot(8, 864),
Dot(97, 633),
Dot(741, 560),
Dot(401, 107),
Dot(560, 313),
Dot(1252, 200),
Dot(1273, 508),
Dot(803, 485),
Dot(25, 294),
Dot(1193, 449),
Dot(266, 520),
Dot(927, 100),
Dot(848, 668),
Dot(1300, 135),
Dot(430, 379),
Dot(398, 809),
Dot(1285, 376),
Dot(70, 533),
Dot(800, 673),
Dot(869, 553),
Dot(552, 432),
Dot(736, 207),
Dot(227, 658),
Dot(202, 523),
Dot(1096, 414),
Dot(1300, 807),
Dot(611, 386),
Dot(1124, 40),
Dot(194, 768),
Dot(786, 789),
Dot(428, 134),
Dot(125, 610),
Dot(301, 723),
Dot(637, 154),
Dot(1114, 119),
Dot(1166, 597),
Dot(1253, 276),
Dot(373, 757),
Dot(842, 854),
Dot(447, 56),
Dot(338, 592),
Dot(1186, 819),
Dot(412, 766),
Dot(1042, 700),
Dot(529, 50),
Dot(932, 130),
Dot(914, 469),
Dot(766, 765),
Dot(1274, 549),
Dot(253, 555),
Dot(55, 787),
Dot(142, 630),
Dot(567, 423),
Dot(1054, 292),
Dot(584, 816),
Dot(870, 775),
Dot(530, 744),
Dot(560, 761),
Dot(376, 791),
Dot(127, 746),
Dot(142, 679),
Dot(383, 354),
Dot(1213, 185),
Dot(147, 458),
Dot(619, 449),
Dot(17, 549),
Dot(1292, 113),
Dot(830, 115),
Dot(1290, 711),
Dot(579, 627),
Dot(594, 744),
Dot(459, 49),
Dot(17, 101),
Dot(960, 312),
Dot(1114, 103),
Dot(962, 331),
Dot(510, 221),
Dot(960, 648),
Dot(808, 40),
Dot(1158, 854),
Dot(453, 31),
Dot(445, 757),
Dot(972, 722),
Dot(82, 648),
Dot(84, 766),
Dot(252, 350),
Dot(656, 673),
Dot(994, 572),
Dot(242, 576),
Dot(438, 93),
Dot(972, 684),
Dot(930, 765),
Dot(810, 366),
Dot(1245, 219),
Dot(796, 630),
Dot(912, 85),
Dot(851, 553),
Dot(590, 362),
Dot(522, 768),
Dot(1223, 471),
Dot(912, 187),
Dot(1000, 194),
Dot(455, 61),
Dot(1140, 882),
Dot(142, 862),
Dot(440, 548),
Dot(310, 194),
Dot(579, 267),
Dot(335, 527),
Dot(52, 129),
Dot(338, 572),
Dot(52, 317),
Dot(249, 334),
Dot(428, 456),
Dot(1240, 592),
Dot(649, 127),
Dot(1235, 715),
Dot(720, 532),
Dot(976, 236),
Dot(855, 820),
Dot(58, 694),
Dot(1019, 803),
Dot(1016, 119),
Dot(338, 302),
Dot(360, 441),
Dot(631, 277),
Dot(360, 119),
Dot(388, 374),
Dot(1054, 332),
Dot(398, 292),
Dot(1240, 361),
Dot(781, 675),
Dot(199, 746),
Dot(338, 266),
Dot(1068, 94),
Dot(1144, 733),
Dot(1191, 838),
Dot(1293, 101),
Dot(190, 593),
Dot(572, 17),
Dot(187, 393),
Dot(35, 673),
Dot(604, 878),
Dot(960, 376),
Dot(1285, 742),
Dot(1153, 767),
Dot(907, 229),
Dot(1236, 171),
Dot(74, 798),
Dot(529, 844),
Dot(304, 742),
Dot(1111, 509),
Dot(1186, 75),
Dot(330, 789),
Dot(902, 836),
Dot(507, 807),
Dot(594, 150),
Dot(1150, 824),
Dot(350, 648),
Dot(460, 374),
Dot(637, 602),
Dot(443, 775),
Dot(1029, 585),
Dot(1006, 152),
Dot(855, 74),
Dot(989, 367),
Dot(945, 308),
Dot(654, 673),
Dot(599, 115),
Dot(1148, 388),
Dot(1185, 442),
Dot(972, 266),
Dot(1247, 485),
Dot(57, 618),
Dot(1068, 383),
Dot(160, 525),
Dot(1006, 182),
Dot(82, 528),
Dot(542, 877),
Dot(189, 144),
Dot(515, 607),
Dot(950, 329),
Dot(335, 367),
Dot(927, 502),
Dot(808, 722),
Dot(790, 513),
Dot(147, 893),
Dot(1211, 630),
Dot(117, 683),
Dot(1247, 261),
Dot(380, 577),
Dot(338, 628),
Dot(1240, 85),
Dot(966, 194),
Dot(85, 819),
Dot(229, 331),
Dot(1115, 789),
Dot(100, 57),
Dot(534, 641),
Dot(857, 722),
Dot(867, 551),
Dot(15, 779),
Dot(1226, 847),
Dot(1101, 11),
Dot(701, 590),
Dot(1111, 522),
Dot(410, 246),
Dot(679, 375),
Dot(1275, 673),
Dot(161, 453),
Dot(1208, 822),
Dot(1163, 458),
Dot(244, 684),
Dot(169, 767),
Dot(490, 506),
Dot(25, 40),
Dot(1111, 677),
Dot(20, 403),
Dot(271, 115),
Dot(1094, 660),
Dot(527, 333),
Dot(92, 673),
Dot(838, 745),
Dot(1039, 100),
Dot(950, 791),
Dot(1279, 161),
Dot(401, 787),
Dot(291, 49),
Dot(883, 68),
Dot(644, 583),
Dot(1138, 528),
Dot(858, 696),
Dot(323, 448),
Dot(840, 231),
Dot(1150, 593),
Dot(165, 472),
Dot(885, 709),
Dot(1171, 123),
Dot(1275, 91),
Dot(410, 210),
Dot(664, 10),
Dot(200, 182),
Dot(363, 283),
Dot(529, 610),
Dot(1216, 399),
Dot(992, 478),
Dot(1266, 70),
Dot(194, 40),
Dot(681, 376),
Dot(753, 277),
Dot(857, 303),
Dot(1006, 742),
Dot(609, 670),
Dot(842, 712),
Dot(1131, 767),
Dot(579, 665),
Dot(1109, 610),
Dot(80, 239),
Dot(1240, 816),
Dot(69, 427),
Dot(557, 386),
Dot(781, 284),
Dot(494, 824),
Dot(160, 77),
Dot(36, 252),
Dot(838, 149),
Dot(325, 469),
Dot(611, 610),
Dot(599, 0),
Dot(612, 635),
Dot(979, 446),
Dot(887, 887),
Dot(974, 478),
Dot(609, 130),
Dot(551, 479),
Dot(345, 101),
Dot(343, 781),
Dot(880, 462),
Dot(726, 533),
Dot(164, 871),
Dot(1300, 124),
Dot(780, 408),
Dot(627, 649),
Dot(1246, 362),
Dot(1200, 404),
Dot(398, 1),
Dot(172, 528),
Dot(994, 322),
Dot(572, 528),
Dot(360, 775),
Dot(380, 490),
Dot(783, 522),
Dot(27, 649),
Dot(1278, 329),
Dot(1255, 110),
Dot(27, 245),
Dot(679, 617),
Dot(64, 362),
Dot(58, 418),
Dot(1230, 574),
Dot(281, 254),
Dot(408, 126),
Dot(701, 140),
Dot(659, 448),
Dot(971, 721),
Dot(766, 129),
Dot(671, 115),
Dot(1252, 582),
Dot(271, 840),
Dot(957, 40),
Dot(570, 406),
Dot(110, 490),
Dot(634, 543),
Dot(304, 152),
Dot(699, 610),
Dot(1131, 212),
Dot(1226, 511),
Dot(296, 598),
Dot(773, 30),
Dot(1223, 660),
Dot(693, 586),
Dot(84, 847),
Dot(1208, 72),
Dot(338, 722),
Dot(164, 785),
Dot(924, 418),
Dot(1240, 740),
Dot(75, 504),
Dot(330, 105),
Dot(455, 746),
Dot(842, 264),
Dot(818, 358),
]
enum Fold {
case x(Int)
case y(Int)
}
let folds: [Fold] = [
.x(655),
.y(447),
.x(327),
.y(223),
.x(163),
.y(111),
.x(81),
.y(55),
.x(40),
.y(27),
.y(13),
.y(6),
]
}

151
2021/day14.swift Normal file
View file

@ -0,0 +1,151 @@
@main
struct Day14: Puzzle {
mutating func run() {
let part1 = calculate(start, depth: 10)
print("Part 1:", part1.values.max()! - part1.values.min()!)
let part2 = calculate(start, depth: 40)
print("Part 2:", part2.values.max()! - part2.values.min()!)
}
mutating func calculate(_ string: String, depth: Int) -> [Character: Int] {
var result: [Character: Int] = [:]
for (a, b) in zip(string, string.dropFirst()) {
result.merge(calculate(a, b, depth: depth), uniquingKeysWith: +)
}
result[string.last!, default: 0] += 1
return result
}
struct CacheKey: Hashable {
var string: String
var depth: Int
}
var cache: [CacheKey: [Character: Int]] = [:]
mutating func calculate(_ first: Character, _ second: Character, depth: Int) -> [Character: Int] {
guard depth > 0 else {
return [first: 1]
}
let pair = "\(first)\(second)"
let cacheKey = CacheKey(string: pair, depth: depth)
if let result = cache[cacheKey] {
return result
}
let mid = mapping[pair]!
var result = calculate(first, mid, depth: depth - 1)
result.merge(calculate(mid, second, depth: depth - 1), uniquingKeysWith: +)
cache[cacheKey] = result
return result
}
let start = "KBKPHKHHNBCVCHPSPNHF"
let mapping: [String: Character] = [
"OP": "H",
"CF": "C",
"BB": "V",
"KH": "O",
"CV": "S",
"FV": "O",
"FS": "K",
"KO": "C",
"PP": "S",
"SH": "K",
"FH": "O",
"NF": "H",
"PN": "P",
"BO": "H",
"OK": "K",
"PO": "P",
"SF": "K",
"BF": "P",
"HH": "S",
"KP": "H",
"HB": "N",
"NP": "V",
"KK": "P",
"PF": "P",
"BK": "V",
"OF": "H",
"FO": "S",
"VC": "P",
"FK": "B",
"NK": "S",
"CB": "B",
"PV": "C",
"CO": "N",
"BN": "C",
"HV": "H",
"OC": "N",
"NB": "O",
"CS": "S",
"HK": "C",
"VS": "F",
"BH": "C",
"PC": "S",
"KC": "O",
"VO": "P",
"FB": "K",
"BV": "V",
"VN": "N",
"ON": "F",
"VH": "H",
"CN": "O",
"HO": "O",
"SV": "O",
"SS": "H",
"KF": "N",
"SP": "C",
"NS": "V",
"SO": "F",
"BC": "P",
"HC": "C",
"FP": "H",
"OH": "S",
"OB": "S",
"HF": "V",
"SC": "B",
"SN": "N",
"VK": "C",
"NC": "V",
"VV": "S",
"SK": "K",
"PK": "K",
"PS": "N",
"KB": "S",
"KS": "C",
"NN": "C",
"OO": "C",
"BS": "B",
"NV": "H",
"FF": "P",
"FC": "N",
"OS": "H",
"KN": "N",
"VP": "B",
"PH": "N",
"NH": "S",
"OV": "O",
"FN": "V",
"CP": "B",
"NO": "V",
"CK": "C",
"VF": "B",
"HS": "B",
"KV": "K",
"VB": "H",
"SB": "S",
"BP": "S",
"CC": "F",
"HP": "B",
"PB": "P",
"HN": "P",
"CH": "O",
]
}

171
2021/day15.swift Normal file
View file

@ -0,0 +1,171 @@
@main
struct Day15: Puzzle {
mutating func run() {
let part1 = find(target: Point(x: width - 1, y: height - 1))
print("Part 1:", part1)
let part2 = find(target: Point(x: 5 * width - 1, y: 5 * height - 1))
print("Part 2:", part2)
}
struct Point: Hashable {
var x: Int
var y: Int
var neighbors: [Point] {
[
Point(x: x - 1, y: y),
Point(x: x + 1, y: y),
Point(x: x, y: y - 1),
Point(x: x, y: y + 1)
]
}
}
func find(target: Point) -> Int {
var current = Point(x: 0, y: 0)
var totalCost = 0
var possible: [Point: Int] = [:]
var visited: Set<Point> = []
func isInside(_ point: Point) -> Bool {
0...target.x ~= point.x && 0...target.y ~= point.y
}
while current != target {
visited.insert(current)
for n in current.neighbors where isInside(n) && !visited.contains(n) {
possible[n] = min(possible[n, default: .max], totalCost + self[n])
}
(current, totalCost) = possible.min { $0.value < $1.value }!
possible.removeValue(forKey: current)
}
return totalCost
}
subscript(point: Point) -> Int {
self[point.x , point.y]
}
subscript(x: Int, y: Int) -> Int {
let repX = x / width
let xPos = x % width
let repY = y / height
let yPos = y % height
return (input[xPos + width * yPos] + repX + repY - 1) % 9 + 1
}
let width = 100
let height = 100
let input: [Int] = """
8576219475874583191916312133474175459337114195988185136398151631965391991813219974121211251194786128
1713881999231519357799114192443351147195293575386923868711388519361669464234397975938889146414199688
2151922113728581598379349983928489926261239862882888968998791929828316718921297117449631496888879522
3291722311299652548445221254456535112944431135116631433192483199481594768169444158222431199929579291
1472287315918113113959972189351931626141472991992791413146153989481318998519171231313898962992319251
8978798318873934543151823824117132228417869392811216547172919119998739911121199246294815175868992773
4711149989992717728465321161611967173674239298593935117599173279267114155265393397571372124856718732
9843245319692998612781837729915931371619886371128818266322121623171343132131239399743799827972585645
7183211712173272827112268971459424836293613294388761928991542137194981939169315879277494581991465822
2385721996578834579224651431411717198341139751429156637229888849181154922897746418293184413519931124
6188313732929496115412842928273812153715747411177189187726751991191841181119822534511125656211991265
8797112815141319881991983914861912838966446818811271274141461487947171138945321912192198815813211761
6131514331472951494911549373681638637191295997625615885219668259269297171939941498581919718223529141
3916711951831246219991217199412116783411218814949692281987659123812288937983969116847986196936523779
9111121227192813382983442234171148191131821497435387936912532695293912972491131977272231919798477344
9994893855122115213919995636978443773161196511775674783979472217917498285423121574533491643891265199
9245818921321877317663254739619499915821162412924816263648913499811482474392667993739139899758919393
1717618849193152316325119128859116191132672215933282127611134982961918179152189411131259943819997123
3697848795188715264992962617223177221118919863975198881197837936258983111819488622362584888223331999
1798259211962116675922944295117391732295963342131192112612386194749389991199821195162975124619149837
5365696872959269494682128272373619348283964191994187749824841195582221478325111187843285531119934786
6717218734155417511564162235845524121493736962442884479191718924594119911521121422231982184671122116
9346412964321187889663345611132183519588187791111227269112129947122999176114184938494824342939375892
2912811929919891428891223818529115949522464779949526791515797886471246181118183121786524127979998512
7353285281129811915971956929718433284441492484117162562212973157265812816731843791838571691421923924
3239161911151141369392413114665193927469988999219478935992541193184945926828911597158489999651151172
9229716159183212219791136121569512978927999177196111291668152292896495877722611129462617229299962112
5994461796358342348216915883991574569784887978731368749888449599246152414391635925763156831839199496
1569319989929452917219726368583887924179117119284971126181346919789949393972171262535283921926293357
9983673949614595396426594299567925779229751982433631919159114841538978735893759492592481573495963491
3725632421623191323789991288299296415942162988289929992238877153268488127718372211951117951486111926
1219319492614741819142363452974731825929485665978219725131218962626179997611254524524915768686381164
8248145978417221154822663831966124539168641311913211295941167919583189792718599334616827916567313217
3483959252172951953519211189255336234459522397526829492685113849198611624111948187714151963198671919
7812289559812455122297839958719917624841136314322613942214613249662544617928521344873779598816941946
6868958791119815122313931178313637489923479492171741443993619161318781194421239654468391996636923611
6999166111129212624151555925991311538952878946191234511973499736237742491811348911419449161895978178
3532973784281182181552481928144111246711742443831184236962981395142863562121961323169398316921741321
8433727191521455193223914171391447439164791659118411391327413285243717794299999523811137619197999791
8169125649963232439519311131691539694589231314268683691811431259295132344195953951287639889737849189
8928161919934761971442719391119881113318323987227229831917897151155121657624142152884624176461913498
2949199492196424164751892146395174669111921799261527568751283712133148212821261936297739912939743599
9931616797885267969938591912672996183187926259991298429278229232257219428413174972999193796827459799
3138768154311742899419449741617489465993174645228814938145273266941566223933119891379181296548999222
1338961668848636157444719898575174699419997972125194827176897565699991611654214368232592793825617931
9283643143692917314535119897889711191946688911436965194232811624712861576291987716317193114445363435
8718611398299869991788113111214414269554542114329621286592313532782819131919527228417764279755899812
8128249329314753847725634378737239939769913811444529224619715471961999278915341955188871714271145449
1286217894699991715153139268991918988338684391252271254127193342854156438873836258484975151167996316
4191439845728418348368191516746499654147257851256861534634988281199187541199892317145428248531129171
9799283477747387253566811199414256363339122372629545249542373591951156699415998714124115397811359516
3191985767689943186838293175311429527499612824265518522881922917119891424717751985621941995373481629
8127933395192162886259956635985821651844893796541627799276258692298972881471991789861232424926758792
4349683335349673317521761437148933192989961489478395417258212949865876242843242464123471244631449998
3983861771861891251448475182984833811718959521263444111998834975935221115813839514917176912994677265
3361812177831144971531441184671112122322312961859915722393944269211333951428175372918136112814926411
2492891241191991149319731182431211211971267682931551534199328118919751416881458658314218331565282229
1316191719349114251772629411233483317516143684488291792691839198723174287211241498955137331159443961
1295632882814319179937625629218199381191127966181781921829819969815912382191294796979515315471941392
1952318366571121641614137937116326339638859628226299126699118638171813164397332391891669798271559135
1123676641998146639499323488991286491444174358479157916696196841912233773269986812299996975117612192
2496461124219121332118311532239527696198787778122345883331631119147159212262659717289674149798169111
3994915157217763242581922474512992479146377117411981999549977495462427199524334285127723311599337186
2856249932899117429471617195415229923432185189218399415357391954728651191591964713418532917111296399
1916415314958527255299763241981929186681257596912948977397875248189748655173819381269954513499896991
4214987991254389723346911149661118597277889213928551947599177915888233175182921923128853579525754718
3582279578128192231339943295188563998645998149526284997789731787116991199391984141983177825883111821
3318115949152512995142691231324793411691198458654473675135993122216324137859959272731599996923465431
9431374329994955614816239978928993493129129641248882311462522524447519388869238199852941551532223632
6749132793518479891929891686579791123712167784913331559922969149125915827657111724348929978159964476
6899888192198113996189996152913216258469182711411338723293823541574149295918323829819122941288693461
9833728958423343229397292861582211971713299293611778961979819175799893793971822162383179128129741328
9719459171294372288418126215914532995125999274359633129923191213332337513812761149562146497198614329
6323381739518574154814713691189651618965915153972197836392492518147816118266818195856772641337997448
9515391698994467556612815425651153225477392335219196697477512338938131933398177519117437353271396315
2881769116189531115832167881372917543267529333184621316918481786111252281738298936313393416198972192
1262821848371618529985323295192217356152412696271318793198457225899782896767339942896611231999996177
7611115824284211421951282951346181221891123993118231772195952872865982323819415318945742389969468591
6289176957971112617212793915412958239574931721216722823933461763146195116737161463663388274118352919
2739192179312181318764617662499189345995579913463399176128133623751919135855775349195561978131664671
7419179135228196296753972713643556671138381494838116814212892453118963456121883515171981991493112197
2351962484381996196119791327953881225881124837211622273581789216339733276411173141146816794919111532
4828494193941149581788515219884312818182198261739598611663311138255885557141492174934279519487959315
6266441934234532987821537914696156313934795144412166199339199381197916324959558778257894958593689511
1218836684761811498991162549547375891999161535133511817388897491266957819219117988952234747197245136
7714718988198712181711441996373181351671962319416917816121119483959397254815953421521642116185224622
6315878819598412161782318179586237893968466116199183185764132999388411414821117761121199725212571499
9298381112525419214597534611761811137687414291394795245955115116482261788724982532573616634235949295
2513837618149681781571514782119218925111312739891194994857915371836413112952729282168681511999132471
9859244419394114469927618849921646911349163815889492121537711577378978633345419421881312649299234749
6241913851991287776811271361777919519213726771122769111499925516752149976397933753482495291399144132
7549785323789821947513966787125532113931855716249321712383292746672763573114656989299712174939322181
9188852123922512181114117536955617335173328988385462159798591117758397298294919117614163738414981949
2974419341361292817129453536824618155299993142149899966226312193848695911943416589913541194129913422
1672761999113289962322634642122163368424955339864962951462929691975798622972131599198673671183449929
3462285147627397867977869242451116812198183328383813959284449969199819896174618466572298882951459875
6153638634925613615423256118159531429294172496996993184762972841759153392753653191942715379879852345
6516291128536155113712481272992636373774429965994966353494115221979925592128929821611928492143929534
3993993153713717844719492314391992638719111242291783837279215399219822611221331985991484863975594721
5995891457939115884519762228128552168915218299831775259316392719113962218429351553267511523198149462
""".compactMap { $0.wholeNumberValue }
}

131
2021/day16.swift Normal file
View file

@ -0,0 +1,131 @@
@main
struct Day16: Puzzle {
mutating func run() {
var start = Cursor.start
let part2 = packet(at: &start)
print("Part 1:", versionSum)
print("Part 2:", part2)
}
var versionSum = 0
mutating func packet(at cursor: inout Cursor) -> UInt64 {
let version = getBits(at: &cursor, count: 3)
let type = getBits(at: &cursor, count: 3)
versionSum += Int(version)
if type == 4 {
return literal(at: &cursor)
}
let subPackets = operatorPacket(at: &cursor)
switch type {
case 0: return subPackets.reduce(0, +)
case 1: return subPackets.reduce(1, *)
case 2: return subPackets.min()!
case 3: return subPackets.max()!
case 5: return subPackets[0] > subPackets[1] ? 1 : 0
case 6: return subPackets[0] < subPackets[1] ? 1 : 0
case 7: return subPackets[0] == subPackets[1] ? 1 : 0
default: fatalError("Unknown packet type \(type)")
}
}
func literal(at cursor: inout Cursor) -> UInt64 {
var result: UInt64 = 0
while getBit(at: &cursor) {
result = result << 4 | getBits(at: &cursor, count: 4)
}
result = result << 4 | getBits(at: &cursor, count: 4)
return result
}
mutating func operatorPacket(at cursor: inout Cursor) -> [UInt64] {
let type = getBit(at: &cursor)
var result: [UInt64] = []
if type {
let count = getBits(at: &cursor, count: 11)
for _ in 0..<count {
result.append(packet(at: &cursor))
}
} else {
let length = getBits(at: &cursor, count: 15)
let end = cursor.adding(bits: Int(length))
while cursor < end {
result.append(packet(at: &cursor))
}
}
return result
}
struct Cursor: Equatable {
var offset: Int
var bit: Int
static let start = Cursor(offset: 0, bit: 0)
var remainingBits: Int { 4 - bit }
mutating func skip(_ bits: Int) {
bit += bits
offset += bit / 4
bit = bit % 4
}
func adding(bits: Int) -> Cursor {
var result = self
result.skip(bits)
return result
}
var bitPos: Int {
offset * 4 + bit
}
static func < (lhs: Cursor, rhs: Cursor) -> Bool {
lhs.bitPos < rhs.bitPos
}
}
func getBit(at cursor: inout Cursor) -> Bool {
getBits(at: &cursor, count: 1) == 1
}
func getBitRange(_ x: Int, start: Int, count: Int) -> UInt64 {
let mask = UInt64((1 << count) - 1)
let shift = 4 - count - start
precondition(shift >= 0)
return UInt64(x) >> shift & mask
}
func getBits(at cursor: inout Cursor, count: Int) -> UInt64 {
var remaining = count
var result: UInt64 = 0
while remaining > 0 {
let take = min(cursor.remainingBits, remaining)
let bits = getBitRange(input[cursor.offset], start: cursor.bit, count: take)
result = result << take | bits
remaining -= take
cursor.bit += take
if cursor.bit == 4 {
cursor.bit = 0
cursor.offset += 1
}
}
return result
}
let input = """
E20D79005573F71DA0054E48527EF97D3004653BB1FC006867A8B1371AC49C801039171941340066E6B99A6A58B8110088BA008CE6F7893D4E6F7893DCDCFDB9D6CBC4026FE8026200DC7D84B1C00010A89507E3CCEE37B592014D3C01491B6697A83CB4F59E5E7FFA5CC66D4BC6F05D3004E6BB742B004E7E6B3375A46CF91D8C027911797589E17920F4009BE72DA8D2E4523DCEE86A8018C4AD3C7F2D2D02C5B9FF53366E3004658DB0012A963891D168801D08480485B005C0010A883116308002171AA24C679E0394EB898023331E60AB401294D98CA6CD8C01D9B349E0A99363003E655D40289CBDBB2F55D25E53ECAF14D9ABBB4CC726F038C011B0044401987D0BE0C00021B04E2546499DE824C015B004A7755B570013F2DD8627C65C02186F2996E9CCD04E5718C5CBCC016B004A4F61B27B0D9B8633F9344D57B0C1D3805537ADFA21F231C6EC9F3D3089FF7CD25E5941200C96801F191C77091238EE13A704A7CCC802B3B00567F192296259ABD9C400282915B9F6E98879823046C0010C626C966A19351EE27DE86C8E6968F2BE3D2008EE540FC01196989CD9410055725480D60025737BA1547D700727B9A89B444971830070401F8D70BA3B8803F16A3FC2D00043621C3B8A733C8BD880212BCDEE9D34929164D5CB08032594E5E1D25C0055E5B771E966783240220CD19E802E200F4588450BC401A8FB14E0A1805B36F3243B2833247536B70BDC00A60348880C7730039400B402A91009F650028C00E2020918077610021C00C1002D80512601188803B4000C148025010036727EE5AD6B445CC011E00B825E14F4BBF5F97853D2EFD6256F8FFE9F3B001420C01A88915E259002191EE2F4392004323E44A8B4C0069CEF34D304C001AB94379D149BD904507004A6D466B618402477802E200D47383719C0010F8A507A294CC9C90024A967C9995EE2933BA840
""".compactMap { $0.hexDigitValue }
}

71
2021/day17.swift Normal file
View file

@ -0,0 +1,71 @@
@main
struct Day17: Puzzle {
func run() {
var maxHeight: Int = 0
var count = 0
for speedX in 1..<300 {
for speedY in -600..<300 {
if case .hit(let height) = run(vx: speedX, vy: speedY) {
maxHeight = max(height, maxHeight)
count += 1
}
}
}
print("Part 1:", maxHeight)
print("Part 2:", count)
}
enum Result {
case tooFar
case tooShort
case hit(height: Int)
}
struct Vector: Hashable {
var x: Int
var y: Int
}
func run(vx: Int, vy: Int) -> Result {
var state = State(vx: vx, vy: vy)
while state.x <= xMax && state.y >= yMin {
state.step()
if xMin...xMax ~= state.x && yMin...yMax ~= state.y {
return .hit(height: state.maxHeight)
}
}
if state.x > xMax {
return .tooFar
}
return .tooShort
}
struct State {
var vx: Int
var vy: Int
var x: Int = 0
var y: Int = 0
var maxHeight = 0
mutating func step() {
if y > maxHeight {
maxHeight = y
}
x += vx
y += vy
vx -= vx.signum()
vy -= 1
}
}
// target area: x=138..184, y=-125..-71
let xMin = 138
let xMax = 184
let yMin = -125
let yMax = -71
}

318
2021/day18.swift Normal file
View file

@ -0,0 +1,318 @@
import Foundation
@main
struct Day18: Puzzle {
func run() {
let pairs = Scanner(string: input).all()
let first = pairs[0]
let result = pairs.dropFirst().reduce(first, +)
print(result.toString())
print("Part 1:", result.magnitude())
var largest = 0
for a in pairs {
for b in pairs where a !== b {
largest = max(largest, (a + b).magnitude())
largest = max(largest, (b + a).magnitude())
}
}
print("Part 2:", largest)
}
}
extension Scanner {
func pair() -> Pair? {
guard scanString("[") != nil else { return nil }
guard let leftPart = part() else { return nil }
guard scanString(",") != nil else { return nil }
guard let rightPart = part() else { return nil }
guard scanString("]") != nil else { return nil }
return Pair(leftPart, rightPart)
}
func part() -> Number? {
if let digits = scanInt() {
return Regular(value: digits)
}
return pair()
}
func all() -> [Pair] {
var result: [Pair] = []
while let pair = self.pair() {
result.append(pair)
}
assert(isAtEnd)
return result
}
}
struct ExplodeContext {
var lastRegular: Regular? = nil
var addToNextNumber: Int? = nil
var didExplode = false
}
enum Action {
case goOn
case stop
case replace(Number)
}
protocol Number: AnyObject {
func toString() -> String
func visitExplode(depth: Int, context: inout ExplodeContext) -> Action
func visitSplit(didSplit: inout Bool) -> Action
func magnitude() -> Int
func copy() -> Number
}
extension Number where Self == Pair {
static func parse(_ string: String) -> Self {
Scanner(string: string).pair()!
}
}
class Regular: Number {
var value: Int
init(value: Int) {
self.value = value
}
func toString() -> String {
"\(value)"
}
func visitExplode(depth: Int, context: inout ExplodeContext) -> Action {
if let add = context.addToNextNumber {
value += add
return .stop
}
context.lastRegular = self
return .goOn
}
func visitSplit(didSplit: inout Bool) -> Action {
if value >= 10 {
let first = value / 2
didSplit = true
return .replace(Pair(Regular(value: first), Regular(value: value - first)))
}
return .goOn
}
func magnitude() -> Int {
value
}
func copy() -> Number { Regular(value: value) }
}
class Pair: Number {
var left: Number
var right: Number
init(_ left: Number, _ right: Number) {
self.left = left
self.right = right
}
func toString() -> String {
"[\(left.toString()), \(right.toString())]"
}
func splitFirst() -> Bool {
var didSplit = false
_ = visitSplit(didSplit: &didSplit)
return didSplit
}
func explodeFirst() -> Bool {
var context = ExplodeContext()
_ = visitExplode(depth: 0, context: &context)
return context.didExplode
}
func reduce() -> Pair {
while true {
if explodeFirst() { continue }
if splitFirst() { continue }
break
}
return self
}
func visitExplode(depth: Int, context: inout ExplodeContext) -> Action {
if !context.didExplode && depth == 4 {
context.lastRegular?.value += (left as! Regular).value
context.addToNextNumber = (right as! Regular).value
context.didExplode = true
return .replace(Regular(value: 0))
}
switch left.visitExplode(depth: depth + 1, context: &context) {
case .goOn:
break
case .stop:
return .stop
case .replace(let number):
left = number
break
}
let result = right.visitExplode(depth: depth + 1, context: &context)
switch result {
case .goOn:
return .goOn
case .stop:
return .stop
case .replace(let number):
right = number
return .goOn
}
}
func visitSplit(didSplit: inout Bool) -> Action {
switch left.visitSplit(didSplit: &didSplit) {
case .goOn: break
case .stop: return .stop
case .replace(let new):
left = new
return .stop
}
switch right.visitSplit(didSplit: &didSplit) {
case .goOn: return .goOn
case .stop: return .stop
case .replace(let new):
right = new
return .stop
}
}
func magnitude() -> Int {
3 * left.magnitude() + 2 * right.magnitude()
}
func copy() -> Number {
Pair(left.copy(), right.copy())
}
}
func +(lhs: Number, rhs: Number) -> Pair {
Pair(lhs.copy(), rhs.copy()).reduce()
}
let input = """
[4,[3,[9,[9,0]]]]
[[[7,6],[2,[2,5]]],[5,[[7,3],8]]]
[4,[4,6]]
[[0,[5,6]],[[[1,3],[2,7]],[[0,6],4]]]
[6,[[3,[6,0]],3]]
[[7,[9,[8,5]]],[6,7]]
[[[[2,6],1],2],[3,[8,4]]]
[4,[[[5,4],[2,7]],[[8,0],[2,3]]]]
[[[[4,3],2],[[3,6],[2,5]]],[[[3,7],8],0]]
[[[8,[0,7]],1],[[9,[3,9]],9]]
[[[[3,0],[1,3]],[[0,9],8]],[[[7,2],9],[[1,4],[3,5]]]]
[[[[9,6],[4,4]],[1,3]],[[4,3],[[6,4],[8,4]]]]
[[[1,2],[[7,6],[2,3]]],[[4,6],[4,2]]]
[[[4,8],[[5,8],1]],[2,3]]
[[[5,2],[3,[5,7]]],[[2,9],5]]
[[[6,[3,2]],[2,6]],[[8,[4,2]],[[5,2],7]]]
[[[[2,6],[0,1]],[7,[3,6]]],[[1,6],[[7,9],0]]]
[[[0,3],[8,1]],[[[9,0],3],[0,2]]]
[[8,[[7,1],[4,7]]],[[0,[1,3]],[8,2]]]
[[[[2,3],4],[[0,8],[9,0]]],[1,[[5,3],4]]]
[[[[7,2],2],[[1,3],[8,3]]],[4,[[7,9],[0,6]]]]
[[[[2,2],[3,4]],[[1,5],[4,3]]],[6,[[7,2],1]]]
[1,[[[5,7],0],[9,[8,8]]]]
[[[[9,2],[0,9]],[4,[7,8]]],[[4,8],[[1,8],[4,9]]]]
[[[[4,7],2],2],4]
[1,[[2,[4,2]],1]]
[[[[7,2],[3,8]],[0,[1,3]]],[[[4,4],[2,4]],[8,2]]]
[[[[1,0],[0,5]],2],[[9,[5,0]],[[1,6],5]]]
[4,[[[8,1],[1,4]],[7,[1,3]]]]
[[[6,[0,4]],[[4,6],[2,4]]],[9,[1,5]]]
[[[[3,6],[3,3]],1],[0,[[8,8],2]]]
[[7,[5,[2,6]]],[[[7,9],6],[0,[3,6]]]]
[[[[6,7],4],[[2,9],2]],3]
[[[7,[1,7]],[5,4]],[[[1,1],[0,1]],5]]
[[6,[[1,0],6]],[0,[6,[0,5]]]]
[[[[2,4],[4,6]],9],[4,[[8,0],7]]]
[[[[9,9],[5,7]],[9,[8,6]]],[[3,[2,3]],0]]
[[0,[1,[5,3]]],[3,[8,[3,4]]]]
[[[[4,3],8],[2,9]],[[1,[6,5]],[[5,7],2]]]
[[[0,[7,4]],[9,[9,6]]],[[8,[5,5]],[[6,4],1]]]
[[[[7,3],[7,9]],[8,[6,2]]],[[8,[4,5]],[[6,4],[6,7]]]]
[[7,[[9,0],[9,0]]],[[[0,8],2],[8,[8,3]]]]
[4,[7,[5,6]]]
[7,[[[3,8],8],3]]
[[[4,[6,6]],0],[9,0]]
[[[[7,4],8],8],[[0,1],[[0,0],[2,4]]]]
[7,[1,[[9,4],[3,6]]]]
[[[[2,8],9],[[8,6],[2,2]]],[[[5,1],9],[2,[0,7]]]]
[8,7]
[[[[0,8],4],[[9,9],[9,9]]],[[[4,3],[1,0]],[6,8]]]
[[[[8,3],[8,9]],1],[[4,[1,0]],[[4,0],[2,3]]]]
[[[[4,7],[1,3]],[6,9]],[[1,0],[[1,8],5]]]
[[2,[4,[6,5]]],[3,[[9,9],5]]]
[[[[7,6],4],9],[8,[4,5]]]
[[[0,[6,6]],[7,[8,9]]],[[[0,0],[3,4]],[4,[1,8]]]]
[[[9,[7,0]],[5,8]],[6,[[5,0],[0,6]]]]
[[[[4,0],[1,9]],[7,[3,6]]],[[2,[8,6]],[[2,8],[8,2]]]]
[[[9,6],8],[[[5,5],[4,8]],0]]
[[[[1,7],1],2],[[[6,8],3],[[3,3],5]]]
[3,[5,[[3,8],6]]]
[3,[[[9,6],[5,8]],[9,2]]]
[[6,1],[6,4]]
[[2,6],[[[1,2],2],8]]
[[[[1,7],[3,6]],[2,[0,2]]],[[3,0],9]]
[1,[[0,[4,9]],5]]
[[[[5,5],[5,2]],[0,[6,4]]],8]
[0,[7,[[6,9],[6,0]]]]
[[[[2,2],[4,7]],[[7,4],6]],[[0,[1,7]],[[3,2],6]]]
[[9,8],0]
[[[[5,4],[4,8]],2],[3,[8,9]]]
[[[[7,0],8],5],[2,6]]
[[[5,[0,8]],5],[[[5,0],[1,8]],[[0,2],7]]]
[[[[9,4],8],[[6,5],4]],[[5,[8,9]],[4,[0,4]]]]
[[[[3,6],7],[[9,3],7]],[7,[[8,3],9]]]
[[[[0,7],5],[[5,7],2]],[[2,[9,5]],[[7,7],[5,0]]]]
[[[[7,5],2],[8,6]],[[2,[6,2]],[5,[3,1]]]]
[[9,[9,1]],6]
[[[0,7],[[5,9],2]],3]
[[[9,3],[8,8]],[0,[4,5]]]
[[[[6,2],5],[4,[3,1]]],[9,[2,8]]]
[[[1,[9,4]],[[0,0],2]],[[1,[2,1]],[[7,8],[3,2]]]]
[[[[0,6],[8,9]],[[4,7],[5,6]]],[[[1,4],[8,7]],[4,6]]]
[[[[6,4],[1,5]],[0,8]],[[[9,7],[1,2]],[9,4]]]
[[[[4,5],[0,7]],[9,[1,8]]],[[[5,0],6],7]]
[[[0,[6,9]],[5,[5,6]]],7]
[[4,5],[[7,[6,5]],1]]
[[[7,9],[6,7]],[4,1]]
[[[[9,6],1],[[3,1],[9,7]]],[1,[7,1]]]
[[[0,[2,0]],5],[[8,[7,6]],[[7,3],4]]]
[[[6,[1,7]],[9,[2,7]]],3]
[[[6,[8,2]],5],[4,[[1,3],[5,1]]]]
[[[4,[3,3]],[4,[2,4]]],[5,4]]
[[[1,6],[4,[4,0]]],[[8,[2,2]],[[8,1],[4,7]]]]
[[2,0],[[2,1],[[4,8],[2,7]]]]
[9,[[8,4],0]]
[[1,6],[[5,[1,3]],[9,[0,9]]]]
[[[0,[3,5]],3],[[2,[8,0]],[[2,0],[4,3]]]]
[[[1,[1,9]],[9,[7,9]]],[[2,2],[[6,7],[0,7]]]]
[[[4,6],[[6,2],[0,9]]],[[1,0],[1,[6,7]]]]
[9,[[[0,1],4],[[9,3],3]]]
"""

1104
2021/day19.swift Normal file

File diff suppressed because it is too large Load diff

1029
2021/day2.hs Normal file

File diff suppressed because one or more lines are too long

192
2021/day20.swift Normal file
View file

@ -0,0 +1,192 @@
@main
struct Day20: Puzzle {
func run() {
var image = Image(input)
for i in 0..<50 {
image = image.run(map: map)
if i == 1 {
print("Part 1:", image.countLitPixels())
}
}
print("Part 2:", image.countLitPixels())
}
}
struct Image {
var width: Int
var height: Int
var pixels: [Bool]
var border: Bool
init(_ string: String) {
pixels = []
width = 0
height = 1
border = false
for char in string {
if char.isNewline {
width = 0
height += 1
continue
}
pixels.append(char == "#")
width += 1
}
}
init(width: Int, height: Int, border: Bool) {
self.width = width
self.height = height
self.border = border
self.pixels = Array(repeating: false, count: width * height)
}
subscript(x: Int, y: Int) -> Bool {
get {
guard 0..<width ~= x && 0..<height ~= y else {
return border
}
return pixels[x + width * y]
}
set {
precondition(0..<width ~= x && 0..<height ~= y)
pixels[x + width * y] = newValue
}
}
var borderIndex: Int { border ? 7 : 0 }
func lookupIndex(x: Int, y: Int) -> Int {
var index = 0
for i in -1...1 {
for j in -1...1 {
index = index << 1 | (self[x + j, y + i] ? 1 : 0)
}
}
return index
}
func run(map: [Bool]) -> Image {
var outputImage = Image(width: width + 2, height: height + 2, border: map[borderIndex])
for y in -1...height {
for x in -1...width {
outputImage[x + 1, y + 1] = map[lookupIndex(x: x, y: y)]
}
}
return outputImage
}
func countLitPixels() -> Int {
pixels.lazy.filter { $0 }.count
}
}
let map = "#####.#.###.###.#.#.####.#####.####.#..####.##.####...##......#.##.##.#.#..#.##.###.#.#.#.##.#..#.####.#.#.##.#..........###.##...#.#...#.#.###..#...##.####.##...###.....##..##..##.#......#.#.##.#.#.##.#.......###.##.#.###.##..##.......##....#.##....#..###.######.##...##.##...#.#.##...##.##.#....##.####..#..#..##.##.#.#..#....#######.###.##...#.####..#.#.#.##...##..##.#.#.#.##.#.......###..######..###..##......###..###.#.#.#.......#.....##.#.##..#.##.#.##.####..#.##....##.#.#..#.####.##.#.#.##...#..##.####."
.map { $0 == "#" }
let input = """
.###..###..#.####.#....#..##.###.........#.#.#..#.#...##...#.#........##..#..#.##....#.##.#...###.##
.##......#.#..###.######.#.#.######...#..##...#......###..###..####..####..##..#.##.#.#.#....##..##.
.##.##..###..#...###....#...##..#..######...#.#.##.#..####.#..#..###.######.#.#..#..##...##.#.#.#.#.
..#..##..####...###.#...###.#.####...#####...#####.#.#########.##..####...#....##....##..#.#..#...##
.#.#..#.###....###.#.#..###.#..#####.#.###...####.##.###.#..####.###...#...#.##.#..#..#...##....####
######....#..##.#..#.##.#.#.##.#.#####.#..###.#####..#######..##..#..#........##.##..#...#..##..#.##
.#.##########.##...#.###..##...#....#.##..##.###..#####...#..##.#.....##..####.#.#.##..#...#..#.#.#.
.##..#.#..#..##.###..#.#.##..#.#...########.##..#....##...#..#.####.#.#..####..#.#..##.#.......##..#
##..##...###.##..#.#.#.###.#..####.##.....#.###.##.##......#.......#.#####.#..##.#.##.#..##.###.##.#
..#..###.....##..####.......####.###.#..##.##.#######.####..#.######.###.#.#..###..###.##.###.##..#.
.##.#..#..#.####.#.#.#..#....#...##.....##..##..##.##.#..##...#..#...##.###.##.##........#.#..#..###
....#.######.#..#......####.##....##.#..#..##...#...#...#.##...##..###.......##.....#..#..#..#####..
###..##.....##.##.###.##..#.#.###..####.#.#....#.##.#.....#######.##.....#..#####.##..#..#####.##.##
#.#.#..#.#.#..........##.##......#.###...#.###.##.######..#...#...#.......###...#..#.##...#.#.##..##
##...##.###.#.##..##....#.#.#..##.###.##.#...#.#..##..#..##...#..##...##..#.####..#..##.#.#...##....
...#..###.####.#.####.#.#.#....#.##.##..#####..#####.#.####.##..##.###....###..#....#..##.#.#..#..#.
..#.#.###...###...#####.####..#.#...###.###..#.##....#.#.#.....#..#...#..#.##..####......##.#.###...
#.##.....##...#.#...#####.#.##.##..##.#.##....#.##.....#.###..#..##....###.....#####..####..#..##...
#...##....##..#..####...#...###...##.##..##..#....###.#....#########.##..##.#.#.######.###..####.##.
##.#.##.#.#..#......##.#..#...##..#.#..##.###..##.......##..#.#...#.#..#.#.#....##..######.##...#...
.#..##..#....##..#.##..##...#..##...#....#.#.##..#...##.##.#..#.#.#.##.#..#.#.......#.###..######..#
#..##.#.##.#..##...####.##.#...####...##.###..#..#.#.#..###..##.#....#####.###..#...#..#..####...##.
#.####....######.....#.##..####....###.##....###.####...##.#....####.#..##..#..##..#.##.####.#.....#
...#.#.###...####..#..#.#..#.##...###.#.#.##.#...#.#.#.##.##...#.#####.#....#..##.#...#.##....##....
#..##..#.##..#.#.#..#..###.##.....####..##..##..#######...##.#.....##..#...########..#.#.#.....##...
.#...##...##..##.#..##......#.##......#.###.##.##...##....####.##.#.....#.#.#.#.##.#.##.#..#..##..#.
.#..#..###.#.#.##..#..#...#..##..#.##...##..#...######..##.#.######........###.#..##..##..######.##.
#####..##.####.....###.###.########.###...#########..#.#.###.#..#####..#..###.#####.#.....#..##..##.
........#....###..####.#.###...#.###.........#.......#..#.#..###.##.#...#.###.##..#...##.##.##.#.###
.##....##.#..##.##...####..########.#####..#.#.#...#.##..#.##..######.###.#..##.#..#...###..#.##.###
#..#.##.##..#.#.#...##....#.#..#..###...###..#..#..##.#.....#..#..#.#.####...#.####.#.#.#.#.#.....#.
###..##.##..##.###.#.#....#..........##.#....#.#.##......#.##.#...#####.##....#.#.#....#.###.#.####.
###.....##..#....#..##.##....#..##.##.#...#..#.#####.####.#..##...#...##.###..##.####..##...##.####.
#.#..#.#.#.#..#..#.###..###.##.###.#....#..###.#.###.....##..#.#.##.#....#.#.#.#.#...#.##.######.##.
##....###...#...#.######.#.#.#....#..#.###.#.##..##...#.....##.#...##.#.#####.##.#.##########.####..
...#.#.#..###.#....##.#....##.#..##...#.##....#...#####.#.....##..#.##.##.....#.##.#.#..##.##.#..#.#
###.##.########.##..##.#...###.##.##.#.#...##..###...##.##.##.#..#...#.#.##.....##.....#..##.#.#.###
#.###.#......####..###.....#.##.###..###....###.....#..#.#.##.##....#.#....#.######.#...##...######.
#.#####.###..#.#..........#.######.#####.####.##.##....###..#.###.#.......#....##.#.######..#.###.#.
#.#.##..#...##...#..#.#...#.##..#.####.#.......####.##..#.##..##.#....#.#.....#..#..#...#..#.##.#.#.
####.#.##..##....####.#.##.#.##..#.#.#.#####.####.#.##..##.#.####.#..##..#.###.#...#.##...#.#..###..
#.###...#.#...##.####...###..#..##.#.##.#.#.######...##.##.#.####..#.###..#.##...##..##.#.#.#.####.#
....#.##..##..####......###.#.....#....######.#...##..#..##.###...##.#####..#.##...#...##.#.#####.##
..##...#..##.......#.#.###.#...#####........###...#....#...##.##...###....##.##.#..####....##..##.#.
.#..###.##......##..#...##......##.#.##..##..###.#.#.#.##...#...#.#.....#.#.#.......#..###..####.###
...#..#..##...#..##.##.#####.#.##.#.#...##.####.###...###.#.##.##.##..#.##..#..##.#.###....###..####
#......#...#.#.#.#.#..#...#....###.##.##...######....#.###.##..##.##..#.#..#.####.#.####.......##.#.
#.###.#..####....##.##.###.##.#.#######..##.##.#.#..#...#.#..####.##..#.##.#....#....#.##.#.#.##...#
##...###..##.#.##...##..####.#.#.#####.#.#.##..........#....#...#..##.###....###.#.#.###.###....#.#.
###.#.##..#....#.#.#.##.######..####.#...##.##...###..###..##.........##.##.#.###...##.#####...###.#
..###..#.##..######.#....#...##.#.....#..######.....#.####.#..###..#..##.##...##..##....##..###..##.
#.#.#############.#..##.#.#..#####.#.#.....#....####.#....#..##..###.#...#.##...###.##...##...#..##.
##..##........##.#.##.###..#.#.##..#...#.##.###.#.###########..#.#.#.##..#....#....#.#..#...##...#..
#.#.#...##...#.#.##.#..##.#.####.##.#....##..#..#.....#...######.#...##...####.#....#.........#.#...
##..#.#.##...#....#.##..##.##.###.#.....#.########.##.....#....######..##......#.#...##..##.#.####..
.##.....###..######..#...#......#..#....##.##...#.##...#.#.###...##...#.#.#..#.#..#.#..#.##..###.###
#..#....#...###.#..#########..##.#.......#.###..###...#....#..##.########.#....#.#..###.##.#......#.
###..#..##....####...###.....#....###...#.#..#...#..#.#####...#.####.......#.##.#.....##.##...##.###
..###..#.###..#.##.......###..##...##....#.#.##.#.....###..###.#..####.#####.#..#.#...###.#.####.#..
#.##.#..##.####.#####.#.#.#...######.#.#.#....#.#.#.#...#.##.......###...#.#######...##..###.#.###.#
..##.#....#####.#..##.#..#.#.#.#.#####.#.###...#..##.#.#.####.#.#....##..###.#.##....##..#####.###..
.....#####.#..#.#.....##.#..###.#.##..#..#..#...#.#....#.##.#.#..#...####...#.#..#.##.##.##.#.#...#.
##.##.######....#....#..##.##.##..###..##.#..####.#.##.#...##.#.##.###.######..#####.....#.##.##.###
.#.##.#...#..#######.####.....###.#.#.#...###....#.#.#...#..#.#.....###...###.#...#.#.....##..#...##
.#####..##.####.##.###.##.##..##..####...#.#.###....#.#.....#...#####...###.##.####....#.###.##.####
.#...#....###.########.##..##.##.##...###..#...#..###..#..#.#.###.#...#.#..##..###.#.....#...####.#.
#.#.#..#.##..###.#.###.##.##...##.#.##.#.#.##......#.....###.#.####..##.#.##..#...###...#..##..#...#
.##....######.#.##....#.#.#...#....###...##.##.#.#..##.#....#.##.###.#.####..##...###.#..#.....#.###
...#.####......#.##...#...####......#..#######.#.#.##..##....####.##....##.##..#.##..###...####.#.#.
####..####..##..#...#.#.....#..#..#.......#.##.#.....#.#.##.#..#####...##.#....##........#.........#
.#.#.#......##..#.###.##....#.#..######.##.#.#..#...#...#....###.....###.###....##.#.##..#.#..##.#.#
.##..###....#.#.#.#..#...##.#.##..#.#.##..#.#####.#...#..#...#####.###..##..#.....#.##..##.####.#.#.
..#..##.####....##.##.#...##.##.#..##.###.#..#.###...#...##.#.##.##.#.#..##.##.##..#.....#..####.##.
.#..#..#.#.#.###...###.##.##..##..##....##...#####.#.#...###.##.#.#....##.#..#...#.#..##...#.####...
#####......##....#.##..##.##..#..#.##..#.##.#.######..##..##...#..#.#..#.###..####.#.###..#.#..###.#
.####..#....#.##....#.##.#...#.###..###...##.##.##.#..#..##..#.....#.#########..#...#..####.##....##
####.###..##.#.#.##.#.####.###.###.#...#####.....#.#....#..####.#.#...####.##....#..##.#....#.#.###.
##.##...##...#.#####..#...#.#.##.#.##.#....#..#..####.#.#####.#...#...#..##.##.###..###.#....####..#
.##.####..###..#.####.#.....###....#...#..#...###.....#.####.#....##.####..#.###..#.###...#....##...
#....#...###.#...#.##.#.....#.##.###.....#.##...#...##.##...###.##...##..#####...#....#....#..###.#.
#.#...##.#.#..##..##..###..##...####.#.#..##..##.##..##.#.###.##...###.#.##.##.##..##..###..###..#.#
.##..###...#.####..#####.#.###....###.#..##..####.##..#.##.....###...#...#..##.###.#.#######.....#..
.####....##..##......##.#......#.##..##.##.###..###.......###....####.#.##..#.####..##.#.#.#.##.#..#
##...#####.#.....##..###...###.#...##..#####.#..###...#####..###......#...#.###.##..##...#.###...###
..###.#...###..#.##.##.##.#.##.....##.###..##..##.##..###....###..#.......###.##...##....#..##..##.#
#..#...##.###.#...#.##...#.##.###.##...##.#...##.#.#.#..#....###.#.......#.##.#..###.#.##.##..####.#
##...####.......#..##.#.#.#..#.##....#.##.##.#.####..##....###.####.#.###..#.##..##......#.#.#..####
###...#..##.#.......##..#.#...##...#...##..##.#.#..#.#.#.####.##.##.###..#.##.####..####....####.#..
#..###.##.....###..##.##.###.###.#.#....##.#######....#####.##.#....###.##..#...#..#.##.#.#.#.#..#..
.#.....#..###..##.#..##.##.###.##..#....###...##........#....####..#.##########...#....###.#...##.##
#.....#.#.#####.#.######.#....##..#...#.##.#..##..#####...######.#.####...#.####.###..#.##....##...#
.#.#...#.#..#.##.#..####..#..#.....##.#....#.#######.#.#..#.#.#.#.###...##..####..##.##.##..#.###...
.#..#.##.#..##.#.##....##..###..#........#.##.#.##.#..##.#.###.#.#..#...#..####.##...#.....#.###...#
..#....##..#..#.##.##.#..###......#.#..#.####..##.#.######..#######..##.##.#..##....#.##.....#.###..
.#....#..#....#...#.###...#.#...##..#..##.######..#..###.##..###.#.##...#.#..#.###.#..###.##...#..#.
##...##.####.##..####.#.#....#....#.###.##.###.##.##..#.#.#.....##...#.#..#####.##..#.#.....#.#.####
.....#.#.##..######....#...#.##..#...#..#...#.##..##..##.....#...##..#.#.####......#.#.##..#.#.#..#.
#.##...##.##..###..#....#...####..##....#.#....#..#.#.....######.....##....#..###.......##......#.#.
#.##.....##.#.#...#.#######.###..#..#...##...#.#.####.#########...#..#....#.....##.#.#.###.##..#.#..
.#.......#.##.....#####.#..#...###.##...#.#..#.###.#.#.#.#########.##.###...####...##.##...###.#.#.#
"""

101
2021/day21.swift Normal file
View file

@ -0,0 +1,101 @@
@main
struct Day21: Puzzle {
func run() {
part1()
part2()
}
let player1 = Player(position: 6)
let player2 = Player(position: 7)
func part1() {
var die = Die()
var player1 = player1
var player2 = player2
while true {
if player1.move(die.roll3()) >= 1000 {
print("Part 1: Player 1 won. ", die.rolls * player2.score)
break
}
if player2.move(die.roll3()) >= 1000 {
print("Part 1: Player 2 won. ", die.rolls * player1.score)
break
}
}
}
func part2() {
var universes: [Universe: Int] = [Universe(player1: player1, player2: player2): 1]
var player1Wins = 0
var player2Wins = 0
while !universes.isEmpty {
var newUniverses: [Universe: Int] = [:]
for (universe, universeCount) in universes {
for (offset, player1RollCount) in rollCounts.enumerated() {
var u = universe
if u.player1.move(offset + minRoll) >= quantumMax {
player1Wins += universeCount * player1RollCount
continue
}
for (offset, player2RollCount) in rollCounts.enumerated() {
var u2 = u
if u2.player2.move(offset + minRoll) >= quantumMax {
player2Wins += universeCount * player1RollCount * player2RollCount
continue
}
newUniverses[u2, default: 0] += universeCount * player1RollCount * player2RollCount
}
}
}
universes = newUniverses
}
print("Part 2: ", max(player1Wins, player2Wins))
}
let quantumMax = 21
let minRoll = 3
// 3 4 5 6 7 8 9
let rollCounts = [1, 3, 6, 7, 6, 3, 1]
struct Universe: Hashable {
var player1: Player
var player2: Player
}
struct Player: Hashable {
var position: Int
var score: Int = 0
mutating func move(_ roll: Int) -> Int {
position = 1 + (position + roll - 1) % 10
score += position
return score
}
}
struct Die {
var rolls = 0
var nextRoll = 1
mutating func roll() -> Int {
defer {
rolls += 1
nextRoll += 1
}
return nextRoll
}
mutating func roll3() -> Int {
roll() + roll() + roll()
}
}
}

564
2021/day22.swift Normal file
View file

@ -0,0 +1,564 @@
import Foundation
@main
struct Day22: Puzzle {
func run() {
let scanner = Scanner(string: input)
var cubes: [Cube] = []
while !scanner.isAtEnd {
cubes.append(scanner.cube())
}
var xStepsSet: Set<Int> = []
var yStepsSet: Set<Int> = []
var zStepsSet: Set<Int> = []
for cube in cubes {
xStepsSet.insert(cube.start.x)
xStepsSet.insert(cube.end.x + 1)
yStepsSet.insert(cube.start.y)
yStepsSet.insert(cube.end.y + 1)
zStepsSet.insert(cube.start.z)
zStepsSet.insert(cube.end.z + 1)
}
var xSteps = xStepsSet.sorted()
var ySteps = yStepsSet.sorted()
var zSteps = zStepsSet.sorted()
let firstX = xSteps.removeFirst()
let firstY = ySteps.removeFirst()
let firstZ = zSteps.removeFirst()
var part1 = 0
var part2 = 0
var x0 = firstX
for x in xSteps {
let possibleX = cubes.filter { $0.xRange.overlaps(x0..<x) }
var y0 = firstY
let px0 = max(x0, -50)
let px = min(x, 50)
for y in ySteps {
let possibleY = possibleX.filter { $0.yRange.overlaps(y0..<y) }
var z0 = firstZ
let py0 = max(y0, -50)
let py = min(y, 50)
for z in zSteps {
let state = possibleY.lazy.reversed().first { $0.zRange.overlaps(z0..<z) }?.state ?? false
if state {
let pz0 = max(z0, -50)
let pz = min(z, 50)
if px0 < px && py0 < py && pz0 < pz {
part1 += (px - px0) * (py - py0) * (pz - pz0)
}
part2 += (x - x0) * (y - y0) * (z - z0)
}
z0 = z
}
y0 = y
}
x0 = x
}
print("Part 1:", part1)
print("Part 2:", part2)
}
}
struct Point: Hashable {
var x: Int
var y: Int
var z: Int
}
struct Cube {
var start: Point
var end: Point
var state: Bool
var xRange: ClosedRange<Int> { start.x...end.x }
var yRange: ClosedRange<Int> { start.y...end.y }
var zRange: ClosedRange<Int> { start.z...end.z }
func contains(_ point: Point) -> Bool {
xRange.contains(point.x) && yRange.contains(point.y) && zRange.contains(point.z)
}
}
extension Scanner {
func onOff() -> Bool {
if scanString("on") != nil {
return true
}
if (scanString("off") != nil) {
return false
}
fatalError("Invalid command")
}
func range() -> ClosedRange<Int> {
guard let min = scanInt(),
scanString("..") != nil,
let max = scanInt()
else {
fatalError("Invalid range")
}
return min...max
}
func line() -> (Bool, ClosedRange<Int>, ClosedRange<Int>, ClosedRange<Int>) {
let command = onOff()
guard scanString("x=") != nil else { fatalError() }
let xRange = range()
guard scanString(",y=") != nil else { fatalError() }
let yRange = range()
guard scanString(",z=") != nil else { fatalError() }
let zRange = range()
return (command, xRange, yRange, zRange)
}
func cube() -> Cube {
let (state, xrange, yrange, zrange) = line()
return Cube(
start: Point(x: xrange.lowerBound, y: yrange.lowerBound, z: zrange.lowerBound),
end: Point(x: xrange.upperBound, y: yrange.upperBound, z: zrange.upperBound),
state: state
)
}
}
let input = """
on x=-28..25,y=-34..15,z=-36..13
on x=-9..36,y=-2..43,z=-47..5
on x=-27..26,y=-7..41,z=-8..46
on x=-28..26,y=-11..39,z=-32..17
on x=-13..41,y=-30..24,z=-47..1
on x=-4..45,y=-48..-3,z=-39..13
on x=-49..-3,y=-29..20,z=-39..7
on x=-29..20,y=2..46,z=-31..15
on x=-10..34,y=-12..41,z=-15..32
on x=-12..41,y=-6..42,z=-22..28
off x=-8..9,y=-31..-21,z=-30..-11
on x=1..47,y=-30..16,z=-9..38
off x=-34..-16,y=-48..-31,z=9..25
on x=-29..19,y=-38..15,z=-34..20
off x=-3..12,y=25..34,z=-43..-32
on x=-36..17,y=-23..31,z=-29..16
off x=14..27,y=-30..-21,z=3..21
on x=-31..14,y=-36..8,z=-36..12
off x=24..34,y=-27..-10,z=-34..-17
on x=-33..14,y=-45..6,z=-42..2
on x=-84084..-49906,y=-34022..-13687,z=-26508..-7161
on x=-5505..17661,y=15376..39557,z=64634..85669
on x=-77470..-55853,y=-25906..-5142,z=-42096..-20083
on x=-44899..-13829,y=5050..26536,z=67119..86305
on x=-63460..-40980,y=33003..59169,z=25378..44440
on x=-17311..-1574,y=-86884..-62730,z=-54963..-35830
on x=26480..34369,y=39725..55309,z=-77095..-43655
on x=22634..49093,y=-13808..7138,z=-79760..-60071
on x=2496..13198,y=-73951..-53407,z=-43731..-30331
on x=-27451..-20414,y=1868..32906,z=-75318..-62730
on x=31848..53483,y=-73320..-63684,z=-24860..-14722
on x=6461..28672,y=1908..29124,z=-84376..-68877
on x=16931..39395,y=37598..63107,z=47860..60054
on x=14945..40546,y=-7317..549,z=-93498..-67762
on x=-85540..-58126,y=7461..23740,z=-29051..-17311
on x=38640..49114,y=55796..68805,z=-35888..-8915
on x=-25483..8215,y=32431..39809,z=70198..82364
on x=-3703..17675,y=61466..90313,z=16909..48922
on x=44604..75529,y=-22304..-6299,z=45999..60729
on x=-70454..-38359,y=25776..59602,z=-45661..-30347
on x=-20074..9993,y=56063..71942,z=-49242..-17209
on x=35569..59289,y=-52746..-21591,z=33105..49948
on x=-69553..-46700,y=-21120..10014,z=-78684..-51923
on x=-64276..-54684,y=41723..63992,z=-20013..-7784
on x=-1824..9573,y=66316..76607,z=23895..46349
on x=-54006..-31537,y=9127..34043,z=57224..87032
on x=61803..80774,y=-50282..-13511,z=-35593..-9531
on x=-65653..-36057,y=-71428..-40824,z=-50428..-20504
on x=-35635..-12972,y=-6991..2475,z=-88189..-59265
on x=67324..77277,y=12215..17359,z=9446..16623
on x=46320..63691,y=23806..30377,z=-62984..-38536
on x=-6894..13135,y=-44916..-38815,z=49846..79849
on x=-67348..-54769,y=-57422..-29061,z=25855..36474
on x=-28019..-23220,y=-60400..-37046,z=49526..71048
on x=-42960..-30831,y=-78337..-64643,z=-22038..12986
on x=-43601..-27531,y=-35599..-20965,z=52822..83732
on x=-85892..-68621,y=-10994..9023,z=-20838..-9147
on x=59413..97068,y=-22230..-1408,z=-3604..19485
on x=62228..85826,y=-31716..-2979,z=12114..36300
on x=46514..66369,y=-35480..1374,z=36011..51592
on x=-48685..-36357,y=55919..77922,z=-20577..-12176
on x=63584..82792,y=11027..31562,z=-43607..-35346
on x=-13917..12041,y=-73682..-62007,z=35642..62665
on x=45316..56476,y=13007..26619,z=45798..63220
on x=-58080..-47733,y=8079..17846,z=53185..62392
on x=2479..30325,y=-52517..-31422,z=-70357..-52270
on x=-39911..-29141,y=52574..71764,z=-33493..-5376
on x=75460..84989,y=-7735..-6452,z=-5685..823
on x=45533..71393,y=-47931..-13874,z=30994..40795
on x=-14338..7548,y=-96290..-65900,z=-14911..-8244
on x=39472..46973,y=-45007..-16123,z=-68972..-43340
on x=15323..43991,y=-79867..-60758,z=-46..13763
on x=-34857..-7822,y=70341..75898,z=22751..44742
on x=60051..71299,y=6656..26788,z=-46635..-29393
on x=46242..67391,y=-60111..-42428,z=-63406..-43389
on x=-35622..-13029,y=61821..88175,z=-19300..-3468
on x=45267..78654,y=940..23235,z=34829..52055
on x=-64544..-32718,y=-52424..-32259,z=-51391..-42170
on x=-45310..-38884,y=60663..76511,z=-20350..6437
on x=-19546..3011,y=-85555..-56269,z=-48635..-31142
on x=76489..97638,y=-35407..-9542,z=-22900..679
on x=-34856..-12798,y=57873..72105,z=28432..41250
on x=-12695..5075,y=-51326..-34170,z=60410..74327
on x=48950..55957,y=21833..41299,z=33762..48570
on x=-72062..-57619,y=-30380..-10079,z=-68295..-30427
on x=9722..29351,y=-48714..-33393,z=-78170..-44048
on x=8231..26025,y=-77925..-54037,z=-47724..-25194
on x=-95495..-73464,y=-7621..14339,z=-29593..-13088
on x=55959..79890,y=-5967..31737,z=-67844..-36315
on x=53044..74610,y=-30723..-8664,z=-43868..-14796
on x=-4307..16454,y=35270..42969,z=-81647..-68472
on x=45874..60812,y=-69404..-54747,z=-8298..-3138
on x=-41808..-29758,y=-75803..-46116,z=23276..33715
on x=-65837..-40686,y=-69179..-53790,z=-47995..-22105
on x=-9912..23894,y=-1700..23494,z=-97303..-62588
on x=19429..37260,y=-67376..-50156,z=-53512..-16652
on x=-57067..-52828,y=-12667..-9591,z=-67443..-58160
on x=-3523..27218,y=74994..90403,z=-3493..14395
on x=14169..48854,y=-1568..28900,z=-71852..-52553
on x=32821..57220,y=47717..78952,z=-34811..-13134
on x=-79129..-71287,y=-18716..3046,z=21374..41841
on x=-11439..14964,y=-16250..3021,z=65364..90013
on x=-38129..-32162,y=58740..80207,z=-9827..9520
on x=40614..58324,y=-11736..-1046,z=47782..68564
on x=68136..85433,y=19038..53903,z=-31789..4892
on x=6721..20772,y=74233..93387,z=-17766..5311
on x=-38118..-12460,y=-57425..-34797,z=55638..66558
on x=2737..32272,y=-76998..-55030,z=43688..49155
on x=12198..38223,y=-73791..-64926,z=31631..39745
on x=-20261..-2856,y=-74905..-54211,z=7861..34791
on x=19178..35422,y=-11638..11563,z=58261..77953
on x=-31001..1243,y=11174..27421,z=-83906..-70511
on x=26738..51818,y=41573..66947,z=5480..38191
on x=-4120..27825,y=-85989..-63889,z=-33571..-17260
on x=65955..73858,y=-30937..-20357,z=19699..51919
on x=59986..81449,y=-17094..-6765,z=-21325..4146
on x=-15287..13119,y=-67414..-61189,z=-46979..-43603
on x=-82390..-66004,y=-10271..3338,z=-38978..-23333
on x=48845..69167,y=43156..62169,z=6515..37326
on x=-29512..-18622,y=66618..85799,z=16557..37597
on x=-13680..11486,y=-73990..-52854,z=34292..59824
on x=16528..41696,y=-88669..-71438,z=6756..28376
on x=47386..68246,y=26567..41173,z=32881..45573
on x=41552..60418,y=-13177..5608,z=40188..72493
on x=-17271..1089,y=66757..90442,z=-8173..4840
on x=-83957..-59723,y=-5356..22092,z=-46860..-26958
on x=-66297..-47544,y=-37434..-22806,z=-60731..-47364
on x=-14961..-8051,y=-82471..-63555,z=-17655..1207
on x=4207..34538,y=61925..92475,z=-30435..-15910
on x=-17617..-10570,y=-95348..-63368,z=-16210..14719
on x=38496..55233,y=-64106..-40918,z=16863..31642
on x=50719..79640,y=-61152..-34221,z=-10774..9297
on x=-42969..-7619,y=-26388..-18715,z=70740..87672
on x=-47929..-35133,y=49347..82369,z=-14714..10853
on x=-82604..-63475,y=-34831..-12713,z=-11929..4941
on x=29358..63182,y=8015..29986,z=54315..82349
on x=-20131..-1839,y=60333..80914,z=7967..29376
on x=9353..36272,y=59895..89044,z=-25822..3230
on x=43168..59888,y=-66721..-49667,z=32648..46482
on x=10941..29136,y=-72555..-53370,z=-54573..-35565
on x=11496..31590,y=-44959..-13029,z=53082..87310
on x=-43159..-20524,y=-3743..13835,z=-87866..-52777
on x=26627..54751,y=-43550..-24270,z=49732..64107
on x=28255..59585,y=43842..68816,z=-44115..-40179
on x=-66412..-55612,y=12166..35074,z=39006..51003
on x=6312..30405,y=57804..74608,z=39542..48278
on x=-14943..3493,y=10401..33502,z=-94020..-67669
on x=35476..48357,y=-61493..-38961,z=-71382..-32979
on x=20435..28523,y=7481..28051,z=-78353..-58295
on x=-83068..-71048,y=-23721..6279,z=-22062..-4203
on x=-51208..-17644,y=46982..85355,z=-40990..-15261
on x=547..20809,y=60044..87576,z=22999..55796
on x=-17902..-14415,y=-31625..-509,z=-87372..-60462
on x=566..30656,y=65157..93943,z=-10094..25356
on x=-57372..-44522,y=-43215..-4269,z=47571..76802
on x=51928..61917,y=27192..61997,z=16462..35892
on x=-91859..-77183,y=-25309..-6900,z=-6636..4658
on x=35451..49841,y=42840..59899,z=27902..37733
on x=46697..63066,y=-61951..-35956,z=-43402..-31431
on x=56820..85532,y=22368..37611,z=8181..29028
on x=3479..28543,y=-92950..-65707,z=-20147..3586
on x=-30715..-8102,y=-74389..-38791,z=41838..65462
on x=53923..84954,y=21345..48479,z=-31117..-22707
on x=30692..68472,y=51381..74010,z=-43163..-26289
on x=3963..13629,y=-82301..-68766,z=-32189..-28695
on x=-26230..-18986,y=48059..75879,z=33800..57890
on x=-11868..364,y=-17805..-6376,z=-87840..-69361
on x=-62123..-34039,y=53323..65684,z=-18759..11519
on x=-49686..-22329,y=44482..62763,z=-50165..-42433
on x=8295..16668,y=-21473..2954,z=72427..92773
on x=61061..82812,y=-34561..-15760,z=12195..29626
on x=18871..46026,y=45645..74462,z=34738..47404
on x=45250..66555,y=-51615..-23083,z=11910..48756
on x=-74771..-44970,y=-21127..-11037,z=-64988..-33929
on x=-28575..-24424,y=71792..78110,z=-18221..-8555
on x=-25338..-6923,y=-8631..16382,z=-96932..-68836
on x=-15816..8291,y=23502..46422,z=61737..75046
on x=24012..60280,y=-83721..-54294,z=7578..28977
on x=11501..29155,y=3402..11782,z=75736..82902
on x=-57546..-26246,y=36304..67304,z=-45286..-27281
on x=-6325..5065,y=22635..45274,z=-81700..-68260
on x=-66720..-54631,y=-61236..-46412,z=-5425..22854
on x=-72710..-48798,y=50508..64307,z=1333..13462
on x=13865..37998,y=51466..65722,z=36006..59077
on x=-28281..-7465,y=-68705..-40266,z=50853..66666
on x=-38630..-17127,y=-7930..4812,z=-74660..-58504
on x=20647..33497,y=-62711..-45250,z=-65188..-41498
on x=-81459..-63994,y=11465..40585,z=18212..44406
on x=-52840..-33789,y=-40941..-18153,z=40378..62402
on x=-53664..-41652,y=19752..32228,z=50381..70997
on x=-1080..23636,y=-33517..-25727,z=-86099..-57148
on x=-23319..-3146,y=24049..42456,z=-71419..-57820
on x=9982..44125,y=-22469..-5211,z=-89475..-57073
on x=-59300..-41829,y=25692..39878,z=-65256..-42734
on x=-47735..-38054,y=57249..66648,z=-43826..-25726
on x=-39553..-32542,y=5165..28643,z=55369..70627
on x=-12872..3087,y=-36519..-10753,z=60621..76591
on x=59192..83731,y=18036..35836,z=20877..51212
on x=26020..48490,y=12554..14377,z=-87022..-63383
on x=-11854..-4515,y=-12639..-5946,z=-85165..-60021
on x=-52581..-30843,y=59055..82392,z=-11397..22455
on x=-36423..-16444,y=29020..41390,z=-71107..-58690
on x=55463..71190,y=-39991..-27121,z=29306..33897
on x=-41896..-15502,y=-51414..-28802,z=-67096..-46901
on x=21576..51776,y=-40007..-10272,z=-84715..-46191
on x=8292..28045,y=6514..32122,z=66114..83717
on x=-66406..-58191,y=-43227..-14898,z=-59859..-26427
on x=65446..83471,y=-7820..13657,z=-50600..-32289
on x=35134..36286,y=-59572..-40161,z=-60756..-35239
on x=9254..29476,y=67387..82064,z=-6059..20274
on x=-53418..-42461,y=-25344..-8754,z=-68658..-59076
on x=-20702..-12302,y=61483..82828,z=-4045..12127
on x=-15861..5533,y=65152..81160,z=13062..33238
on x=-67682..-56916,y=-59758..-23693,z=-36108..-26134
on x=-19546..10161,y=-85094..-56733,z=-41987..-28045
on x=48091..80455,y=-55246..-43231,z=-2817..22958
on x=-77749..-58276,y=50507..63707,z=-10508..8335
on x=-64911..-37001,y=-62267..-44370,z=-47966..-28755
on x=62326..86674,y=-51021..-17862,z=-32602..-1819
on x=-70525..-48780,y=-47797..-19221,z=25331..55925
on x=-50427..-25959,y=-48504..-28133,z=49547..73361
on x=-73330..-63515,y=-35500..-23379,z=-5114..19001
off x=51657..61655,y=-4634..27427,z=-69455..-41980
off x=-84315..-77210,y=19373..36993,z=-21053..15930
on x=-22479..10246,y=-36815..-4914,z=-83609..-58867
off x=-55431..-27453,y=-5767..21070,z=60471..70160
off x=-5688..3205,y=873..13973,z=77366..82873
off x=-53425..-21862,y=41423..59744,z=-55084..-43328
on x=-22427..5852,y=-74250..-39007,z=37690..67662
on x=-63041..-49752,y=-75175..-41598,z=-28260..4287
off x=15413..50919,y=-63278..-30199,z=54233..71948
off x=37405..60088,y=-30130..-3727,z=50275..74270
on x=44953..76055,y=45013..54715,z=3402..24097
on x=4477..16728,y=585..26623,z=-96312..-73210
off x=-60707..-37806,y=33556..55805,z=-55830..-34506
on x=-43041..-34231,y=-75795..-62433,z=-39802..-6990
on x=7577..34465,y=63182..75089,z=-51613..-28445
on x=-40656..-32795,y=39054..59670,z=39143..61093
on x=61949..77774,y=-8028..6257,z=-25989..-7928
off x=62480..84973,y=-25581..6076,z=-51280..-37961
on x=-72929..-40057,y=-35351..-8508,z=-65932..-29811
off x=-65518..-39994,y=-279..16674,z=-73725..-40504
off x=-37297..-22465,y=-75675..-45041,z=39399..60052
off x=-52661..-19343,y=49842..69777,z=-49307..-28892
on x=16218..47208,y=-36176..-16797,z=62683..80721
off x=-46068..-21667,y=60526..77312,z=4157..8754
off x=34070..60691,y=-41133..-30172,z=-53958..-38014
off x=64392..81661,y=-45176..-23170,z=-14523..15457
off x=-15744..-7180,y=58905..69594,z=43007..61745
off x=-52963..-30458,y=-10496..7448,z=-83916..-66777
off x=69857..81390,y=-25485..-7871,z=5030..29910
on x=-60959..-46201,y=50105..71526,z=-45080..-16114
on x=18978..27486,y=55295..76687,z=-58694..-25206
off x=-6820..20202,y=61572..82949,z=17286..19675
on x=1728..22714,y=-70670..-51775,z=39263..62822
off x=-45895..-27411,y=-82575..-60562,z=-35050..-16558
off x=34247..55770,y=-32892..-572,z=49237..79617
off x=6279..24334,y=-76907..-57315,z=-58904..-36830
off x=-29510..-16989,y=69568..77772,z=14319..29972
on x=60510..76515,y=-9611..13786,z=-33980..-20539
on x=5371..24846,y=-53948..-32082,z=-72848..-59739
on x=-16322..-10658,y=-42921..-29325,z=-82769..-55636
on x=-33676..-21096,y=37528..59995,z=-58072..-30498
off x=-47215..-31769,y=55087..91254,z=-14424..-5524
off x=51272..71648,y=-9020..25748,z=48474..57714
on x=-14910..-9150,y=-31157..-7755,z=-86976..-75518
off x=-55245..-27776,y=-49766..-31808,z=42101..67152
on x=-69367..-63985,y=-2671..13295,z=-53128..-27235
on x=58929..86759,y=24302..53114,z=-4060..16704
off x=-47623..-41234,y=52703..55259,z=-54922..-38332
off x=234..33436,y=-38688..-18973,z=-78624..-64983
on x=-77931..-67269,y=-25142..-18496,z=2965..13641
off x=-35975..-19833,y=3648..26277,z=-89061..-67038
off x=47790..55618,y=38688..74556,z=10070..21478
on x=40666..45048,y=-82962..-64543,z=-4571..9994
on x=11044..15406,y=66273..95175,z=-5970..10319
off x=27658..45091,y=-13546..1126,z=47771..81258
off x=-51366..-27007,y=-44699..-24577,z=46267..69042
on x=-7039..14125,y=-67429..-36525,z=55266..77835
off x=-14528..14651,y=60280..82464,z=-5661..19789
off x=6049..22392,y=57922..78173,z=-68277..-33899
on x=5350..16965,y=-67738..-44461,z=50282..75195
on x=-29214..-16694,y=-8319..-575,z=66463..75789
off x=-15036..-8425,y=70237..95064,z=-4849..24023
off x=-53599..-37851,y=41971..53928,z=41571..46610
on x=-13677..2807,y=78384..91484,z=-25356..-8582
off x=24528..48582,y=62896..85790,z=12460..18522
on x=-48176..-33364,y=14289..15341,z=54092..87843
on x=-46960..-30174,y=-12120..6661,z=61935..70809
on x=3481..27256,y=12288..22963,z=-97219..-62916
off x=-28747..5507,y=17194..46753,z=-91768..-56220
off x=42783..58738,y=51060..58581,z=820..24693
on x=-49353..-37799,y=6360..36216,z=48187..77532
on x=52517..57152,y=-57632..-40065,z=-52048..-22663
off x=45577..82271,y=-53151..-34086,z=7606..27599
on x=-49439..-39837,y=30695..51461,z=-60976..-40779
off x=-66635..-46630,y=49057..63361,z=3526..12518
on x=54010..66216,y=36321..55042,z=-6269..24374
off x=4695..7233,y=455..14476,z=62747..89572
off x=-85493..-66739,y=2471..22091,z=-36223..-29382
off x=7884..18171,y=-68024..-45024,z=55300..75806
off x=43549..76092,y=49654..69738,z=1376..15517
off x=12346..17191,y=-4032..16643,z=72903..90015
on x=60703..73244,y=-20825..-3421,z=-57128..-36177
off x=-9866..20026,y=16321..41773,z=60404..85148
on x=29431..48382,y=48462..74606,z=41279..44993
on x=62612..79405,y=-5487..10920,z=-40427..-31710
off x=11859..30527,y=-34097..-19197,z=-73407..-51576
off x=63862..89056,y=21916..38067,z=-5939..30841
on x=40214..68256,y=25471..56425,z=-62984..-30420
on x=-25366..-18100,y=66147..95119,z=3805..18767
off x=68425..79895,y=-47516..-25384,z=15293..41106
on x=30487..56428,y=-1794..5876,z=51906..74514
off x=46653..51836,y=-76040..-44634,z=18181..37159
off x=-41608..-12051,y=72020..86112,z=-8983..14501
on x=-88247..-62536,y=-8129..11276,z=-24055..-4427
off x=3323..10767,y=59354..66158,z=-59632..-39108
on x=-76419..-67309,y=3713..37240,z=-37530..-11311
off x=-2144..12919,y=-54075..-34178,z=57045..63905
off x=62020..79582,y=-31594..-23121,z=-26597..-24909
off x=-17401..-9352,y=73039..84740,z=-44057..-13270
off x=-24093..-6768,y=57829..80320,z=-61101..-38285
on x=-11495..16269,y=-13453..-6215,z=63349..88399
on x=36287..58675,y=59742..70305,z=-17524..18377
off x=56801..73533,y=19040..42381,z=-1979..-474
on x=-58575..-33767,y=-25457..8768,z=-73498..-64326
off x=-12572..14666,y=-1123..34793,z=-83475..-75872
on x=-3033..9242,y=17802..28168,z=74794..85676
on x=2218..27568,y=67949..82818,z=-25004..-15400
off x=-6346..-4816,y=56884..81616,z=-27429..-3943
on x=854..25707,y=22958..43224,z=-82067..-52397
on x=32311..41886,y=-68875..-42153,z=-52635..-38254
off x=26308..38287,y=-83310..-60192,z=28347..48640
on x=-32295..-7848,y=-87573..-75370,z=-12081..5422
on x=-42105..-23185,y=-29547..-19413,z=-72177..-51007
on x=-14692..-9809,y=73589..83475,z=-36237..-2728
off x=-73543..-55115,y=23711..51688,z=-3991..1464
off x=-14534..22568,y=-54527..-44508,z=57714..69383
on x=-63081..-39714,y=-45491..-26566,z=-49886..-38562
off x=-93619..-68748,y=-16696..-4661,z=-30558..-4126
on x=-66806..-44552,y=37360..55716,z=35175..52862
on x=-6316..26070,y=-54927..-28748,z=-69954..-47453
off x=-78871..-50085,y=17954..32161,z=-36569..-23985
on x=6369..25338,y=67499..88203,z=-26047..-4496
on x=24755..54569,y=-67118..-37387,z=32897..50992
off x=46528..63982,y=-54598..-26438,z=37906..65831
on x=4634..24098,y=-45650..-35611,z=-67709..-58572
off x=44078..48115,y=-75447..-53188,z=-5442..33129
off x=-62868..-56634,y=46581..58996,z=-29362..-5667
on x=-6024..8438,y=65634..74871,z=-38003..-30673
off x=-59956..-46026,y=41808..66700,z=10165..36114
off x=-32873..-20372,y=-88729..-60899,z=-19758..7186
on x=-67393..-60159,y=-56680..-32136,z=-8305..17511
off x=-20681..3599,y=-92468..-57916,z=12916..28612
on x=-7301..9140,y=37170..58369,z=-72713..-47101
on x=66247..85599,y=3262..12242,z=-7260..4927
off x=-40965..-24082,y=-82146..-62002,z=20400..46309
off x=-70415..-51837,y=-56997..-42331,z=-875..19799
on x=57902..75722,y=35482..60227,z=-26389..-9051
off x=-22870..7672,y=-4882..25620,z=-87984..-60436
on x=3155..35881,y=-80545..-67121,z=-6933..22549
off x=-65134..-44685,y=-66218..-45701,z=21040..41162
on x=36126..69247,y=23453..60238,z=38126..59678
on x=55937..67003,y=6113..21587,z=45836..60761
off x=18451..41277,y=-70926..-44655,z=-60498..-33470
on x=-59038..-40083,y=-53819..-22588,z=-55419..-43900
on x=-22584..6019,y=59421..80916,z=29210..42933
on x=-33931..-15311,y=-76316..-51022,z=21445..40518
on x=42213..72279,y=-36048..-15004,z=26488..50017
on x=-28842..-8697,y=-68609..-44348,z=40673..64865
off x=-7167..8781,y=-3621..17677,z=-93905..-64435
off x=-82972..-60284,y=6678..34439,z=-3504..-1846
off x=-67834..-53928,y=-58517..-42067,z=-42608..-8421
off x=-4855..22956,y=9296..23886,z=-94874..-60207
off x=-79569..-70626,y=2629..27696,z=-22941..3340
on x=-10965..20710,y=51680..63775,z=45787..69225
off x=46280..63357,y=42031..64449,z=3459..25386
on x=-254..28129,y=-81371..-67737,z=21692..52613
on x=-2383..5437,y=66740..96837,z=10033..17517
off x=17147..30452,y=-91140..-71945,z=1097..25760
on x=-47454..-28074,y=-77661..-55440,z=-24637..-384
off x=-57862..-35813,y=19453..29451,z=39191..70727
off x=-57539..-22945,y=22003..33680,z=-67047..-49723
off x=-3162..20231,y=-54634..-42988,z=53582..62417
on x=-81550..-64507,y=-27996..-14746,z=-10531..-7267
off x=20586..40845,y=-81557..-67548,z=6923..34475
off x=22518..23900,y=-66351..-34420,z=51370..64347
on x=-49052..-35860,y=-19130..11213,z=61999..78547
off x=-34188..-18864,y=-81419..-70168,z=-37648..-12239
off x=77362..83490,y=2745..18791,z=8832..18419
on x=-32402..-14262,y=68320..76432,z=-35620..-15790
off x=67237..90757,y=4695..12771,z=-19624..10840
off x=27845..36686,y=-50404..-33761,z=-80878..-55141
off x=36370..69461,y=33759..65210,z=-54985..-28791
on x=-59135..-46273,y=7474..26326,z=-65975..-58673
on x=26877..52927,y=-79947..-54801,z=-2779..22702
off x=-71675..-51921,y=-59152..-34586,z=-8374..19519
on x=6403..17478,y=-75737..-58369,z=-71127..-34808
on x=13332..40836,y=-40399..-31096,z=-85343..-51700
off x=-67594..-49756,y=11777..23306,z=-64433..-55048
off x=21971..44163,y=52916..80818,z=-13270..-4413
on x=66677..74076,y=22958..42365,z=5671..22730
on x=-17496..17047,y=2018..35721,z=62177..87439
off x=-46335..-30771,y=12773..50699,z=45373..69810
on x=-82592..-68036,y=-13665..-9385,z=185..18296
off x=31849..55495,y=-68614..-40470,z=37173..62475
on x=-57887..-33776,y=-71721..-40636,z=-45..25174
off x=35969..44108,y=-5435..14452,z=-88200..-55792
on x=60659..86542,y=7942..20443,z=4839..32918
off x=50661..57689,y=-33927..-25875,z=46713..54231
on x=-30700..-7902,y=71035..78599,z=-30964..-7588
off x=769..24000,y=-68842..-41325,z=-63861..-53498
on x=-83517..-62573,y=-23803..5911,z=20605..50776
off x=-77447..-65015,y=-7232..20379,z=36322..47544
off x=54759..77852,y=-9847..18404,z=-54569..-25017
on x=8814..38891,y=44583..61983,z=27053..54606
off x=-32755..-26721,y=-27446..-17253,z=-80334..-52377
on x=23465..37441,y=-37240..-20297,z=-68128..-49025
on x=-5136..18927,y=17373..30697,z=66878..78408
on x=-29189..-6136,y=-76807..-52880,z=26943..52440
"""

284
2021/day23.swift Normal file
View file

@ -0,0 +1,284 @@
@main
struct Day23: Puzzle {
func run() {
part1()
part2()
}
func part1() {
var board = Board(height: 3)
board.fillHomeRow(for: .a, pieces: [.c, .c])
board.fillHomeRow(for: .b, pieces: [.b, .d])
board.fillHomeRow(for: .c, pieces: [.a, .a])
board.fillHomeRow(for: .d, pieces: [.d, .b])
board.show()
print("Part 1:", find(start: board))
}
func part2() {
var board = Board(height: 5)
board.fillHomeRow(for: .a, pieces: [.c, .d, .d, .c])
board.fillHomeRow(for: .b, pieces: [.b, .c, .b, .d])
board.fillHomeRow(for: .c, pieces: [.a, .b, .a, .a])
board.fillHomeRow(for: .d, pieces: [.d, .a, .c, .b])
board.show()
print("Part 2:", find(start: board))
}
func find(start: Board) -> Int {
let target = Board.makeGoal(height: start.height)
var current = start
var totalCost = 0
var possible: [Board: Int] = [:]
var visited: Set<Board> = []
while current != target {
visited.insert(current)
for (next, cost) in current.possibleMoves() where !visited.contains(next) {
possible[next] = min(possible[next, default: .max], totalCost + cost)
}
(current, totalCost) = possible.min { $0.value < $1.value }!
possible.removeValue(forKey: current)
}
return totalCost
}
}
struct Board: Hashable {
enum Piece: CaseIterable, Hashable, CustomStringConvertible {
case a, b, c, d
var homeColumn: Int {
switch self {
case .a:
return 2
case .b:
return 4
case .c:
return 6
case .d:
return 8
}
}
var cost: Int {
switch self {
case .a:
return 1
case .b:
return 10
case .c:
return 100
case .d:
return 1000
}
}
var sign: Character {
switch self {
case .a:
return "A"
case .b:
return "B"
case .c:
return "C"
case .d:
return "D"
}
}
var description: String { String(sign) }
}
enum Cell: Hashable {
case piece(Piece)
case empty
case outside
}
var width = 11
var height = 3
var board: [Cell]
init(height: Int) {
self.height = height
board = Array(repeating: .outside, count: width * height)
for x in 0..<width {
self[x, 0] = .empty
}
}
mutating func fillHomeRow(for piece: Piece, pieces: [Piece]) {
precondition(pieces.count == height - 1)
let x = piece.homeColumn
for (y, piece) in pieces.enumerated() {
self[x, y + 1] = .piece(piece)
}
}
static func makeGoal(height: Int) -> Self {
var board = Self(height: height)
for piece in Piece.allCases {
for y in 1..<board.height {
board[piece.homeColumn, y] = .piece(piece)
}
}
return board
}
func movablePieces() -> [(Piece, Int, Int)] {
var result: [(Piece, Int, Int)] = []
for x in 0..<width {
if case .piece(let piece) = self[x, 0] {
result.append((piece, x, 0))
}
}
for home in Piece.allCases {
let x = home.homeColumn
var y = 1
while y < height, case .empty = self[x, y] {
y += 1
}
if y < height, case let .piece(piece) = self[x, y] {
result.append((piece, x, y))
}
}
return result
}
func possibleMoves() -> [(Board, Int)] {
let pieces = movablePieces()
.filter { !isHome(piece: $0.0, x: $0.1, y: $0.2) }
var result: [(Board, Int)] = []
for (piece, x, y) in pieces {
if let newY = freeHomeRowPosition(piece: piece), freeCorridor(from: x, to: piece.homeColumn) {
result.append(move(piece: piece, from: (x, y), to: (piece.homeColumn, newY)))
}
for newX in openCorridorPositions(x: x) {
result.append(move(piece: piece, from: (x, y), to: (newX, 0)))
}
}
return result
}
func freeCorridor(from x0: Int, to x1: Int) -> Bool {
let minX: Int
let maxX: Int
if x0 < x1 { (minX, maxX) = (x0 + 1, x1) }
else { (minX, maxX) = (x1, x0 - 1) }
for x in minX...maxX {
if self[x, 0] != .empty {
return false
}
}
return true
}
func move(piece: Piece, from: (Int, Int), to: (Int, Int)) -> (Board, Int) {
let (x, y) = from
let (newX, newY) = to
precondition(self[newX,newY] == .empty)
let cost = (y + distance((x, 0), (newX, newY))) * piece.cost
var board = self
board[x, y] = .empty
board[newX, newY] = .piece(piece)
return (board, cost)
}
func freeHomeRowPosition(piece: Piece) -> Int? {
let x = piece.homeColumn
var depth = height - 1
loop: while depth > 0 {
switch self[x, depth] {
case .piece(piece): break
case .piece: return nil
case .empty: break loop
case .outside: preconditionFailure("Invalid board")
}
depth -= 1
}
return depth
}
static let homeColumns = Set(Piece.allCases.map(\.homeColumn))
func openCorridorPositions(x: Int) -> [Int] {
var xmin = x
var xmax = x
while xmin > 0 && self[xmin, 0] == .empty {
xmin -= 1
}
while xmax < width && self[xmax, 0] == .empty {
xmax += 1
}
let result = (xmin..<xmax).filter { !Self.homeColumns.contains($0) && self[$0,0] == .empty }
return result
}
func distance(_ a: (Int, Int), _ b: (Int, Int)) -> Int {
let (x0, y0) = a
let (x1, y1) = b
return abs(x1 - x0) + abs(y1 - y0)
}
func isHome(piece: Piece, x: Int, y: Int) -> Bool {
guard x == piece.homeColumn && y > 0 else { return false }
for otherY in (y + 1)..<height {
if self[x, otherY] != .piece(piece) {
return false
}
}
return true
}
func show() {
print(String(repeating: "#", count: width + 2))
for y in 0..<height {
var line = ""
for x in 0..<width {
switch self[x, y] {
case .outside:
line += "#"
case .empty: line += "."
case .piece(let piece): line.append(piece.sign)
}
}
print("#\(line)#")
}
print(String(repeating: "#", count: width + 2))
}
subscript(x: Int, y: Int) -> Cell {
get {
board[x + width * y]
}
set {
board[x + width * y] = newValue
}
}
}

453
2021/day24.swift Normal file
View file

@ -0,0 +1,453 @@
import Foundation
@main
struct Day24: Puzzle {
func run() {
let program = readInput()
var alus: [(Alu, min: Int, max: Int)] = [(Alu(), 0, 0)]
for instruction in program {
if case .inp(let register) = instruction {
buildNextAlus(&alus, register: register)
} else {
for index in alus.indices {
alus[index].0.run(instruction)
}
}
}
let serialNumbers = alus
.lazy
.filter { $0.0[Alu.resultRegister] == 0 }
print("Part 1:", serialNumbers.map(\.max).max()!)
print("Part 2:", serialNumbers.map(\.min).min()!)
}
func buildNextAlus(_ alus: inout [(Alu, min: Int, max: Int)], register: RegisterId) {
var table: [Alu: Int] = [:]
var newAlus: [(Alu, Int, Int)] = []
for digit: Alu.Register in 1...9 {
for (alu, oldMin, oldMax) in alus {
var alu = alu
alu[register] = digit
let newMin = oldMin * 10 + Int(digit)
let newMax = oldMax * 10 + Int(digit)
if let index = table[alu] {
newAlus[index].1 = min(newAlus[index].1, newMin)
newAlus[index].2 = max(newAlus[index].2, newMax)
} else {
table[alu] = newAlus.count
newAlus.append((alu, newMin, newMax))
}
}
}
print("Alu count", newAlus.count)
alus = newAlus
}
func readInput() -> [Instruction] {
let scanner = Scanner(string: input)
var program: [Instruction] = []
while !scanner.isAtEnd {
program.append(scanner.instruction())
}
return program
}
}
struct Alu: Hashable {
static let resultRegister: RegisterId = 3
typealias Register = Int
var w: Register = 0
var x: Register = 0
var y: Register = 0
var z: Register = 0
func getValue(_ operand: Operand) -> Register {
switch operand {
case .register(let registerId):
return self[registerId]
case .number(let int):
return Register(int)
}
}
subscript(register: RegisterId) -> Register {
_read {
switch register {
case 0: yield w
case 1: yield x
case 2: yield y
case 3: yield z
default: fatalError()
}
}
_modify {
switch register {
case 0: yield &w
case 1: yield &x
case 2: yield &y
case 3: yield &z
default: fatalError()
}
}
}
mutating func run(_ instruction: Instruction) {
switch instruction {
case .inp:
break
case .add(let registerId, let operand):
self[registerId] += getValue(operand)
case .mul(let registerId, let operand):
self[registerId] *= getValue(operand)
case .div(let registerId, let operand):
self[registerId] /= getValue(operand)
case .mod(let registerId, let operand):
self[registerId] %= getValue(operand)
case .eql(let registerId, let operand):
self[registerId] = (self[registerId] == getValue(operand)) ? 1 : 0
}
}
}
typealias RegisterId = Int
enum Operand {
case register(RegisterId)
case number(Int)
}
enum Instruction {
case inp(RegisterId)
case add(RegisterId, Operand)
case mul(RegisterId, Operand)
case div(RegisterId, Operand)
case mod(RegisterId, Operand)
case eql(RegisterId, Operand)
}
extension Scanner {
func register() -> RegisterId? {
if scanString("w") != nil {
return 0
}
if scanString("x") != nil {
return 1
}
if scanString("y") != nil {
return 2
}
if scanString("z") != nil {
return 3
}
return nil
}
func operand() -> Operand {
if let register = register() {
return .register(register)
}
if let int = scanInt() {
return .number(int)
}
fatalError()
}
func instruction() -> Instruction {
guard let command = scanUpToCharacters(from: .whitespaces),
let target = register()
else {
fatalError()
}
switch command {
case "inp": return .inp(target)
case "add": return .add(target, operand())
case "mul": return .mul(target, operand())
case "div": return .div(target, operand())
case "mod": return .mod(target, operand())
case "eql": return .eql(target, operand())
default: fatalError()
}
}
}
let input = """
inp w
mul x 0
add x z
mod x 26
div z 1
add x 14
eql x w
eql x 0
mul y 0
add y 25
mul y x
add y 1
mul z y
mul y 0
add y w
add y 8
mul y x
add z y
inp w
mul x 0
add x z
mod x 26
div z 1
add x 15
eql x w
eql x 0
mul y 0
add y 25
mul y x
add y 1
mul z y
mul y 0
add y w
add y 11
mul y x
add z y
inp w
mul x 0
add x z
mod x 26
div z 1
add x 13
eql x w
eql x 0
mul y 0
add y 25
mul y x
add y 1
mul z y
mul y 0
add y w
add y 2
mul y x
add z y
inp w
mul x 0
add x z
mod x 26
div z 26
add x -10
eql x w
eql x 0
mul y 0
add y 25
mul y x
add y 1
mul z y
mul y 0
add y w
add y 11
mul y x
add z y
inp w
mul x 0
add x z
mod x 26
div z 1
add x 14
eql x w
eql x 0
mul y 0
add y 25
mul y x
add y 1
mul z y
mul y 0
add y w
add y 1
mul y x
add z y
inp w
mul x 0
add x z
mod x 26
div z 26
add x -3
eql x w
eql x 0
mul y 0
add y 25
mul y x
add y 1
mul z y
mul y 0
add y w
add y 5
mul y x
add z y
inp w
mul x 0
add x z
mod x 26
div z 26
add x -14
eql x w
eql x 0
mul y 0
add y 25
mul y x
add y 1
mul z y
mul y 0
add y w
add y 10
mul y x
add z y
inp w
mul x 0
add x z
mod x 26
div z 1
add x 12
eql x w
eql x 0
mul y 0
add y 25
mul y x
add y 1
mul z y
mul y 0
add y w
add y 6
mul y x
add z y
inp w
mul x 0
add x z
mod x 26
div z 1
add x 14
eql x w
eql x 0
mul y 0
add y 25
mul y x
add y 1
mul z y
mul y 0
add y w
add y 1
mul y x
add z y
inp w
mul x 0
add x z
mod x 26
div z 1
add x 12
eql x w
eql x 0
mul y 0
add y 25
mul y x
add y 1
mul z y
mul y 0
add y w
add y 11
mul y x
add z y
inp w
mul x 0
add x z
mod x 26
div z 26
add x -6
eql x w
eql x 0
mul y 0
add y 25
mul y x
add y 1
mul z y
mul y 0
add y w
add y 9
mul y x
add z y
inp w
mul x 0
add x z
mod x 26
div z 26
add x -6
eql x w
eql x 0
mul y 0
add y 25
mul y x
add y 1
mul z y
mul y 0
add y w
add y 14
mul y x
add z y
inp w
mul x 0
add x z
mod x 26
div z 26
add x -2
eql x w
eql x 0
mul y 0
add y 25
mul y x
add y 1
mul z y
mul y 0
add y w
add y 11
mul y x
add z y
inp w
mul x 0
add x z
mod x 26
div z 26
add x -9
eql x w
eql x 0
mul y 0
add y 25
mul y x
add y 1
mul z y
mul y 0
add y w
add y 2
mul y x
add z y
"""

203
2021/day25.swift Normal file
View file

@ -0,0 +1,203 @@
@main struct Day25: Puzzle {
func run() {
var current = input
var stepCount = 0
while true {
let (next1, count1) = step(.right, input: current)
let (next2, count2) = step(.down, input: next1)
stepCount += 1
current = next2
if count1 + count2 == 0 {
break
}
}
print("Part 1: ", stepCount)
}
}
func step(_ space: Space, input: [Space]) -> ([Space], Int) {
var result: [Space] = input
var count = 0
for y in 0..<height {
for x in 0..<width where input[x, y] == space {
let tx = (x + space.dx) % width
let ty = (y + space.dy) % height
if input[tx, ty] == .empty {
result[tx, ty] = space
result[x, y] = .empty
count += 1
}
}
}
return (result, count)
}
extension Array {
subscript(x: Int, y: Int) -> Element {
get {
self[x + width * y]
}
set {
self[x + width * y] = newValue
}
}
}
enum Space: Character {
case right = ">"
case down = "v"
case empty = "."
var dx: Int { self == .right ? 1 : 0 }
var dy: Int { self == .down ? 1 : 0 }
}
let width = 139
let height = 137
let input = """
>>vv..>v..>vv.....>vv...>.>...>>......v>v>.v.v..>v..v>.>v.....v..>vvvv.>.>vv>v..v>.v>..v>>>>.>vv.>>vv>.>>v..>.v.v>v>v.>vv.>.......vv.>.>>v.
v>.>v>.v....>>v.vvv>.>>..>....>.v>.v>vv.v..v.v..v>.>.v>.>..>>v.vvvvv..>>..v.v..>>v>vv>.v..vvv.>v..v>>>v.v..>>...>vv..>>v.v>>.>v>.>.v....>>.
v.>vv....v>..v>>..v.>>.....vv..>.vvv>.....v.v>vvv>..v>>..v...>...>...vv.v..>.vvv>>.v>vv..v.>>>....vv.>.>..v..vv.>>v>.>>.>>.v>>>>.>v.v.>v..v
.v.>>..v...v>v.>>>>v....>.....>...v.v..>.v...>.v.....>>.v>v...>.>.>..>v>>>>.v.>..>......>.v>>.>v..>.>vv.>.v>...v.>v.>>.>...>>>..>>>.vv>..v.
.....v.>.v>........>>.>.>vvv.vv>>.>vvv.v.>vvv.....v..>v..>vvv.>.v>.>..v..vv.>...v>>>>vv>.v..v.>..>..vvvv.>.>vvv....v>..>...v...v.v>v.>..v.v
>..>..>..v.>>..v>..vv....>vv.vv>..>v........>..vv>.>.v.>...v.>v...vvv>.....v..vv...vv>.......v>vv.vv.>.vv..v>..v.v.>..>>>.v..vv>...v>vv..>v
.v.>..vv>.>>v>...>.>.>..>.vvv..v>v.v>>.v...v.vv..v..vvv..vv.v.vv...>v..v....>>>>..vvv>v..>.vv.v.>...>vv>......v...>.v...v....v....>v>.>>...
>v.v.v>>.v.>..>>v>>>v.>v.....v>...>>..v.v>.v..>>..>.vvv...>...v>>v.v....v>.vv>.>.v>.>>>.v>>.v..vvv>.vv......v.v...v...>.vvvvv>.>..>>...vv>.
vv>..v.>>v.>.>v.>..v.v>....vv.v..>>.>>v>>..>>>>v>..v>.>.>.>......vv>vv>>>v....>.>v..>.>>vv...>>v.....>>>....>.v.v>v>..v>v.>vv.v>..>.>.>v>..
v.v.v>v.v..vv.v..>.>v.v.v..>.>.>.>...v.v.>>v>vv.>>....>>..vv...v.v.>..v>.>v.v..v.>.>.>..>...>.vvv...v.v>....vv.>..v>.vv.....>..v>>>.>v.>..v
v...>.>.>v>.v....vv.v>...>v>vv.>..v...>>>..>.>........>..v.>>.v.....>.vvv...>v.>>.vv>v>>>>...>.vv.>...>.>..v..>>>...v.vv.>>v....>.vvv>>v.>.
.>...vv.v........v.v......v>vvv.vv..>...vvv..vv.>.v.v...>vv.>>...>.v..>v>vv>.>v...v.v..vvv.v>>...>.>.vv.>v>v>.v.....>.v.v.>.>v>....>v..>v.>
..v.vvvv.v....>.vvvv.....v.v...>>v>.>.>.vv>>vv.v.v.v.>.vv>vv.>.>v.vvv.>v..v>v.>...>.>>....>.v>...vv...>v>vv...vv>>vvvv..v.v.v>>.>v>..>v>>v.
..>...v.v.....v.v.v>..>..v.>>>..v>v....>v>v..v>.v....>vv>..>.>.....v..>>v..>>v>...>v>v..>.v.>>....>vv.>...vv.........>v>.v..>>v.v.v>>v.v>>>
vv>.v.v>vv.........v..>..>>>>v...>..v...v..v>..vvvv....v.v...v>..>>v.>.v.>v.v.v>..>....>...>v>>vv...>...>>>..vvvv..>.>>v.v>...v..>.v..v.v..
.>.>vvv..v.>.v>.........v...>..v.>....v.v>.v.v.v..>.>.v..v...v.......v...vv.v.v..v..v>..v..>>>.v.v>...>..>.>>>v>>..>>..>..v...>v....>.....>
v.v.vv.vv>..>>v.>>.>.>>v..>v.vv...>vv....>.>....v...v...>v...>..v>.v>v...>..vv>>..vvv>....>vv>vv>.....>v>>v.>>v..>.v...v..v>>..>.v..v..v...
vv...>v.>...vv>.>.v>..v.....v.>...>.>v.>vvv>>>.>>.........>.>..>...v>..vv..v.v.v.>vv.v>>...>>.v..v>v......>v>v.v...vv....>>>..>>v>.vv......
v>.v..v..>.v>>....>.>.>>v>.v.>v.v>.v.>v.v>..v>.>.v>v......>>.>......v..v.v.>.v>>v...>v>>..v....>.v..vv>...>.v..>....v.>.>......v.>.>v>v>..>
>>v>.>>..vv>.>>v.v.>v.v...vv.>>.>>..v...v>v....vv...>>>.v>..vvvv.vv>v>>...v....vv.>.>.v....v>...>vv...v.>.v.vvv.v>.vvv>>vvv>.......>.>.vv.v
...vv>v.v>..>.>.v...vvvv..vvv..>.v>..>.>v.>>>v....>..v>.vvv.>..v.>.>.v....>.......vvvv>.v>.>v.>>>.>vv..>.vv.>>...v..>>v.>>.>v..>vv.>v.>.>>.
>.>>v>>>.....>vvv>.vv.>v..>.>..v>>...>>v..>....>>>v..>.>v..>>v>v.>.>v.vv.v>vv..v>v>>>v.>...>...>>>.v>.v>>>v..vv>v>>>>>v>>.........>v.v>.>>>
.......vv....>v..>..v>v.>.v.v.v>>...v.v.v...v>v.>...v...v.v...>>>.v...>v.v>>.v.>....>>>..>.>v...v....>..>.v..>.v>.>..>>>.v>>.>v.>v>.>.v..>.
..v.v...vvv..>>>vv....>>..>.v>..>>...v.>>v..v...v..v.>>.>...v>..>.........v.>..>.vv.>.v..>.>v.>v>.v.v.vv.>...>v..>.v.v.v...v..>.vv...vv>.v>
v....>v.v>...>...>>.......v....vv>>....>....v...v>>.>v.>....>.>.vv>v.>>..>..>...v>v.v.>>>..>.>>..v>.>...>.v..........>..>>>.>v.v.>.v>.>>...
>>.>.....v...>.>>>vvv>.>>>.v>.v>v..>v..vv>..v>vv.>.v.>v.....>>>v.v.>v.>..>>v..>>..v.v..v.v.>>.v.>..v..>.....v...v>..>>>.v..v....v.....v.>..
....vv..vv.>..vv..>.>..v..v>.v.>v.>>.v...>.v..vv.>v.>>v...>...>..>v>.>...>vv.>.vv..v..>v>v>.v.>..>.>v>....v.>.v>..v...v>..v>>.>..vv.v>v.>.>
v.....>>.>v>v..v..>>v>v>>vvv.v..>....>.>>..vv.....>>..>.....vv>.......v>.>.v..>..v>.v.>.>.>...>.v..v.>>..v.v.vvvv>.v..vv>.>v..>v.v>>.vv...>
v>.v.>>...v.>..v.v>v.vv>..>...>v>>v.v>.v.>..v.>....>.>>v>.>>>vv.>..vvvv..>..>>>...>.....v>..v>>..v.v>..>>v....>.v.v..vv...>....>vv.>.vvvv>.
vv>v>>..>v>>.vvv..>v>vv..>>.vv.v>vv>.v..vv.v...>>....>.v.v>>.>>.>v...v.>...v>>>.v.vv>>v...v.>>..v..vv..v.....>>>.>>v.v>..>.vv>>..>.>.....>.
....vvv>..vv....v...v.v>v>>vvv>.v..vv.>>....>.>.v...>..>....v>...>..>v>vv..>..>>..v..>vv....vvv.>vvvv..v....>....>vv.vvvv.v..>vv.v.>v>.vvv>
>>>>v>v.....vv..>v.v>>.v.v.>..vv.v>>...>v.v..v.v...v>>.>v.v...>..>.>>..>.>v.>>vv.vv.v....v>.v..v....v.v..v.>>....v.>>>>.......>v....>vv.>v.
....v.v.>..v..>vv..v.>>>>.....vvv...v>>.v>..v.>.v>v..v.>v...v.>>>>.>..>..vv.v>.v>..>.>v.....v..>>..>.>v.v..v>v.>>.....>v.v>v.v.vvvv.>.v>>>>
>.>>.vv....>>.>..>........>.>.v...v.>.v......>.>..v.>..v..vv>.vv...vv...v.>v.>....>>......>>>.>vv>.v.>..>vvv...vv.....v>vvv.v...>.vvv.v.>>.
....v>...v...v.v...v...>...>.>v>v.>>...>...v>vv..vv.>.vv>...>v>..>>..>.>v..v>..>.>....v>..v...vv>.vv.>>.v>v.vvvv>.....v.v..>.v..vv>v>>v.>>v
v>..>>v>v.......v.......>v>.>.>.>.v.>>.v.>v>.vv.v.v.v>.v>.v...v.>>..v.....vv.vv...v.>.....>...>..>>>v..v.v>.v>v>v.>..v>v.>.>..v>...>..>v..v
.>..>>vv.>vv.v>v..v>>>....>>v.v..vv>>...>v....v>vvv>.....>.>.>..>v.>...vvv....v...vv..v.>...>...v.vvv..>>..>v.vv>.vvv>.>.>.v.>..>v.>vv..vv>
....>vv.>>>.v..>>v.>vvv...v..>v..vv...vvv.v.vv.v.....>.>vv..v>..>....vvv>..>>.>.....>>>..>v....>..v.>v.v>.>>.v...vv>.v>.v>.v>v....v...vv..v
>.v>....>>.>>...vv..v.>..>.v..>.v>..>.v.v.>v....v>>.v...>>v..>.>>vv..>v>>>.>..v>.>..>.vv.v....>.>.v...>.v.vv>v>v.vv..>.vv.v..vv...>.v..vv>>
..v.vvvv.>>>.......>>>...........v.>.>>>v>v.>....>vv.>.v>..v..>>.>.v.vv>...v...vvvv.>v>vv...>>>.v.v....v.>.>..v.>>>>..>..v..>>..>>.>>>.>>..
v>v>v....vv..v>v.>v..v.v..v..>.v.>>..>>v>....>...v.....>..>.vv.v.vv....>....vvv>.v.v.....>>v>...v.v>.>>>.vvv>>v.>>>.>v...v>.v..>......>>vvv
.>.v.>..>>..>..v..v>v>...vvv>v>>v>.>.>..v>>...>v....v.v...v>vv>>v.v.v.>v>>..>...vvv.....v>>....v...v.v>.v.>...v>.vv...>v......v>>.v>vvv.vvv
.v>.vv.>>..v.>.>...v>.v.>>.vv..>>>....>.>.>.vv..vv.....>v...v.>.>.....vv.v...>.v>v.vv>.>>v..v>>vvv>vv>>..v.v>>..>.>>>>v>...>v.v>.>v...vv...
>>vv>...v.v>..v..>vv..v>...>......>..v.>.v.v>v........v.>vv>v..v>.v.>.v.vv..v....v.>...v>.v>v.v>...>..v....>....>>>v>>vv...vv>vv.>..>.>....
.>.>>.>>.>.v>>...>.....>v..>.>vv.>.>v...vv..vvv....v.vv>v...v>.vv>.......v>.v.>....v>...vvv.>.v...v..>v.>>..v...v.>..v..>.>v...v>v>v>..>v>.
..v..v..v.vv>.v..v.....v.>...v>..>..v.>>>v>>v..>.>...>.v.>...>vv>.v..v...>v>v...>v>v.v>v>>v.>v..>..>vv..v>v.v.v.vvvv.>v>>>.>v>v.v...>v..>>>
..vvv>>..>v..v>>...v.>..v>>>>.....v.>>.........>....>.>..vv...v>..v.>......>.v>.v>..>..>>.vv.>>.>..v.>v...v>.>>>.v>.>.>.>v.>.....>>>vvvv>.v
v..v...vv...>>>>....v..>..v>..>.............>v..v.........vv.>.v>vv...v.v.>>v...>...>..vv....v...>..>..>...>v.....v.>v.>..>>.vv.......>>...
...>...>vv>>>.v>......v.>>>>>..>.>>..v>v>.>..v>>>v.>>......v.vv..vv.>>vv>v.v.>>>v>>.vvvv.>v...v..vvv>vv.....v...v>..vv...>...>>>..v.......v
v>.>>.>v.v>>..v>v....v>>>...v.v>..v..v>>.v..v.v.vvv.v>..>..v.vv...v.v....>..v.>vv>v>..>..>vvvv.>.v.......>v>>...>.>..v>v>.>>>v..>v>..>.....
..>.>.>>...>.v.>v..v..>...>..>v.>v.>..vv..v..>>..v.>>...>v...vv>>>>.>.v..v...>.v>.>v>...v.v.v>.vv......v..>v.....>.>.>.>.>>vv>>...>>.v.v.>.
v.>>>...vv>.>v>.>>v......>>>v.v..>.>.>v>>v>.v.vv.v>>.....>.>.vv.>>v>v......v>>v.v.>>..vv..v..>.v..>.>.>>>.vvv.>v..>..v...>.v.vv>>..v...v..>
vv.>.......v.v.....>.>.v.>>>vv..>v.v..>..v.v>vv.v>>..vv..vvv...>..>>.>.v..>.v>.>>v>.vv>.>v>vv.....v....>v>v>.v>.v..vv..v>.vv..v>...vvv..>.>
..v......v..v.>........v...>..v>v>.v....>..v>.>>v>.>>....>>>>.>.>v>..>.v>vv>>.v....>..>.>v>..v....v>>.>.vvvv.vv....>v.v>..vv.......>>.>.>.>
.>....v..>..vv....>.>....>vv>......v.>.vv...v>>....>>.v>.v>.>..>.>>..v..>....v.>>vvv..v.>>>>>.>>v..vv>v.v...vv.v.v>...>...v>.>vv.vv...v.>v.
>v>.>.>v>v>v......>.>>v....v.>.v.....v..>.v>v..>.v..>>.v>>v>.....>>>.>v....>vv.v.v>v..vv..v>vvvvv...>>>...>..>.v...>.>>..v>v..>vv.>..>.v...
v..>v.>..v..v>....v.>.>v..>....v>v.>.....>>vv.>>v...>v>.>v..vv.>>...v.>>..>v.>.>>.>>>>....v.>.vv>>v.....>.v....>.>..v>v..>.....>...>>.v....
>.v>.vv..>..v.>.....vv.vv.v>>...v>>v..>.v..v..>>v.>.vv...>.v.>..>>v..v..v....v>.>..>.v>v.>......vv..>...>.v>v.....>.>>>.v.v....>>>.>>.vv>>>
>..v>>...>.v..v.>..>v...v...v>..>.>v.v>.>.vvv.v..>.>..>>.>.>v.>vv....v>>.v>>.v>.v.v>v.v>..>.vv...vv.>.v......>>>v.>..>v..v....>v..>>...>.v>
...v>.>vv>..v>v>.vv>.>.>..v......>>v.>.>>.vv>v>.v.v.v.>>..>..>>.v.>.v.>.....v..>..v.>>..v>v>>.>.>.vv.v>vvvvvv.>v>.>>v..v.>.v.>.>>..v.>..vvv
v>..v.>v.......>.v.v>v>v.v.....v>.v>...>v>.>.>.v.vv..>v.>>..v>...v>.>.v>>.vv.v...v....vv.>..>.v>>...v..>>>.v>v....>v.>..>.v...v...>.>..v>..
v>vvvv>vvv.>>...>>.>.v..v....v.>v......>.v.>>.vv....>v>>>>..v......>vv>...>>>.v.v.v>>v..vvv>....>..>v.>v...v.v..>..vv..>>.v..v..>.>.v.>v.>.
......v.>....vv>>>.vv..>.>v>.v....>v..>..>.v.....vv>v....v>.v.vvvv.>.v...vv>.v.vv..>v.v.>>vvv...>vv.v>.v>.v..>.>.>.>>>..v.v>v>vv.vv>.......
>...>v..v..>vv>.v....>.v>.v.v.>..>v>.>..v.>.>v.>.>.v..v.v.>v>>v.>v.>....vv.v..v.>..>>...>...v>>..>....v.v>>v.v>v>.>.v..v.>..v.>>>...>......
....v>>v...v..>.v>.>.v..>>.>.>v..v>.>....v.v>..v>>vv>..vvvv>>...v....vv..>..v.vvv.>.vvvv>....v.>.>v...v...>....v..>.v>vv.v.vvv..v.v>.vvv>..
...v.vv.>v>>v>>.v>..v>.vvv>v>>...v>v>v>.v.>.v..v>.vv>v.v....>.>>vvv..vvv>.v.vv..>..>...>v.>.v.v....v....v>>...v..>.vv.vv.vv.>.>.vv..v.>.>vv
>....vvv>>..v.>.v.v>v.>.v>>>..>.>.vvv.>.>>vv>v.v.>..>...v.v>..v..v.v>v>........v.vv...v>v.>>..>.>...>v..vv>>.>>.>....>vvv>..>.>>....>..>v..
v>>.>>v.>>vvv...v.>.....>.vv...v>>v..v..v.v>>.>..>>v.>>.>.>vvvv..vvvv.>v....>>....vv..v.v>v.>.>..v.>v...vv.>>.>>v.>...vv...v...v.v.>.vv.>..
v..>>>v.>>...v>.>v>..v.v>.>..>>..vvv.v....v>v..>...v>>>.>.vv>v>v.v....vv.vv>>.........>........v.vv>.>v.vv.v.v..>vvvvvv...vv..>>>......v>..
v>.....vvv>.vv>vv.>vv>v...>>v..>..>vvv..vvv....vvvvvvv.>v.>>..>v>>>>...>..v>.v..........>v>...>v>>..v..>>v....vvv..>......vv..v.>>v........
v.....v>....>>v>.vv.>>v.vvv.v>>v>.>..v..>.v>.vv....>>>v>.>vvv>.v.>..>.>>.v.vv..>>.vv.>>.>.>>.>.vv>>..v.v.>.>........>vv>>v.>..>vv.>...vv.>v
>v..>v.....v.....v..v>.v>v.>..>v.v>>>..v.v.>.>.v....>...v.>>v>v>..>..v>>.v......v>.v.v>.v..>v>..>.....vv.>>..>v.vv.vv.....>>.v..>.....v..>.
.>.>>vv>...>>vv.>..>>.v.>v.>vv>.v>>...>..>....v>.v.v>vvv>.v.>>>..v.>.v...v..>......v...>.>>>v..>v.v.>..>.>>..vv.>v....vvv>.>.>>>>..>.vv...v
>......>...v>..>.v.v>.vvv.v>v>..>...>vv..vv>v>>>...vv.>.vv.v>vv..v.v>v.v>>>.>v.>>.>>.v..>.>.>.>..>.vv..vv.v.v.....>..>v..v.vvvv>v......>.>v
..v.v>v.>v.>v>vv>>.>..>..>v>>.vv...>....>v.v.v.vv.....>.>vv....>..v>.>.>.>.>..>>v>v...>....v.vv.>>.v>.>..>>.>v..vvv>....v.>.....v>.>...v..v
v...>v....v....>..v....>vv.>.v.v.vv.......vv.v...>vv>>..>...>.....v.v>.v.v>>.....>>.v.....v.>.vv.v>.>.v..vv...v....>>.v.vvv.v....>.....>>.>
...v.v>.>..>.>.>...vv....>....>v....>.>>vv..>v>v..>...>..v..v.>>>v>>vv...v..>>.>>..v.v.>>.>.v.>...>....v.>.>v.>...>>v....>>.>v.v......>>..v
..vv.v.....v>...>..v...vv.........>.....v..vv>....>vvvv..>v>v.>>>>v.v>..v.v.>..>...>v...>.>...>.>v>>...vv.v..v.>>.....>....v>v>v..>..vv....
..>v>>.>..v.....>v.v.v.v>.>.>v.v.>>>>vv>......>.v.v.>>.>.v>>..vv..v.>...>..v>v.>v>>vvv..v..vv.>.>...>>....v>v..v.......>v.vvvvv...>.>>..>..
v.v..>>v..>.>v..>..v.>...>v.....>vvv..>.>v.v....>.v.v>..>.v>.>.>vvv..>..v...>...v>v.....>v..v.v.>vv..>>...>.v>..v>>.v..v.v>..v>..vvv>..>.v.
.v.vvvvv.>.>>>>vvv>>v>>>....>vv..>..v>..v>>v...>..>v.vvv>....>.v.....>....>.v>.>vv.v>vv..>..>...vv>.v.>>..v.v.v>.>v>.>v>..v..>.>v...v>.v..>
.v.v>..>....>.v>vv..>.>>.....>.>..>v>..vvv.v.v>..vv.>.>>.>..v.>>.>............v.vvv>.v>>>..v..>...>.>v>>...v..v.v...>>v>.>..>>v.v>v..>.>v>.
v>>..v.>v.v.>.>>v......>v...>>...>>>..v...v...>.v...v>>v>>>>vv.>>..>...v....>>...>vv.>v...v.....vv.v.v..v.>...v.v.>....>.>..>v...>.>>.....v
>.v.>.v...v.....>v.vv....>v.>.v.v.v....>.........>v..>....v.vv.v>.v.>...vvv.v.>>>vv.....>.....vv.>>>.>>.v>..>..>.>v.....vvv>v>....v.>..>.>v
v>v.v.>...>.vv..>>.>>.>..v.>.>v.>.v..v>v.>.v.>..vv..>>v..>..v>.v.......v..v..v.>vv.>>v...>....v>..vv.>>..vv.v....v>...v.v....v>.>..v..vv.v.
..>.v.v.>v...>>v>.>.>.vv..>..>v.....v.>>vv..v.>.>..>>..>.vv..v.v..vv...>>.vv>v...>vv>>>v....>vv.v.....>>.vvv.v>>>v....vv.v..v..>.>.v..>..>>
v>..v>.>..>..>....>>v...v..v>..>..>v..>.v>...v...>.>.>.>...>...v.v>..v.>v>.>vv..>..vvv..v>vvv.>..v.v.v.>v..>>>>....v>>>>>v.v..v>>...v..v..v
.>....v>.>...v.v>.....>v....>......>>..v..v..>>vv.vv.v.v.v......>..>.vvv.>>..>v.v.>.>.....v>.>.>>>.>.vv.....v>v.>>v....vv.>.v.v.v....v...>.
v>v.....>.vvv.>....>.>>vv.>>vv>>>.v>>vv>..>..vv...>>>vv>>>..v..vv.>...vvvv>....>......v.v..v.v>..vvv..>.v>.v..>.v...v.vvvv>...v.v>.>>....>.
>>.>v..v.>.v.>...>>.>..>vv.....>v...v...>..>>..>v>..vv>..v.vv>.v.....v....>v..>v.v.>....>>vv>...>v>>..vvv>.......vv..>.v>v.vv>vvvv...>v..>>
..vv..>..vv.v>..v>>>>v....>>......>..v.v...v.v>vvv.>>>>vv..>v..>>..>.>.>v.v.>.>vvv>.v.v..vv...v.....v..vv.v....v.>.v.v.v>>v....v.....v.v>vv
..v.v.>>....v...v>.>.....v.v>....v...v..v..v>.>vv.>v.v>v.v..>>>.v>.>>v>..v>vv.>vv......>v.v.........v..v>v>.vvv.>v>v..>v.v..v.vv.vv>.v>>.>.
v...v.>v>.>..>v>..>>v.>.v>.v>.>.v....vv>..>..vv.>..>v..>v.vv.>.>..>>v..v>.>.....>....>>..vv.>>..>.>....>vv.v..v>.vv.vvv...>.>>......>>..v..
.v..>>...>>..>...>v.>....>>..>.v..>>..>.>..>v>.v>.v.v..>v.>>v>...v.>.v..>..>....>...>.>.>.v....>vv>>>.>v>.vv...>.v.v>v..>>vv..>.>v.v.v.>.v>
v..v...v..vv.>.>.vv...vv.v..>>vv.>..v.v>.>v..>>.>..>>...v..v.>>>.v>>...v.>v.v>>v..>..v>>>v.v..>..v.>vvv..v>>...v..>...>...>>..v..vvv>.>....
...vv.v.>.....>>>vv.....v.>..v..>v..>>v>..>.>.v>.v..>>v.vv>>v...vv..>vv.>.v..v>v....>>....v.>>v...>>.v.v>>v..v.v..v>>>>vv>>..>...>v.....v.>
>>.v..v.>.vv..v.>.v>.>.v>.v>>.>>v.>>..>..>.vvvv>>.v.>>..vv...>vv>.v.>...v>...>.v..>v>.v>v..>>>>.....v.>..v.vv.v.>....v.>.vvv.v>v>..>.>>v>.v
....>.v>>..v.vv...>.v...v>....>v>..v>.>>...>>..vvv.>.vv>>>>v.vv>..>..>vv.v>>..>.>>>>..vv>>.>.....>.v.v>...>.>>>..vv..v...>.>..>>v..v.....>>
>>>.v.v.>>.v..>v.v......>.v>..v>.>>..v>v...v...v>v>..v.>.v...>>v>.>..v...v>..>.>vv.>v.vvv>>.v>..........v.........>.v..>.>...v.....>..>..v.
>.>>>>vv.>>..>.v>...vv..vv>..v>.>>.vv>.>..v...v>...>>.....>v.vv...>>.v>>>.v...>.>v>v.vv.....>>.v>.v...v>>.....>..>>v..v>.v....>v...v.....>v
.v..>.v.v..>.v..v..v.>..v.v>v.v>>vv>vv...>..v.v.....>>..v.v>.v>>.>vv....>>>.>>vv>......v>>..v..>.>v>.>>>....v>vv..>.....>>vv.v..>..v...vv.v
..v.>.>.>v>.v>.>...>..vv..vvv....v.vv..v....>.>.v.v...>....>v>v....v.v..v>vv...v>>v.v....v.v.>..vv..vv.v>>>..v....>>.v>.>.>v...v.v>>>>.v...
v.v..>>.>vv.v>......>v>.>..v>.v.>...>......vv..vvv.v..>...v..v>.>.v>v...v..>....>.>.v.v..vv.>v.>v>.vv>v...v>.vv..v..v...>>.v.v....>>..>>vv.
.vv...vvvv.>.v...v.>.v.>v......>vvv>>v..v>vv.>>.v>v.vv..>.>..>......v>...v.>>>>>.vvv.>>v...>>>v..v.v>>v>...>.....>vv..v.>v>......>>>v...vv>
....>>...v>...>v>>>>v>>>.....>....>.>.>..>>..vv...vv...v.>vv.>>>...>>.>..v>>vv.v.>..vv..>.>v..v.>.v>..>.vv.>v>v>.>...v..v.v...v>.>v...v>>>.
.v.v.v.v>.......v.vv......v..v.vv>>>...>..v.>.>..>......>v..>v>v>vv.>.v.v.>.v.>..v>.v.>>>v..>>..>.v..>...vv>.>>v>.....v..v.>.>.....vvv>..>.
v.....vv>vv>.>vvv..>>...>>v.>v.vv...>....vv.v>>>vv>.>>v.v>>vv.vvv>.>>..>vv>.v...v...v...>...>.>>vv>>v...vvvvvv>..v..>.v..>>>..>.vvv...>vv>v
>..>>>.vvv..vv>>...v.v>...>...vv...>>v.v.>v....v>.....>v.v.>...>v>vv>>.>>.>.>..>v>..v.>>v.v.v.>...>>vv.>>.>.>..v>.v.v....vv..v>.>...v>v.>.>
>v>...v..vv...>.>.v.>>...v>>>.>v.v.v.v.>...vvv.>.v....v....>....v>v>.>vv.>v...v>...v.......>.v.......>v..v...v>.>..v>..>..v>.v..>vv.>v>vv..
vv.>>..v>>vv.v..v.vv>>>>>v>>......v..v...v>vv>>v.v..>.v>vv>v..v......>>>>v>v..>v>.v..v.>..v....vv..v...v>>>..>....>...v...>>>.vv>..>v....>>
..>.v.v.>>.vv...>v>>>v>>.v..>>.>>>.....>>.vv>v>>>>...v...v...v.>>..>>..>>v.>>>>v...>.>v..>.>.>..>.v.>..>v>v.v.vvv>....vvv.v..v.v...v.v>v...
>v.>>>.v.>v.v.>.>v>.v>.>>>vvv.>.v>..>vvv..v..>.>.vv>v.>..vv..v...v.>>v.v>..>.>vv.v.vvv.v...vv>v.vv..>v.v>..vv...>>.......v....>>...>vv..vv>
vv>.>v>.v.vv..>.>.>..>>vv>>v>......v>.v.v>.v.v.....>.>>..>v>v.>>.>..>v.v>v.v>...vv>v>...>>vv..>.>.>>.>>.v.v...vv....>v.v..v>v.>>..v>>..v.>.
.>v>.v.>>...v....>...v..>v>v.>...v.>.....v.>.>.>v......v>>.>v.>.v..>.>>...v..vv..>v..v.>..vv>>..>.>.v..vv.>.>.....>....>.vv.v...v.>v.>v..v>
>v>.>vv..>.>>v>..>v>v.>...>.v.vv>>.>.vvv..>...>....v>.>vv>>..>>>.v>>v>.v..>..v..v>...v.>v...>>..v>>..>.v.>>..v..vv..vv.....v.v..v..vvv....v
vv....>vvv.>.>>>....>.>v>.>vv..v>.>..>.>v>.vv.v.vv.>v>.>>.>>vv>>.....v....>>>vv>>....>vv.v..>.v.vv>..>v....>.>>>>..>.>..vvvv.v.......v...v.
>>..>.>.>vvvv>.>v>>v.v.>>>.v>..>.>.....v>.....vv.>...>>v.>v.>>...>>.>.vvvv>v.....v>v.v>>..v..>..>>.v.>.>...v>...vv>.>v>..vvv......v.>v....>
.vvv...vv.>.>.v..vv>>.>...v>.vv....vv.v..v>........>v.>>>...>.>.....v..>.v.>v.v..>..v..v>.>..>>>vv>..>.v...v>...vvv.v.v.>.v.vv..>>>v..v.>v.
>v.v>>>....vv>v..v..vv>.v.>.....vv.>..v..v>vvv.>>v.v.>.>.>v.>..v>v.v>.v.v...v..v.>.v.>v.>v>.v>v.........v..>v.>v>v.v.v.>..>.>....vvvvvv>.>.
...vv.>v.vv.>v>..v..v.>v>v..>.....>.>>.v.>>.>..>>.v.vv..>v.v>v..>v...v.vv.>.>.v>vv...vv.vv.v.vvv...>v>vvv......v...>>>>.>.v>..vv..>.v>>>..>
>.v...v....v.vv.....>>v..>....vv..vv....>..>>..>.>vvvvv>v..>.>>>>>..>v..v.>>>.v>....v..v>...>...vvvvv...>>.......>>vvvv.>v..v...>..>>>.....
.v.>v..>vv...v>......>vv..v.....v....>.vv.>vvvv.>v>v.>.>v..v......v.>>.>....v.>>v>>v.>v>>v....>.v>.v>...v>.v>v>.>..v>...>>>v>..>>>>>...v.v>
.>>.v>.>.>>>..v.v.v.v>>v..v>.>>v..v>v>>.>>......>v>..>.>vvv.>...>>...>.>>.>>..>>v.....v..>......>...v>vv..v>..v>.vvv.vvv....>.>..vv>>..v>v.
v.>.....v>>vv.v.v>>vvvv.>>vvv...>v>.v.v...>..>>v.>v>.>v>>>....>v..v..v>..>..vv>>.v.v.vv.v..v....v>.v.v.vv...v...>.>.>v....v.>>v..vv.>>.>...
>v>vv.>v...>.v..v.vvv>.>.>>vv>..>>>...>v.>v..vv>vv..v.>>v.>v>v.>..>...v..v>>>vv..>>v...>.v.>.v.>.v>.v.>.>v...v>>>v..vv..v>.v>.vvvv>v.>.v.>.
.>..v..>.vv.>vv.>v.>......>..v....v>.v>vv>...>vv....v...>v>.v..vv>>.......v>v.>v>>....v.>....vv.vv..>.>>v.v...>.>v.>.v.vv.>.>v...v..>>.v...
>.....vv...>v.>v>.>>.vv>>>..vvv.>vvv>..>v..v..v>v>>vvv>v>>..>>v..>..v.v.>v..>...v>.>v....v.v>>..v....v.vvvv...>....v.>..v.>v....v.>.>vv...>
>v..>>.v>>.vv>..>v.>..>>v>v..>..v...>>v.v.>v............>vv.v.vv..>>vv...>..>>.>...v.v>>..>>v..v>.vv..v.v>v..>.vv>>.>>>v>.v>>.>v.vv>..v.v.>
.v..vv....v..v.v.v.........v>>.>v...>v..v>.v.>..vv>...v...v.vv.v>v..>..vv.vv..>vv.v.>.v>vv.>..........>>vv...>v>.....vv..v.>.>v.v>...>....v
.>>.>...v.>v....>.....v>.>v.>.....v.>>..v...>.>v.>>.>>>.vv..>>.>v..v>.>>...>..>.vv.v....>.v..>vv.v...>>.v>.vvv.>.>v.vv..v.>.>>>>.>>...v>v..
...vv>>>v..>..v>vv.>.>>v..v>vvv>..vv.>v>.v..vvvvv.....vv>>.v>v..>..>>.v.v...v...v...>v.v..vv.v.>v>.>v..>v.>.vvv..>.>.v..>.vv..>v>...>>v.>..
v.vv.>>>..v>.vv>...v>....>v>..>v.>.....>vv..>>......>>.>.vv.v..>>..>.>.v.....vv..v.v.....>.v>v.>.>>.>.v>>>.v.>..v..vv..>.v...>..v.>.>>..v.v
..vv...v.>v>.........v>>v.....v..vv.>vv.>.>.>...>>.......>...vv.>vv>.....v.>>.>......>....>>.vv..>....vv.v.vv..v..v....>...v.......>v>..>.>
.>.v..v.....>.>>.v.>>...v.>.v>.v>..v....>>v>>v>.>..>.v.vv.v.>..v>>...vv.v.>>>...v.>>.v>vv.v.vv...>v.>>....v.v....vv>.v.v...>...>...>v.>v...
.v.vv>>v.v.vv>..>.>.>.v.v.>.....vv>>v>vvv.....>>>v.>.v..>>..>.vv>v..v.>vv.>v.>..v.>vv>v>...v>v>>.v.>.....>..v>.>..v>v..........vv......>.vv
.vv..>>>>..v.>.v.>..>.v>.....v.>>v.>.......vv>>v.v>..>>.>v>>>.....>..v......>..v>.vv.>.v.>v>.>.v....>v.>v.v.v..v.vv...v.vv>.v>.....>..v>..v
.v..v>v>>....v....vv>...vv..v>...v..>...>...>..>v..>>>.vv.v....v...>v.>.......v.v.>v....v..v>...........v..>v>>.>.>>v....v.>.v...v.>.v>..v.
""".compactMap { Space(rawValue: $0) }

1078
2021/day3.hs Normal file

File diff suppressed because it is too large Load diff

788
2021/day4.hs Normal file
View file

@ -0,0 +1,788 @@
import Data.Maybe ( catMaybes )
import Data.List ( find )
randoms :: [Int]
randoms = [13,47,64,52,60,69,80,85,57,1,2,6,30,81,86,40,27,26,97,77,70,92,43,94,8,78,3,88,93,17,55,49,32,59,51,28,33,41,83,67,11,91,53,36,96,7,34,79,98,72,39,56,31,75,82,62,99,66,29,58,9,50,54,12,45,68,4,46,38,21,24,18,44,48,16,61,19,0,90,35,65,37,73,20,22,89,42,23,15,87,74,10,71,25,14,76,84,5,63,95]
update 0 (_:rest) newValue = newValue : rest
update n (first:rest) newValue = first : update (n - 1) rest newValue
update _ [] _ = []
updateMatrix :: (Int, Int) -> [[t]] -> t -> [[t]]
updateMatrix (x, y) matrix newValue = update y matrix $ update x (matrix !! y) newValue
newtype Board = Board [[Maybe Int]] deriving Show
board :: [[Int]] -> Board
board list = Board $ (map $ map Just) list
boards :: [Board]
boards = [
board [
[88, 67, 20, 19, 15],
[22, 76, 86, 44, 73],
[ 7, 42, 6, 69, 25],
[12, 68, 92, 21, 75],
[97, 45, 13, 52, 70]
],
board [
[75, 98, 24, 18, 77],
[17, 93, 46, 49, 13],
[92, 56, 97, 57, 66],
[44, 0, 65, 54, 74],
[23, 6, 53, 42, 20]
],
board [
[92, 94, 9, 27, 41],
[73, 28, 62, 90, 40],
[78, 3, 12, 37, 32],
[ 8, 86, 91, 16, 30],
[84, 38, 68, 11, 19]
],
board [
[51, 5, 12, 76, 97],
[72, 31, 15, 61, 71],
[38, 32, 55, 87, 10],
[91, 4, 85, 84, 53],
[59, 79, 28, 69, 23]
],
board [
[35, 48, 10, 81, 60],
[25, 86, 24, 43, 15],
[44, 55, 12, 54, 62],
[94, 89, 95, 2, 23],
[64, 63, 45, 50, 66]
],
board [
[80, 87, 49, 88, 39],
[33, 81, 95, 68, 55],
[83, 46, 36, 41, 54],
[90, 74, 3, 52, 7],
[71, 40, 35, 8, 77]
],
board [
[34, 21, 24, 8, 97],
[99, 23, 94, 70, 9],
[14, 98, 2, 11, 10],
[16, 38, 92, 13, 35],
[82, 25, 76, 42, 39]
],
board [
[52, 76, 98, 25, 3],
[24, 41, 13, 39, 56],
[11, 72, 77, 47, 86],
[50, 32, 26, 88, 48],
[18, 99, 22, 78, 58]
],
board [
[22, 24, 53, 84, 80],
[26, 97, 42, 95, 11],
[ 1, 59, 81, 7, 35],
[47, 14, 40, 63, 30],
[37, 31, 10, 50, 43]
],
board [
[26, 86, 85, 69, 45],
[81, 43, 96, 12, 42],
[ 7, 36, 5, 28, 95],
[55, 90, 54, 4, 46],
[52, 30, 79, 59, 87]
],
board [
[76, 87, 62, 13, 38],
[40, 44, 75, 2, 37],
[51, 22, 58, 84, 57],
[ 1, 29, 82, 67, 35],
[39, 20, 31, 77, 32]
],
board [
[53, 62, 61, 26, 95],
[78, 44, 9, 5, 97],
[83, 11, 18, 69, 2],
[94, 66, 8, 14, 27],
[ 1, 6, 7, 73, 76]
],
board [
[87, 34, 62, 93, 43],
[49, 20, 63, 29, 22],
[30, 94, 11, 5, 69],
[74, 9, 89, 41, 37],
[98, 38, 72, 13, 97]
],
board [
[69, 39, 15, 59, 14],
[42, 84, 56, 23, 1],
[99, 16, 62, 83, 89],
[32, 36, 33, 24, 3],
[22, 31, 55, 10, 13]
],
board [
[22, 44, 75, 3, 17],
[51, 79, 37, 59, 19],
[98, 4, 86, 35, 34],
[36, 20, 85, 5, 23],
[62, 92, 43, 7, 90]
],
board [
[ 3, 68, 95, 51, 71],
[20, 62, 47, 4, 30],
[60, 91, 27, 86, 73],
[14, 76, 66, 97, 85],
[79, 65, 8, 11, 36]
],
board [
[38, 63, 1, 64, 14],
[10, 52, 17, 90, 98],
[28, 61, 29, 20, 55],
[49, 58, 43, 3, 7],
[88, 81, 67, 32, 68]
],
board [
[63, 65, 15, 61, 57],
[47, 94, 6, 14, 49],
[42, 3, 83, 96, 31],
[23, 77, 1, 70, 18],
[45, 36, 64, 48, 35]
],
board [
[ 8, 92, 88, 32, 95],
[26, 41, 34, 11, 48],
[81, 35, 36, 62, 28],
[64, 33, 52, 97, 82],
[ 7, 37, 78, 0, 86]
],
board [
[75, 64, 84, 80, 60],
[17, 5, 55, 95, 70],
[ 0, 90, 68, 53, 93],
[ 8, 50, 38, 9, 65],
[97, 35, 26, 30, 6]
],
board [
[76, 87, 36, 5, 74],
[79, 94, 82, 48, 20],
[24, 88, 90, 62, 45],
[ 9, 40, 78, 22, 68],
[73, 71, 35, 42, 66]
],
board [
[52, 11, 17, 9, 72],
[45, 13, 90, 0, 80],
[93, 77, 37, 51, 96],
[ 3, 68, 94, 61, 1],
[26, 78, 12, 29, 66]
],
board [
[49, 14, 74, 11, 48],
[73, 58, 54, 3, 40],
[90, 38, 62, 18, 26],
[53, 93, 19, 41, 94],
[35, 63, 7, 80, 68]
],
board [
[13, 19, 92, 38, 1],
[63, 69, 4, 99, 36],
[53, 57, 54, 21, 80],
[97, 17, 2, 44, 29],
[ 0, 3, 89, 45, 58]
],
board [
[ 9, 26, 45, 57, 67],
[72, 10, 59, 79, 88],
[32, 2, 87, 42, 60],
[62, 98, 1, 93, 28],
[24, 99, 41, 44, 29]
],
board [
[57, 60, 54, 36, 84],
[28, 5, 32, 66, 77],
[13, 19, 42, 39, 37],
[56, 89, 74, 50, 55],
[ 8, 71, 78, 0, 80]
],
board [
[62, 38, 24, 44, 4],
[17, 8, 70, 77, 86],
[98, 6, 9, 88, 23],
[85, 63, 78, 60, 72],
[71, 48, 36, 69, 81]
],
board [
[84, 19, 8, 20, 17],
[40, 88, 33, 52, 81],
[29, 83, 11, 36, 92],
[66, 6, 73, 32, 82],
[44, 39, 26, 60, 63]
],
board [
[13, 73, 45, 32, 42],
[69, 20, 83, 8, 84],
[82, 61, 11, 89, 25],
[ 6, 92, 99, 52, 57],
[88, 22, 46, 26, 86]
],
board [
[74, 65, 17, 64, 94],
[19, 34, 40, 69, 80],
[43, 83, 45, 77, 87],
[41, 49, 13, 51, 89],
[91, 72, 54, 1, 60]
],
board [
[38, 10, 16, 51, 46],
[80, 60, 64, 62, 70],
[89, 71, 58, 49, 39],
[37, 5, 35, 88, 40],
[93, 72, 98, 42, 13]
],
board [
[ 9, 47, 91, 69, 68],
[27, 1, 49, 60, 13],
[ 6, 17, 95, 59, 35],
[26, 14, 75, 57, 11],
[15, 18, 19, 46, 74]
],
board [
[88, 66, 25, 89, 6],
[63, 4, 56, 73, 8],
[57, 0, 51, 5, 36],
[68, 96, 84, 67, 53],
[49, 82, 7, 32, 9]
],
board [
[81, 75, 92, 1, 62],
[ 0, 96, 27, 63, 46],
[76, 31, 93, 67, 12],
[74, 78, 59, 5, 60],
[69, 33, 25, 94, 43]
],
board [
[40, 72, 79, 58, 22],
[16, 24, 99, 96, 44],
[69, 4, 87, 90, 26],
[34, 43, 56, 15, 35],
[63, 88, 89, 52, 54]
],
board [
[43, 7, 44, 31, 24],
[71, 18, 84, 17, 64],
[ 8, 47, 93, 85, 36],
[72, 29, 22, 67, 74],
[69, 41, 58, 98, 61]
],
board [
[55, 21, 72, 14, 3],
[75, 12, 69, 91, 54],
[80, 40, 78, 39, 4],
[44, 88, 84, 76, 25],
[96, 57, 49, 52, 28]
],
board [
[54, 74, 32, 40, 64],
[ 5, 94, 71, 80, 22],
[82, 92, 79, 93, 16],
[53, 33, 98, 85, 14],
[29, 49, 9, 47, 12]
],
board [
[98, 67, 8, 10, 6],
[58, 13, 77, 99, 81],
[ 5, 55, 21, 19, 1],
[ 0, 26, 44, 70, 93],
[41, 96, 31, 91, 27]
],
board [
[50, 28, 48, 13, 18],
[96, 43, 25, 2, 78],
[88, 60, 0, 16, 73],
[12, 32, 15, 68, 22],
[95, 74, 10, 80, 21]
],
board [
[18, 49, 85, 55, 21],
[11, 68, 80, 59, 41],
[56, 94, 14, 62, 60],
[32, 20, 40, 6, 2],
[42, 66, 98, 71, 17]
],
board [
[13, 38, 1, 63, 82],
[33, 55, 54, 53, 92],
[36, 20, 39, 84, 83],
[67, 43, 70, 73, 75],
[94, 77, 76, 29, 16]
],
board [
[82, 27, 25, 18, 86],
[73, 3, 36, 28, 1],
[11, 96, 40, 23, 93],
[58, 90, 88, 35, 64],
[ 9, 38, 69, 8, 43]
],
board [
[87, 90, 16, 56, 67],
[41, 75, 89, 1, 80],
[22, 62, 5, 45, 69],
[28, 36, 19, 96, 71],
[26, 63, 88, 76, 31]
],
board [
[68, 80, 83, 95, 20],
[75, 0, 16, 38, 21],
[34, 89, 87, 36, 14],
[94, 47, 53, 73, 71],
[63, 8, 61, 96, 50]
],
board [
[81, 23, 6, 14, 26],
[86, 42, 82, 95, 85],
[77, 52, 38, 2, 33],
[69, 98, 54, 37, 4],
[78, 39, 3, 75, 80]
],
board [
[92, 99, 93, 28, 44],
[ 5, 8, 67, 45, 10],
[61, 79, 63, 85, 81],
[ 2, 87, 76, 68, 18],
[69, 52, 22, 16, 12]
],
board [
[95, 50, 21, 82, 60],
[ 5, 8, 6, 28, 26],
[52, 3, 38, 70, 74],
[75, 0, 53, 51, 44],
[10, 30, 34, 47, 71]
],
board [
[71, 44, 65, 48, 51],
[78, 57, 75, 6, 86],
[95, 58, 66, 12, 92],
[22, 61, 68, 88, 50],
[ 4, 36, 45, 28, 54]
],
board [
[37, 17, 5, 2, 52],
[57, 47, 4, 53, 39],
[11, 72, 66, 81, 46],
[27, 0, 67, 40, 83],
[98, 19, 10, 35, 84]
],
board [
[43, 59, 30, 72, 17],
[66, 50, 12, 84, 65],
[49, 60, 14, 1, 29],
[89, 75, 62, 82, 47],
[33, 5, 9, 58, 45]
],
board [
[14, 9, 3, 47, 74],
[69, 29, 57, 62, 22],
[ 4, 90, 40, 64, 15],
[21, 27, 30, 2, 63],
[97, 96, 99, 55, 41]
],
board [
[75, 73, 3, 59, 80],
[65, 34, 52, 20, 72],
[50, 84, 81, 69, 41],
[97, 77, 19, 85, 39],
[88, 46, 15, 35, 87]
],
board [
[96, 42, 74, 38, 78],
[58, 73, 67, 70, 10],
[62, 8, 82, 64, 16],
[65, 25, 13, 3, 89],
[40, 30, 53, 95, 51]
],
board [
[ 7, 16, 92, 88, 38],
[14, 52, 46, 93, 64],
[49, 48, 8, 76, 51],
[97, 67, 89, 75, 19],
[69, 9, 29, 43, 82]
],
board [
[81, 51, 24, 57, 9],
[46, 43, 77, 11, 35],
[83, 5, 14, 25, 84],
[70, 99, 47, 37, 16],
[ 3, 39, 75, 97, 80]
],
board [
[18, 74, 64, 6, 94],
[12, 59, 46, 48, 31],
[73, 77, 33, 1, 39],
[ 0, 69, 10, 24, 56],
[83, 66, 5, 76, 58]
],
board [
[40, 48, 72, 65, 2],
[19, 28, 93, 53, 44],
[75, 85, 42, 68, 66],
[99, 49, 55, 31, 41],
[94, 35, 78, 13, 61]
],
board [
[ 4, 20, 54, 33, 21],
[50, 61, 17, 53, 64],
[69, 30, 24, 90, 95],
[82, 51, 39, 52, 67],
[43, 73, 44, 62, 83]
],
board [
[31, 32, 63, 42, 60],
[39, 41, 28, 51, 53],
[15, 20, 24, 54, 5],
[ 9, 65, 70, 57, 99],
[50, 29, 35, 4, 47]
],
board [
[40, 99, 95, 72, 35],
[10, 14, 52, 83, 19],
[ 5, 51, 87, 49, 16],
[60, 66, 13, 63, 93],
[68, 57, 31, 6, 78]
],
board [
[58, 96, 49, 87, 28],
[95, 50, 54, 53, 52],
[24, 16, 64, 9, 5],
[ 7, 63, 8, 4, 17],
[59, 98, 3, 31, 25]
],
board [
[31, 83, 61, 58, 93],
[94, 52, 97, 30, 98],
[99, 2, 13, 66, 73],
[69, 71, 68, 40, 19],
[74, 84, 45, 25, 77]
],
board [
[58, 85, 45, 64, 74],
[18, 88, 91, 53, 2],
[93, 0, 92, 55, 39],
[75, 49, 87, 80, 4],
[89, 97, 57, 14, 54]
],
board [
[20, 92, 64, 50, 25],
[52, 90, 80, 31, 38],
[55, 54, 10, 76, 21],
[95, 97, 4, 77, 19],
[30, 26, 12, 39, 11]
],
board [
[71, 10, 84, 68, 77],
[48, 82, 69, 75, 3],
[93, 24, 16, 42, 60],
[15, 62, 76, 36, 20],
[21, 18, 94, 22, 45]
],
board [
[10, 91, 66, 56, 75],
[ 6, 8, 45, 59, 83],
[52, 93, 48, 81, 87],
[99, 78, 43, 64, 84],
[21, 12, 61, 71, 9]
],
board [
[98, 77, 95, 63, 15],
[30, 14, 39, 12, 20],
[13, 32, 27, 0, 5],
[86, 80, 51, 40, 99],
[68, 44, 26, 29, 91]
],
board [
[92, 79, 49, 44, 33],
[88, 4, 34, 3, 90],
[51, 46, 31, 50, 47],
[61, 11, 94, 6, 24],
[72, 18, 98, 65, 57]
],
board [
[88, 94, 93, 11, 33],
[75, 77, 53, 54, 51],
[97, 15, 89, 38, 76],
[47, 64, 55, 22, 0],
[40, 56, 34, 19, 3]
],
board [
[36, 55, 51, 86, 91],
[49, 21, 78, 6, 58],
[90, 1, 88, 45, 33],
[37, 69, 75, 41, 50],
[81, 24, 34, 38, 93]
],
board [
[21, 73, 99, 50, 65],
[72, 77, 86, 7, 68],
[24, 63, 71, 26, 25],
[ 9, 12, 29, 93, 87],
[81, 23, 22, 94, 67]
],
board [
[37, 47, 66, 89, 73],
[49, 23, 79, 31, 86],
[58, 52, 21, 39, 15],
[60, 38, 82, 50, 36],
[74, 30, 25, 35, 99]
],
board [
[ 6, 18, 53, 36, 87],
[ 3, 59, 50, 2, 75],
[69, 61, 57, 19, 63],
[44, 77, 42, 22, 7],
[89, 29, 45, 35, 71]
],
board [
[28, 26, 53, 47, 21],
[31, 71, 27, 58, 85],
[10, 20, 74, 59, 42],
[89, 44, 12, 91, 54],
[32, 87, 36, 22, 7]
],
board [
[62, 17, 61, 75, 51],
[44, 60, 37, 14, 76],
[96, 0, 1, 52, 5],
[57, 42, 97, 66, 90],
[12, 23, 50, 98, 25]
],
board [
[42, 30, 86, 89, 66],
[41, 98, 39, 29, 23],
[75, 73, 20, 79, 90],
[38, 60, 45, 16, 18],
[17, 10, 47, 5, 13]
],
board [
[ 4, 10, 26, 74, 38],
[66, 84, 60, 23, 57],
[30, 59, 58, 2, 49],
[83, 82, 70, 64, 43],
[71, 31, 35, 90, 0]
],
board [
[27, 99, 33, 56, 3],
[41, 97, 1, 68, 88],
[43, 63, 81, 89, 22],
[30, 32, 59, 64, 12],
[84, 58, 10, 39, 76]
],
board [
[98, 16, 75, 27, 57],
[ 0, 9, 3, 79, 50],
[ 5, 34, 93, 6, 21],
[52, 70, 87, 31, 49],
[58, 46, 24, 20, 45]
],
board [
[78, 24, 19, 13, 30],
[83, 59, 79, 37, 72],
[84, 81, 99, 17, 77],
[10, 93, 3, 33, 70],
[29, 35, 49, 6, 5]
],
board [
[47, 6, 82, 94, 53],
[83, 19, 25, 54, 64],
[ 9, 56, 39, 31, 96],
[ 1, 81, 66, 41, 5],
[55, 48, 43, 12, 14]
],
board [
[47, 55, 86, 31, 17],
[89, 45, 65, 34, 56],
[99, 88, 18, 97, 3],
[52, 21, 14, 68, 13],
[ 9, 26, 22, 7, 32]
],
board [
[75, 27, 62, 19, 72],
[20, 49, 7, 21, 85],
[53, 46, 1, 59, 99],
[61, 71, 87, 24, 83],
[ 5, 77, 41, 51, 73]
],
board [
[57, 59, 82, 77, 52],
[99, 49, 81, 37, 54],
[70, 89, 23, 20, 90],
[31, 1, 21, 98, 66],
[86, 35, 46, 36, 18]
],
board [
[37, 39, 70, 76, 27],
[68, 84, 25, 1, 33],
[50, 82, 77, 20, 44],
[18, 11, 51, 62, 54],
[80, 67, 35, 89, 30]
],
board [
[85, 96, 1, 3, 73],
[25, 47, 10, 46, 98],
[ 2, 33, 91, 71, 35],
[ 0, 32, 11, 55, 67],
[14, 81, 17, 5, 94]
],
board [
[68, 84, 46, 43, 81],
[42, 35, 48, 89, 30],
[ 4, 17, 65, 77, 6],
[49, 97, 85, 12, 66],
[75, 25, 13, 90, 51]
],
board [
[94, 42, 46, 58, 56],
[97, 50, 86, 84, 15],
[52, 9, 28, 32, 59],
[26, 96, 91, 57, 83],
[29, 99, 18, 31, 43]
],
board [
[ 2, 19, 31, 10, 32],
[81, 16, 50, 59, 7],
[76, 30, 63, 44, 95],
[82, 54, 61, 75, 36],
[85, 78, 12, 67, 9]
],
board [
[92, 1, 72, 27, 37],
[22, 13, 91, 4, 34],
[53, 82, 76, 70, 19],
[99, 38, 59, 33, 52],
[ 0, 61, 36, 67, 75]
],
board [
[94, 41, 5, 57, 1],
[37, 36, 99, 34, 47],
[40, 93, 62, 32, 76],
[61, 75, 48, 42, 73],
[35, 69, 54, 13, 50]
],
board [
[76, 12, 51, 11, 74],
[30, 83, 73, 33, 78],
[95, 77, 15, 14, 80],
[86, 37, 91, 50, 10],
[52, 67, 3, 60, 17]
],
board [
[68, 7, 42, 81, 15],
[46, 58, 9, 31, 18],
[91, 1, 28, 34, 37],
[17, 57, 6, 2, 70],
[97, 54, 20, 27, 44]
],
board [
[44, 80, 45, 28, 14],
[94, 47, 29, 50, 54],
[64, 67, 96, 95, 93],
[76, 36, 82, 39, 43],
[30, 55, 97, 3, 2]
],
board [
[43, 2, 15, 99, 34],
[97, 75, 26, 9, 67],
[30, 63, 74, 12, 82],
[18, 6, 49, 48, 55],
[47, 36, 41, 56, 83]
],
board [
[26, 70, 87, 80, 89],
[17, 50, 61, 21, 96],
[43, 83, 85, 46, 64],
[66, 75, 23, 47, 69],
[22, 72, 55, 52, 8]
],
board [
[67, 54, 11, 29, 42],
[16, 45, 56, 86, 66],
[ 4, 80, 43, 72, 91],
[90, 87, 63, 39, 50],
[32, 6, 59, 27, 89]
],
board [
[14, 92, 78, 47, 59],
[98, 0, 63, 85, 31],
[52, 8, 84, 70, 91],
[43, 3, 76, 65, 57],
[87, 22, 99, 94, 58]
],
board [
[26, 53, 58, 52, 1],
[82, 57, 32, 40, 20],
[72, 21, 74, 46, 43],
[41, 15, 98, 2, 11],
[ 5, 96, 22, 18, 55]
]
]
row n (Board board) = board !! n
column n (Board board) = [ x !! n | x <- board ]
score :: Board -> Int
score (Board board) = sum $ catMaybes $ concat board
mark :: Board -> (Int, Int) -> Either Board Int
mark (Board board) pos = if isWin then Right $ score newBoard else Left newBoard
where
newBoard = Board $ updateMatrix pos board Nothing
isWin = row y newBoard == cleared || column x newBoard == cleared
(x, y) = pos
cleared = replicate 5 Nothing
findPosition :: Board -> Int -> Maybe (Int, Int)
findPosition (Board board) value = find (\(x,y) -> board !! y !! x == Just value) indexes
where
indexes = [(x,y) | x <- [0..4], y <- [0..4]]
markTile :: Board -> Int -> Either Board Int
markTile board num = case pos of
Nothing -> Left board
Just pos -> mark board pos
where
pos = findPosition board num
markAll :: [Board] -> Int -> Either [Board] Int
markAll boards num = getResult $ map (`markTile` num) boards
where
getResult [] = Left []
getResult (Right score : rest) = Right score
getResult (Left board : rest) = case getResult rest of
Left boards -> Left $ board : boards
Right score -> Right score
markAllUntilLast :: [Board] -> Int -> Either [Board] Int
markAllUntilLast boards num = getResult $ map (`markTile` num) boards
where
getResult [] = Left []
getResult [Right score] = Right score
getResult (Right _ : rest) = getResult rest
getResult (Left board : rest) = case getResult rest of
Left boards -> Left $ board : boards
Right _ -> Left [board]
play :: [Board] -> [Int] -> ([Board] -> Int -> Either [Board] Int) -> Maybe Int
play boards numbers markAll = playRound numbers boards
where
playRound [] _ = Nothing
playRound (cur:rest) boards = case markAll boards cur of
Left newBoards -> playRound rest newBoards
Right score -> Just (score * cur)
{-
>>> play boards randoms markAll
Just 49686
>>> play boards randoms markAllUntilLast
Just 26878
-}

554
2021/day5.hs Normal file
View file

@ -0,0 +1,554 @@
example :: [((Int, Int), (Int, Int))]
example = [
((0,9), (5,9)),
((8,0), (0,8)),
((9,4), (3,4)),
((2,2), (2,1)),
((7,0), (7,4)),
((6,4), (2,0)),
((0,9), (2,9)),
((3,4), (1,4)),
((0,0), (8,8)),
((5,5), (8,2))
]
isHorizontalOrVertical :: ((Int, Int), (Int, Int)) -> Bool
isHorizontalOrVertical ((x1, y1), (x2, y2)) = x1 == x2 || y1 == y2
atPoint (x,y) p = minX p <= x && x <= maxX p && minY p <= y && y <= maxY p
minX ((x1, _), (x2, _)) = min x1 x2
minY ((_, y1), (_, y2)) = min y1 y2
maxX ((x1, _), (x2, _)) = max x1 x2
maxY ((_, y1), (_, y2)) = max y1 y2
bounds :: (Ord b, Ord a) => [((a, b), (a, b))] -> ((a, b), (a, b))
bounds input = ((x0, y0), (x1, y1))
where
x0 = minimum $ map minX input
y0 = minimum $ map minY input
x1 = maximum $ map maxX input
y1 = maximum $ map maxY input
grid ((x0, y0), (x1, y1)) = [ (x, y) | x <- [x0..x1], y <- [y0..y1] ]
counts input = length $ filter (>= 2) intersectionCounts
where
intersectionCounts = map (\pos -> length $ filter (atPoint pos) input) gridIndexes
gridIndexes = grid $ bounds input
inputs :: [((Int, Int), (Int, Int))]
inputs = [
((911, 808), (324, 221)),
((161, 890), (808, 243)),
((174, 59), (174, 760)),
((983, 983), (10, 10)),
((321, 12), (870, 12)),
((66, 936), (941, 61)),
((670, 141), (670, 550)),
((783, 935), (496, 648)),
((973, 651), (635, 989)),
((535, 47), (535, 154)),
((355, 183), (754, 582)),
((172, 111), (892, 111)),
((353, 66), (907, 620)),
((741, 960), (741, 805)),
((113, 895), (946, 895)),
((777, 280), (563, 280)),
((679, 815), (626, 815)),
((651, 848), (651, 673)),
((205, 834), (205, 599)),
((895, 118), (82, 931)),
((685, 303), (93, 895)),
((973, 38), (62, 949)),
((867, 23), (867, 300)),
((784, 947), (784, 47)),
((96, 168), (755, 827)),
((909, 321), (909, 716)),
((59, 881), (692, 881)),
((964, 19), (69, 914)),
((752, 869), (67, 184)),
((974, 877), (138, 41)),
((432, 389), (137, 684)),
((458, 822), (458, 402)),
((818, 852), (308, 342)),
((882, 484), (441, 925)),
((82, 959), (976, 65)),
((117, 487), (117, 429)),
((214, 673), (429, 673)),
((72, 955), (72, 829)),
((587, 109), (587, 368)),
((576, 17), (576, 872)),
((685, 102), (685, 905)),
((563, 394), (716, 394)),
((966, 145), (150, 961)),
((555, 582), (555, 385)),
((453, 31), (453, 207)),
((639, 815), (547, 723)),
((431, 869), (431, 811)),
((646, 938), (599, 938)),
((215, 513), (900, 513)),
((809, 82), (798, 82)),
((768, 344), (244, 868)),
((39, 962), (39, 601)),
((675, 186), (61, 186)),
((861, 967), (28, 967)),
((860, 550), (538, 550)),
((283, 740), (571, 740)),
((72, 297), (72, 645)),
((727, 801), (727, 526)),
((799, 519), (799, 497)),
((915, 24), (174, 765)),
((795, 943), (136, 943)),
((518, 971), (599, 971)),
((594, 676), (594, 461)),
((850, 799), (363, 799)),
((958, 575), (958, 231)),
((752, 576), (398, 576)),
((891, 433), (398, 433)),
((524, 126), (397, 126)),
((10, 890), (796, 104)),
((57, 228), (168, 228)),
((168, 521), (338, 691)),
((230, 83), (777, 83)),
((865, 677), (640, 452)),
((866, 821), (825, 821)),
((17, 143), (17, 596)),
((113, 916), (113, 601)),
((268, 187), (551, 470)),
((794, 167), (220, 167)),
((459, 17), (459, 931)),
((211, 31), (526, 31)),
((680, 57), (756, 57)),
((926, 190), (926, 800)),
((85, 284), (63, 284)),
((44, 988), (44, 701)),
((110, 941), (176, 941)),
((480, 163), (480, 112)),
((574, 538), (574, 371)),
((584, 473), (69, 473)),
((303, 621), (303, 380)),
((762, 652), (762, 89)),
((286, 195), (276, 185)),
((957, 87), (217, 827)),
((561, 858), (561, 437)),
((384, 55), (81, 55)),
((19, 977), (981, 15)),
((454, 747), (938, 263)),
((425, 836), (425, 617)),
((860, 135), (775, 50)),
((633, 131), (633, 651)),
((904, 912), (242, 250)),
((880, 177), (480, 577)),
((470, 162), (964, 656)),
((585, 376), (585, 470)),
((696, 760), (594, 862)),
((534, 225), (534, 717)),
((258, 816), (258, 847)),
((990, 244), (990, 93)),
((463, 462), (463, 533)),
((434, 928), (537, 825)),
((813, 734), (533, 734)),
((498, 673), (395, 673)),
((564, 312), (55, 312)),
((280, 550), (939, 550)),
((591, 247), (396, 52)),
((127, 516), (127, 235)),
((850, 425), (552, 127)),
((894, 428), (894, 598)),
((366, 960), (592, 960)),
((579, 488), (170, 488)),
((775, 92), (775, 586)),
((49, 909), (930, 28)),
((856, 113), (284, 685)),
((263, 175), (120, 175)),
((332, 592), (276, 592)),
((920, 157), (141, 157)),
((349, 776), (316, 776)),
((187, 863), (279, 863)),
((218, 872), (83, 872)),
((465, 430), (410, 430)),
((710, 218), (857, 218)),
((797, 314), (184, 314)),
((387, 327), (49, 665)),
((950, 812), (205, 67)),
((803, 133), (803, 682)),
((125, 972), (545, 552)),
((353, 901), (840, 414)),
((936, 843), (202, 109)),
((11, 904), (856, 59)),
((725, 757), (954, 986)),
((227, 697), (345, 697)),
((187, 520), (187, 441)),
((860, 262), (135, 987)),
((700, 95), (976, 371)),
((86, 946), (869, 163)),
((898, 806), (461, 806)),
((717, 796), (717, 195)),
((882, 127), (835, 127)),
((133, 48), (133, 191)),
((521, 51), (521, 927)),
((384, 806), (957, 233)),
((570, 139), (570, 842)),
((949, 819), (949, 350)),
((592, 230), (283, 230)),
((315, 856), (741, 856)),
((870, 674), (549, 353)),
((857, 306), (857, 889)),
((428, 217), (267, 217)),
((47, 93), (898, 944)),
((636, 238), (665, 238)),
((202, 910), (202, 737)),
((246, 432), (617, 803)),
((985, 24), (48, 961)),
((965, 876), (956, 867)),
((618, 650), (810, 458)),
((292, 356), (575, 356)),
((394, 585), (910, 585)),
((137, 453), (137, 178)),
((509, 737), (509, 665)),
((193, 350), (531, 688)),
((805, 219), (107, 219)),
((975, 506), (907, 506)),
((435, 303), (435, 380)),
((344, 83), (344, 224)),
((47, 66), (47, 115)),
((570, 516), (857, 516)),
((162, 91), (926, 91)),
((759, 417), (759, 460)),
((445, 942), (445, 699)),
((421, 340), (421, 743)),
((590, 590), (434, 434)),
((453, 38), (453, 327)),
((865, 134), (865, 773)),
((842, 609), (18, 609)),
((662, 282), (62, 882)),
((489, 32), (344, 32)),
((135, 496), (93, 454)),
((552, 211), (421, 211)),
((620, 678), (642, 678)),
((782, 158), (585, 355)),
((733, 509), (733, 574)),
((932, 383), (369, 946)),
((843, 705), (843, 725)),
((747, 414), (676, 343)),
((294, 218), (962, 886)),
((844, 175), (844, 420)),
((255, 489), (531, 213)),
((555, 532), (821, 532)),
((533, 15), (533, 161)),
((631, 778), (631, 401)),
((75, 282), (468, 282)),
((903, 838), (903, 957)),
((46, 293), (543, 790)),
((30, 834), (30, 948)),
((591, 720), (591, 965)),
((624, 36), (339, 36)),
((425, 323), (425, 442)),
((234, 939), (234, 963)),
((482, 912), (968, 912)),
((228, 614), (189, 614)),
((969, 472), (969, 692)),
((871, 494), (871, 172)),
((101, 624), (848, 624)),
((424, 918), (69, 563)),
((929, 671), (93, 671)),
((81, 187), (707, 813)),
((348, 923), (348, 924)),
((921, 524), (921, 828)),
((678, 454), (678, 364)),
((904, 227), (904, 596)),
((163, 344), (609, 790)),
((206, 180), (206, 59)),
((145, 519), (145, 717)),
((317, 679), (317, 417)),
((503, 724), (221, 724)),
((353, 448), (413, 448)),
((363, 643), (837, 643)),
((594, 54), (359, 54)),
((866, 117), (45, 938)),
((939, 210), (284, 865)),
((410, 556), (410, 801)),
((905, 111), (673, 111)),
((983, 167), (574, 167)),
((595, 758), (97, 758)),
((785, 10), (437, 10)),
((517, 414), (517, 734)),
((691, 567), (186, 62)),
((842, 51), (31, 862)),
((36, 199), (282, 199)),
((864, 758), (864, 610)),
((639, 918), (951, 918)),
((245, 516), (245, 474)),
((951, 203), (557, 203)),
((176, 728), (176, 171)),
((322, 217), (387, 217)),
((149, 208), (836, 895)),
((661, 298), (609, 298)),
((46, 47), (981, 982)),
((769, 45), (769, 610)),
((988, 932), (988, 459)),
((901, 97), (901, 908)),
((195, 395), (121, 395)),
((197, 403), (327, 533)),
((159, 456), (857, 456)),
((480, 981), (881, 580)),
((86, 958), (962, 82)),
((375, 198), (763, 198)),
((950, 381), (341, 381)),
((504, 679), (504, 598)),
((756, 659), (680, 583)),
((146, 328), (886, 328)),
((930, 412), (492, 850)),
((954, 54), (954, 940)),
((790, 498), (790, 305)),
((83, 270), (83, 242)),
((939, 268), (939, 563)),
((423, 756), (916, 263)),
((583, 756), (583, 34)),
((957, 639), (614, 639)),
((484, 523), (521, 560)),
((497, 809), (497, 419)),
((76, 17), (979, 920)),
((49, 39), (943, 933)),
((110, 289), (110, 247)),
((874, 868), (874, 172)),
((576, 127), (53, 650)),
((871, 879), (12, 20)),
((436, 711), (592, 711)),
((132, 285), (225, 285)),
((245, 147), (514, 147)),
((158, 882), (956, 84)),
((21, 984), (937, 68)),
((42, 275), (219, 275)),
((877, 143), (889, 143)),
((593, 841), (508, 756)),
((414, 289), (132, 289)),
((687, 655), (767, 655)),
((453, 981), (459, 987)),
((635, 433), (635, 324)),
((671, 347), (170, 848)),
((412, 579), (915, 579)),
((269, 677), (269, 596)),
((587, 121), (367, 341)),
((153, 883), (153, 709)),
((524, 580), (508, 580)),
((541, 232), (651, 232)),
((93, 948), (284, 757)),
((168, 745), (872, 41)),
((831, 657), (925, 563)),
((908, 389), (442, 389)),
((462, 445), (234, 445)),
((735, 493), (895, 493)),
((274, 624), (296, 646)),
((153, 130), (153, 160)),
((466, 214), (466, 769)),
((474, 499), (686, 711)),
((540, 428), (788, 676)),
((858, 215), (959, 215)),
((788, 91), (788, 410)),
((552, 505), (988, 505)),
((978, 312), (978, 202)),
((108, 321), (616, 829)),
((903, 359), (903, 770)),
((480, 331), (480, 769)),
((503, 842), (34, 842)),
((613, 732), (323, 442)),
((767, 949), (654, 949)),
((514, 589), (386, 589)),
((38, 554), (284, 308)),
((689, 268), (689, 711)),
((860, 66), (190, 736)),
((253, 865), (622, 865)),
((87, 658), (698, 47)),
((506, 892), (829, 569)),
((680, 910), (594, 824)),
((824, 603), (958, 603)),
((576, 802), (562, 802)),
((67, 27), (67, 489)),
((969, 461), (517, 913)),
((674, 763), (674, 226)),
((223, 955), (218, 955)),
((147, 540), (569, 962)),
((455, 703), (596, 703)),
((746, 899), (746, 403)),
((516, 476), (756, 476)),
((897, 674), (373, 150)),
((120, 395), (120, 49)),
((722, 443), (722, 244)),
((724, 924), (724, 39)),
((809, 930), (109, 930)),
((822, 816), (874, 816)),
((796, 539), (895, 539)),
((340, 88), (560, 88)),
((223, 158), (593, 158)),
((779, 977), (856, 900)),
((617, 461), (973, 817)),
((515, 62), (515, 140)),
((12, 586), (724, 586)),
((870, 50), (391, 50)),
((308, 123), (308, 696)),
((119, 164), (863, 908)),
((755, 599), (448, 599)),
((129, 526), (633, 526)),
((478, 668), (102, 668)),
((237, 637), (237, 743)),
((270, 102), (72, 300)),
((115, 470), (115, 427)),
((948, 233), (948, 731)),
((983, 135), (468, 650)),
((748, 439), (748, 642)),
((62, 862), (352, 572)),
((765, 901), (660, 901)),
((917, 807), (917, 587)),
((55, 81), (116, 81)),
((954, 972), (102, 120)),
((340, 503), (294, 549)),
((970, 661), (522, 213)),
((618, 92), (618, 247)),
((688, 965), (965, 965)),
((94, 241), (94, 292)),
((15, 132), (15, 492)),
((979, 927), (488, 927)),
((509, 26), (984, 26)),
((840, 530), (840, 95)),
((55, 956), (849, 162)),
((297, 297), (297, 472)),
((338, 780), (369, 780)),
((487, 292), (37, 292)),
((122, 117), (206, 201)),
((66, 807), (564, 309)),
((643, 242), (906, 242)),
((909, 833), (909, 441)),
((129, 128), (818, 817)),
((406, 42), (406, 297)),
((53, 20), (967, 934)),
((235, 285), (601, 285)),
((275, 625), (275, 539)),
((199, 732), (430, 963)),
((639, 187), (639, 265)),
((549, 740), (549, 824)),
((603, 140), (603, 748)),
((35, 455), (176, 455)),
((888, 611), (888, 271)),
((134, 154), (484, 154)),
((694, 820), (694, 814)),
((535, 584), (187, 932)),
((642, 510), (642, 249)),
((191, 886), (268, 886)),
((918, 353), (881, 390)),
((977, 13), (343, 13)),
((380, 243), (271, 134)),
((410, 758), (410, 670)),
((613, 551), (519, 645)),
((963, 84), (124, 923)),
((702, 252), (821, 252)),
((405, 237), (405, 22)),
((21, 139), (21, 510)),
((548, 499), (132, 499)),
((196, 104), (196, 680)),
((739, 145), (476, 145)),
((751, 746), (91, 746)),
((975, 628), (975, 847)),
((935, 520), (935, 450)),
((803, 372), (803, 393)),
((872, 77), (872, 373)),
((339, 130), (339, 103)),
((226, 886), (226, 45)),
((794, 647), (794, 257)),
((90, 922), (889, 123)),
((615, 971), (615, 574)),
((26, 278), (26, 719)),
((838, 88), (806, 88)),
((263, 691), (804, 150)),
((309, 721), (910, 721)),
((510, 496), (960, 946)),
((195, 236), (46, 236)),
((610, 143), (610, 610)),
((891, 412), (891, 268)),
((714, 21), (156, 579)),
((320, 935), (320, 96)),
((240, 782), (449, 782)),
((754, 472), (48, 472)),
((105, 481), (529, 57)),
((451, 301), (451, 965)),
((796, 638), (796, 185)),
((908, 553), (771, 553)),
((98, 543), (490, 935)),
((481, 159), (762, 159)),
((593, 527), (419, 353)),
((86, 391), (216, 521)),
((260, 716), (42, 716)),
((734, 538), (375, 179)),
((24, 974), (975, 23)),
((402, 466), (787, 851)),
((344, 409), (262, 327)),
((803, 443), (685, 443)),
((986, 152), (249, 152)),
((125, 738), (90, 773)),
((184, 772), (184, 746)),
((729, 829), (729, 340)),
((226, 527), (226, 375)),
((936, 231), (222, 945)),
((254, 333), (254, 167)),
((451, 234), (451, 273)),
((915, 790), (568, 443)),
((869, 794), (504, 429)),
((11, 878), (836, 53)),
((821, 231), (522, 530)),
((285, 419), (732, 866)),
((191, 272), (191, 679)),
((41, 651), (225, 651)),
((30, 13), (879, 862)),
((980, 488), (20, 488)),
((27, 187), (27, 348)),
((53, 238), (53, 514)),
((778, 306), (379, 705)),
((425, 399), (425, 60)),
((162, 859), (57, 859)),
((652, 926), (652, 589)),
((962, 489), (555, 896)),
((197, 378), (436, 617)),
((310, 190), (310, 760)),
((678, 20), (678, 713)),
((390, 653), (985, 58)),
((938, 351), (656, 69)),
((881, 39), (18, 902)),
((313, 681), (323, 681)),
((910, 907), (288, 907)),
((739, 977), (739, 132)),
((856, 479), (154, 479)),
((893, 785), (761, 785)),
((405, 247), (405, 901)),
((58, 933), (808, 183)),
((643, 156), (676, 189)),
((149, 773), (357, 773)),
((479, 518), (434, 518)),
((389, 518), (556, 685)),
((858, 449), (533, 774)),
((503, 133), (409, 133)),
((340, 315), (219, 194)),
((183, 701), (183, 242)),
((810, 151), (195, 151)),
((446, 686), (446, 912)),
((968, 482), (49, 482)),
((203, 20), (203, 667)),
((493, 516), (647, 516)),
((900, 91), (634, 91)),
((660, 554), (119, 13)),
((964, 864), (964, 919)),
((871, 293), (344, 293)),
((895, 258), (972, 258))
]
{-
5167
-}

584
2021/day5part2.swift Normal file
View file

@ -0,0 +1,584 @@
import CoreGraphics
import ImageIO
import Foundation
import UniformTypeIdentifiers
@main
struct Day5Part2 {
let input: [(Point, Point)] = [
((911, 808), (324, 221)),
((161, 890), (808, 243)),
((174, 59), (174, 760)),
((983, 983), (10, 10)),
((321, 12), (870, 12)),
((66, 936), (941, 61)),
((670, 141), (670, 550)),
((783, 935), (496, 648)),
((973, 651), (635, 989)),
((535, 47), (535, 154)),
((355, 183), (754, 582)),
((172, 111), (892, 111)),
((353, 66), (907, 620)),
((741, 960), (741, 805)),
((113, 895), (946, 895)),
((777, 280), (563, 280)),
((679, 815), (626, 815)),
((651, 848), (651, 673)),
((205, 834), (205, 599)),
((895, 118), (82, 931)),
((685, 303), (93, 895)),
((973, 38), (62, 949)),
((867, 23), (867, 300)),
((784, 947), (784, 47)),
((96, 168), (755, 827)),
((909, 321), (909, 716)),
((59, 881), (692, 881)),
((964, 19), (69, 914)),
((752, 869), (67, 184)),
((974, 877), (138, 41)),
((432, 389), (137, 684)),
((458, 822), (458, 402)),
((818, 852), (308, 342)),
((882, 484), (441, 925)),
((82, 959), (976, 65)),
((117, 487), (117, 429)),
((214, 673), (429, 673)),
((72, 955), (72, 829)),
((587, 109), (587, 368)),
((576, 17), (576, 872)),
((685, 102), (685, 905)),
((563, 394), (716, 394)),
((966, 145), (150, 961)),
((555, 582), (555, 385)),
((453, 31), (453, 207)),
((639, 815), (547, 723)),
((431, 869), (431, 811)),
((646, 938), (599, 938)),
((215, 513), (900, 513)),
((809, 82), (798, 82)),
((768, 344), (244, 868)),
((39, 962), (39, 601)),
((675, 186), (61, 186)),
((861, 967), (28, 967)),
((860, 550), (538, 550)),
((283, 740), (571, 740)),
((72, 297), (72, 645)),
((727, 801), (727, 526)),
((799, 519), (799, 497)),
((915, 24), (174, 765)),
((795, 943), (136, 943)),
((518, 971), (599, 971)),
((594, 676), (594, 461)),
((850, 799), (363, 799)),
((958, 575), (958, 231)),
((752, 576), (398, 576)),
((891, 433), (398, 433)),
((524, 126), (397, 126)),
((10, 890), (796, 104)),
((57, 228), (168, 228)),
((168, 521), (338, 691)),
((230, 83), (777, 83)),
((865, 677), (640, 452)),
((866, 821), (825, 821)),
((17, 143), (17, 596)),
((113, 916), (113, 601)),
((268, 187), (551, 470)),
((794, 167), (220, 167)),
((459, 17), (459, 931)),
((211, 31), (526, 31)),
((680, 57), (756, 57)),
((926, 190), (926, 800)),
((85, 284), (63, 284)),
((44, 988), (44, 701)),
((110, 941), (176, 941)),
((480, 163), (480, 112)),
((574, 538), (574, 371)),
((584, 473), (69, 473)),
((303, 621), (303, 380)),
((762, 652), (762, 89)),
((286, 195), (276, 185)),
((957, 87), (217, 827)),
((561, 858), (561, 437)),
((384, 55), (81, 55)),
((19, 977), (981, 15)),
((454, 747), (938, 263)),
((425, 836), (425, 617)),
((860, 135), (775, 50)),
((633, 131), (633, 651)),
((904, 912), (242, 250)),
((880, 177), (480, 577)),
((470, 162), (964, 656)),
((585, 376), (585, 470)),
((696, 760), (594, 862)),
((534, 225), (534, 717)),
((258, 816), (258, 847)),
((990, 244), (990, 93)),
((463, 462), (463, 533)),
((434, 928), (537, 825)),
((813, 734), (533, 734)),
((498, 673), (395, 673)),
((564, 312), (55, 312)),
((280, 550), (939, 550)),
((591, 247), (396, 52)),
((127, 516), (127, 235)),
((850, 425), (552, 127)),
((894, 428), (894, 598)),
((366, 960), (592, 960)),
((579, 488), (170, 488)),
((775, 92), (775, 586)),
((49, 909), (930, 28)),
((856, 113), (284, 685)),
((263, 175), (120, 175)),
((332, 592), (276, 592)),
((920, 157), (141, 157)),
((349, 776), (316, 776)),
((187, 863), (279, 863)),
((218, 872), (83, 872)),
((465, 430), (410, 430)),
((710, 218), (857, 218)),
((797, 314), (184, 314)),
((387, 327), (49, 665)),
((950, 812), (205, 67)),
((803, 133), (803, 682)),
((125, 972), (545, 552)),
((353, 901), (840, 414)),
((936, 843), (202, 109)),
((11, 904), (856, 59)),
((725, 757), (954, 986)),
((227, 697), (345, 697)),
((187, 520), (187, 441)),
((860, 262), (135, 987)),
((700, 95), (976, 371)),
((86, 946), (869, 163)),
((898, 806), (461, 806)),
((717, 796), (717, 195)),
((882, 127), (835, 127)),
((133, 48), (133, 191)),
((521, 51), (521, 927)),
((384, 806), (957, 233)),
((570, 139), (570, 842)),
((949, 819), (949, 350)),
((592, 230), (283, 230)),
((315, 856), (741, 856)),
((870, 674), (549, 353)),
((857, 306), (857, 889)),
((428, 217), (267, 217)),
((47, 93), (898, 944)),
((636, 238), (665, 238)),
((202, 910), (202, 737)),
((246, 432), (617, 803)),
((985, 24), (48, 961)),
((965, 876), (956, 867)),
((618, 650), (810, 458)),
((292, 356), (575, 356)),
((394, 585), (910, 585)),
((137, 453), (137, 178)),
((509, 737), (509, 665)),
((193, 350), (531, 688)),
((805, 219), (107, 219)),
((975, 506), (907, 506)),
((435, 303), (435, 380)),
((344, 83), (344, 224)),
((47, 66), (47, 115)),
((570, 516), (857, 516)),
((162, 91), (926, 91)),
((759, 417), (759, 460)),
((445, 942), (445, 699)),
((421, 340), (421, 743)),
((590, 590), (434, 434)),
((453, 38), (453, 327)),
((865, 134), (865, 773)),
((842, 609), (18, 609)),
((662, 282), (62, 882)),
((489, 32), (344, 32)),
((135, 496), (93, 454)),
((552, 211), (421, 211)),
((620, 678), (642, 678)),
((782, 158), (585, 355)),
((733, 509), (733, 574)),
((932, 383), (369, 946)),
((843, 705), (843, 725)),
((747, 414), (676, 343)),
((294, 218), (962, 886)),
((844, 175), (844, 420)),
((255, 489), (531, 213)),
((555, 532), (821, 532)),
((533, 15), (533, 161)),
((631, 778), (631, 401)),
((75, 282), (468, 282)),
((903, 838), (903, 957)),
((46, 293), (543, 790)),
((30, 834), (30, 948)),
((591, 720), (591, 965)),
((624, 36), (339, 36)),
((425, 323), (425, 442)),
((234, 939), (234, 963)),
((482, 912), (968, 912)),
((228, 614), (189, 614)),
((969, 472), (969, 692)),
((871, 494), (871, 172)),
((101, 624), (848, 624)),
((424, 918), (69, 563)),
((929, 671), (93, 671)),
((81, 187), (707, 813)),
((348, 923), (348, 924)),
((921, 524), (921, 828)),
((678, 454), (678, 364)),
((904, 227), (904, 596)),
((163, 344), (609, 790)),
((206, 180), (206, 59)),
((145, 519), (145, 717)),
((317, 679), (317, 417)),
((503, 724), (221, 724)),
((353, 448), (413, 448)),
((363, 643), (837, 643)),
((594, 54), (359, 54)),
((866, 117), (45, 938)),
((939, 210), (284, 865)),
((410, 556), (410, 801)),
((905, 111), (673, 111)),
((983, 167), (574, 167)),
((595, 758), (97, 758)),
((785, 10), (437, 10)),
((517, 414), (517, 734)),
((691, 567), (186, 62)),
((842, 51), (31, 862)),
((36, 199), (282, 199)),
((864, 758), (864, 610)),
((639, 918), (951, 918)),
((245, 516), (245, 474)),
((951, 203), (557, 203)),
((176, 728), (176, 171)),
((322, 217), (387, 217)),
((149, 208), (836, 895)),
((661, 298), (609, 298)),
((46, 47), (981, 982)),
((769, 45), (769, 610)),
((988, 932), (988, 459)),
((901, 97), (901, 908)),
((195, 395), (121, 395)),
((197, 403), (327, 533)),
((159, 456), (857, 456)),
((480, 981), (881, 580)),
((86, 958), (962, 82)),
((375, 198), (763, 198)),
((950, 381), (341, 381)),
((504, 679), (504, 598)),
((756, 659), (680, 583)),
((146, 328), (886, 328)),
((930, 412), (492, 850)),
((954, 54), (954, 940)),
((790, 498), (790, 305)),
((83, 270), (83, 242)),
((939, 268), (939, 563)),
((423, 756), (916, 263)),
((583, 756), (583, 34)),
((957, 639), (614, 639)),
((484, 523), (521, 560)),
((497, 809), (497, 419)),
((76, 17), (979, 920)),
((49, 39), (943, 933)),
((110, 289), (110, 247)),
((874, 868), (874, 172)),
((576, 127), (53, 650)),
((871, 879), (12, 20)),
((436, 711), (592, 711)),
((132, 285), (225, 285)),
((245, 147), (514, 147)),
((158, 882), (956, 84)),
((21, 984), (937, 68)),
((42, 275), (219, 275)),
((877, 143), (889, 143)),
((593, 841), (508, 756)),
((414, 289), (132, 289)),
((687, 655), (767, 655)),
((453, 981), (459, 987)),
((635, 433), (635, 324)),
((671, 347), (170, 848)),
((412, 579), (915, 579)),
((269, 677), (269, 596)),
((587, 121), (367, 341)),
((153, 883), (153, 709)),
((524, 580), (508, 580)),
((541, 232), (651, 232)),
((93, 948), (284, 757)),
((168, 745), (872, 41)),
((831, 657), (925, 563)),
((908, 389), (442, 389)),
((462, 445), (234, 445)),
((735, 493), (895, 493)),
((274, 624), (296, 646)),
((153, 130), (153, 160)),
((466, 214), (466, 769)),
((474, 499), (686, 711)),
((540, 428), (788, 676)),
((858, 215), (959, 215)),
((788, 91), (788, 410)),
((552, 505), (988, 505)),
((978, 312), (978, 202)),
((108, 321), (616, 829)),
((903, 359), (903, 770)),
((480, 331), (480, 769)),
((503, 842), (34, 842)),
((613, 732), (323, 442)),
((767, 949), (654, 949)),
((514, 589), (386, 589)),
((38, 554), (284, 308)),
((689, 268), (689, 711)),
((860, 66), (190, 736)),
((253, 865), (622, 865)),
((87, 658), (698, 47)),
((506, 892), (829, 569)),
((680, 910), (594, 824)),
((824, 603), (958, 603)),
((576, 802), (562, 802)),
((67, 27), (67, 489)),
((969, 461), (517, 913)),
((674, 763), (674, 226)),
((223, 955), (218, 955)),
((147, 540), (569, 962)),
((455, 703), (596, 703)),
((746, 899), (746, 403)),
((516, 476), (756, 476)),
((897, 674), (373, 150)),
((120, 395), (120, 49)),
((722, 443), (722, 244)),
((724, 924), (724, 39)),
((809, 930), (109, 930)),
((822, 816), (874, 816)),
((796, 539), (895, 539)),
((340, 88), (560, 88)),
((223, 158), (593, 158)),
((779, 977), (856, 900)),
((617, 461), (973, 817)),
((515, 62), (515, 140)),
((12, 586), (724, 586)),
((870, 50), (391, 50)),
((308, 123), (308, 696)),
((119, 164), (863, 908)),
((755, 599), (448, 599)),
((129, 526), (633, 526)),
((478, 668), (102, 668)),
((237, 637), (237, 743)),
((270, 102), (72, 300)),
((115, 470), (115, 427)),
((948, 233), (948, 731)),
((983, 135), (468, 650)),
((748, 439), (748, 642)),
((62, 862), (352, 572)),
((765, 901), (660, 901)),
((917, 807), (917, 587)),
((55, 81), (116, 81)),
((954, 972), (102, 120)),
((340, 503), (294, 549)),
((970, 661), (522, 213)),
((618, 92), (618, 247)),
((688, 965), (965, 965)),
((94, 241), (94, 292)),
((15, 132), (15, 492)),
((979, 927), (488, 927)),
((509, 26), (984, 26)),
((840, 530), (840, 95)),
((55, 956), (849, 162)),
((297, 297), (297, 472)),
((338, 780), (369, 780)),
((487, 292), (37, 292)),
((122, 117), (206, 201)),
((66, 807), (564, 309)),
((643, 242), (906, 242)),
((909, 833), (909, 441)),
((129, 128), (818, 817)),
((406, 42), (406, 297)),
((53, 20), (967, 934)),
((235, 285), (601, 285)),
((275, 625), (275, 539)),
((199, 732), (430, 963)),
((639, 187), (639, 265)),
((549, 740), (549, 824)),
((603, 140), (603, 748)),
((35, 455), (176, 455)),
((888, 611), (888, 271)),
((134, 154), (484, 154)),
((694, 820), (694, 814)),
((535, 584), (187, 932)),
((642, 510), (642, 249)),
((191, 886), (268, 886)),
((918, 353), (881, 390)),
((977, 13), (343, 13)),
((380, 243), (271, 134)),
((410, 758), (410, 670)),
((613, 551), (519, 645)),
((963, 84), (124, 923)),
((702, 252), (821, 252)),
((405, 237), (405, 22)),
((21, 139), (21, 510)),
((548, 499), (132, 499)),
((196, 104), (196, 680)),
((739, 145), (476, 145)),
((751, 746), (91, 746)),
((975, 628), (975, 847)),
((935, 520), (935, 450)),
((803, 372), (803, 393)),
((872, 77), (872, 373)),
((339, 130), (339, 103)),
((226, 886), (226, 45)),
((794, 647), (794, 257)),
((90, 922), (889, 123)),
((615, 971), (615, 574)),
((26, 278), (26, 719)),
((838, 88), (806, 88)),
((263, 691), (804, 150)),
((309, 721), (910, 721)),
((510, 496), (960, 946)),
((195, 236), (46, 236)),
((610, 143), (610, 610)),
((891, 412), (891, 268)),
((714, 21), (156, 579)),
((320, 935), (320, 96)),
((240, 782), (449, 782)),
((754, 472), (48, 472)),
((105, 481), (529, 57)),
((451, 301), (451, 965)),
((796, 638), (796, 185)),
((908, 553), (771, 553)),
((98, 543), (490, 935)),
((481, 159), (762, 159)),
((593, 527), (419, 353)),
((86, 391), (216, 521)),
((260, 716), (42, 716)),
((734, 538), (375, 179)),
((24, 974), (975, 23)),
((402, 466), (787, 851)),
((344, 409), (262, 327)),
((803, 443), (685, 443)),
((986, 152), (249, 152)),
((125, 738), (90, 773)),
((184, 772), (184, 746)),
((729, 829), (729, 340)),
((226, 527), (226, 375)),
((936, 231), (222, 945)),
((254, 333), (254, 167)),
((451, 234), (451, 273)),
((915, 790), (568, 443)),
((869, 794), (504, 429)),
((11, 878), (836, 53)),
((821, 231), (522, 530)),
((285, 419), (732, 866)),
((191, 272), (191, 679)),
((41, 651), (225, 651)),
((30, 13), (879, 862)),
((980, 488), (20, 488)),
((27, 187), (27, 348)),
((53, 238), (53, 514)),
((778, 306), (379, 705)),
((425, 399), (425, 60)),
((162, 859), (57, 859)),
((652, 926), (652, 589)),
((962, 489), (555, 896)),
((197, 378), (436, 617)),
((310, 190), (310, 760)),
((678, 20), (678, 713)),
((390, 653), (985, 58)),
((938, 351), (656, 69)),
((881, 39), (18, 902)),
((313, 681), (323, 681)),
((910, 907), (288, 907)),
((739, 977), (739, 132)),
((856, 479), (154, 479)),
((893, 785), (761, 785)),
((405, 247), (405, 901)),
((58, 933), (808, 183)),
((643, 156), (676, 189)),
((149, 773), (357, 773)),
((479, 518), (434, 518)),
((389, 518), (556, 685)),
((858, 449), (533, 774)),
((503, 133), (409, 133)),
((340, 315), (219, 194)),
((183, 701), (183, 242)),
((810, 151), (195, 151)),
((446, 686), (446, 912)),
((968, 482), (49, 482)),
((203, 20), (203, 667)),
((493, 516), (647, 516)),
((900, 91), (634, 91)),
((660, 554), (119, 13)),
((964, 864), (964, 919)),
((871, 293), (344, 293)),
((895, 258), (972, 258)),
]
func run() {
var minX = 0
var maxX = 0
var minY = 0
var maxY = 0
for line in input {
minX = min(minX, line.0.x, line.1.x)
maxX = max(maxX, line.0.x, line.1.x)
minY = min(minY, line.0.y, line.1.y)
maxY = max(maxY, line.0.y, line.1.y)
}
var grid = Grid(minX: minX, minY: minY, maxX: maxX, maxY: maxY)
for line in input { grid.line(line) }
print(grid.grid.lazy.filter { $0 >= 2 }.count)
let data = CGDataProvider(dataInfo: nil, data: &grid.grid, size: grid.grid.count, releaseData: {_,_,_ in })
let image = CGImage(width: maxX - minX, height: maxY - minY, bitsPerComponent: 8, bitsPerPixel: 8, bytesPerRow: maxX - minX, space: .init(name: CGColorSpace.linearGray)!, bitmapInfo: [], provider: data!, decode: nil, shouldInterpolate: false, intent: .defaultIntent)
let url = URL(fileURLWithPath: "/Users/sven/test.png")
let dest = CGImageDestinationCreateWithURL(url as CFURL, UTType.png.identifier as CFString, 1, nil)
CGImageDestinationAddImage(dest!, image!, nil)
CGImageDestinationFinalize(dest!)
}
struct Grid {
let minX: Int
let minY: Int
let width: Int
var grid: [UInt8]
init(minX: Int, minY: Int, maxX: Int, maxY: Int) {
self.minX = minX
self.minY = minY
width = maxX - minX + 1
let height = maxY - minY + 1
grid = Array(repeating: 0, count: width * height)
}
subscript(x: Int, y:Int) -> UInt8 {
get { grid[(y - minY) * width + x - minX] }
set { grid[(y - minY) * width + x - minX] = newValue }
}
mutating func line(_ line: (Point, Point)) {
let ((minX, minY), (maxX, maxY)) = line
var x = minX
var y = minY
let xStep = minX == maxX ? 0 : minX < maxX ? 1 : -1
let yStep = minY == maxY ? 0 : minY < maxY ? 1 : -1
while true {
self[x, y] += 1
if x == maxX && y == maxY {
break
}
x += xStep
y += yStep
}
}
}
typealias Point = (x: Int, y: Int)
static func main() {
Self().run()
}
}

36
2021/day6.swift Normal file
View file

@ -0,0 +1,36 @@
@main
struct Day6 {
let input = [3,3,2,1,4,1,1,2,3,1,1,2,1,2,1,1,1,1,1,1,4,1,1,5,2,1,1,2,1,1,1,3,5,1,5,5,1,1,1,1,3,1,1,3,2,1,1,1,1,1,1,4,1,1,1,1,1,1,1,4,1,3,3,1,1,3,1,3,1,2,1,3,1,1,4,1,2,4,4,5,1,1,1,1,1,1,4,1,5,1,1,5,1,1,3,3,1,3,2,5,2,4,1,4,1,2,4,5,1,1,5,1,1,1,4,1,1,5,2,1,1,5,1,1,1,5,1,1,1,1,1,3,1,5,3,2,1,1,2,2,1,2,1,1,5,1,1,4,5,1,4,3,1,1,1,1,1,1,5,1,1,1,5,2,1,1,1,5,1,1,1,4,4,2,1,1,1,1,1,1,1,3,1,1,4,4,1,4,1,1,5,3,1,1,1,5,2,2,4,2,1,1,3,1,5,5,1,1,1,4,1,5,1,1,1,4,3,3,3,1,3,1,5,1,4,2,1,1,5,1,1,1,5,5,1,1,2,1,1,1,3,1,1,1,2,3,1,2,2,3,1,3,1,1,4,1,1,2,1,1,1,1,3,5,1,1,2,1,1,1,4,1,1,1,1,1,2,4,1,1,5,3,1,1,1,2,2,2,1,5,1,3,5,3,1,1,4,1,1,4]
var population: [Int] = .init(repeating: 0, count: 9)
func step() -> [Int] {
var result = Array(repeating: 0, count: 9)
for (age, count) in population.enumerated() {
if age == 0 {
result[6] += count
result[8] += count
} else {
result[age - 1] += count
}
}
return result
}
mutating func run() {
for age in input {
population[age] += 1
}
for day in 0..<256 {
population = step()
}
print(population.reduce(0) { $0 + $1 })
}
static func main() {
var x = Self()
x.run()
}
}

267
2021/day8part2.swift Normal file
View file

@ -0,0 +1,267 @@
@main
struct Day8Part2 {
mutating func run() {
var sum = 0
for line in input.split(separator: "\n") {
let parts = line.split(separator: " ")
.filter { $0 != "|" }
.map { Set($0) }
let mapping = self.findMapping(parts[0..<10])
sum += self.decode(parts[10...], with: mapping)
}
print(sum)
}
func findMapping(_ input: ArraySlice<Set<Character>>) -> [String: Int] {
var result: [String: Int] = [:]
var input = input
let one = input.removeFirst { $0.count == 2 }!
let seven = input.removeFirst { $0.count == 3 }!
let four = input.removeFirst { $0.count == 4 }!
let three = input.removeFirst { $0.count == 5 && $0.isSuperset(of: one) }!
let nine = input.removeFirst { $0.count == 6 && $0.isSuperset(of: three) }!
let five = input.removeFirst { $0.count == 5 && $0.subtracting(nine).isEmpty }!
let two = input.removeFirst { $0.count == 5 }!
let zero = input.removeFirst { $0.count == 6 && $0.subtracting(one).count == 4 }!
let six = input.removeFirst { $0.count == 6 }!
result[String(zero.sorted())] = 0
result[String(one.sorted())] = 1
result[String(two.sorted())] = 2
result[String(three.sorted())] = 3
result[String(four.sorted())] = 4
result[String(five.sorted())] = 5
result[String(six.sorted())] = 6
result[String(seven.sorted())] = 7
result["abcdefg"] = 8
result[String(nine.sorted())] = 9
return result
}
func decode(_ input: ArraySlice<Set<Character>>, with mapping: [String: Int]) -> Int {
var result = 0
for digit in input {
result = result * 10 + mapping[String(digit.sorted())]!
}
return result
}
static func main() {
var result = Self()
result.run()
}
let input = """
fgcae ebafc cabdef eg abecfg abgfed feg gafdc bceg ebgcadf | defagbc faecg cfdag gecb
eagd cad fgadbc aefdcg dcebfg fcegd cbeaf ad dbgfeca defca | cfdeg gdcabf fcgde afgced
gfdeca aeb eb fbdag eafdc adfbe cefdab bdaegcf efbc ecadgb | eb dbafe eab faecd
facbdge efdg gcafd daegc caegb aecfdb ade ed gafbdc dgfeac | de abcdef faedcg dfgca
dcaegbf bc dcgeab cbgad cebg cdfaeb fbadg acdge cefdga cdb | gdcfae gbec gdace cgadfe
ceagfbd fbaeg fadge fbegad abgecf dafcg ed edab ged dfcbeg | gebfa de gbafec ed
acedbf adfbg cgafedb geac cgb befcgd bcgaf bfgace cg bfeac | bcdfeg fecba cafgeb bgdfa
egadc cea ac agbcedf cgab bgcade fbcaed eadfbg fegdc ebdag | fdecabg fcged cdbage efgdab
cbfge gdc cdea efdag dc cdfeg gfbcad gcdeaf cdeagbf egfadb | dfcge egcfd fcdbag dc
gaedb fbced gcdbef cge gc decgb dcbgfea cgfb cgdfea fbcade | fdegbac fecbd ecg degba
geaf bcfedg agbcd bfeagd dgfbe cbdaef beacdfg abe agebd ea | fbecdg geaf gebfad adgbc
bed afgebd adecf bafgedc dbca cfegb becdf fabdce bd gcfdea | gfdaeb ebfgad dbefc cbda
gacfebd ag bedfca agbfde egfcab dfga degab fbade bag bdecg | dgaf abcfed agb bfecag
afebg afdeg gcbe acebdf cfbage cafeb gab decgabf bg gcfdba | gb caebf abgef acfebd
efdab fabdgce gcbfa eg gacbfe beg ecag beafg dfabcg bgecfd | bge fgdcbe gcfdba ebdaf
aecbd bdagef gd bfega aefgcbd gfde gfdcab dbg edgba febagc | dg dagfbc dgfe eabdgf
gdfba egfacbd gdcefa baf gcba fdagcb dgfac ab dcfeba ebgdf | dcfag bfa gafcd cbag
afbe bdagc cedfb defcab ebfdgac gfedbc cbfad af adgcfe adf | dafbc dfbac fdcegb gadcb
fcgedb ca fdcage cagdbef ceab acedfb fdbce cafdb cda afgbd | ac dcafb efgdac ebcfda
fbad gbcda dcefgb cfeadg dgbcaf bcafdeg ad gcabe adc dfbcg | gbcdef bcfdg cgbfd bfadgc
afcgeb bedag bdecafg agb bedfga fdagce bg bgfd dfgea cdabe | badge cagdfe efdag afegbd
gfed dcaef ge bafdce daceg acebfg cefadg egc ecdabfg gacdb | bcgad bcegaf agecd degcafb
deacb adcge ab acb fbdce faedbgc fgadec gabd deabgc ecbfag | dfbec ab cbagfe bcfgae
gcabfde ecf cbdeg bacgf fedg ecfgb gfedcb fcaedb cebdag fe | dbcegf bcgdfe gfde defg
daefb bfg gedafb fabge dbafec cfega bdga cgbdfe gb gbacedf | aecfg bcedaf begdcf dgba
bagce bf bdacfeg efbg fcb cefab gbfadc cafebg ecdfa bacdge | fegb cegba bagce efbg
becag cfeg cefgbad fbace gfeacb acdegb bef ef adbcf fdabge | ef bdfac cbegad gbeac
egb afcbe fbaeg eabgfdc gcab gb gfdea fgbecd bcdafe gafbec | fcdgeb edcgbfa gbe fgeba
dga caebd ecdag cegfd abdfce dafbge ecbgda bcdgefa ga agbc | afdbec ecfdab gdaec gad
fc gbdefc cfe ebdaf fcbeda gbafde edacg bfcdgae dacef cabf | decgfba cdbefg cf ecdabf
febgd dafgbc caed cgd ceadgf fbecga dcgfe aegfc gbedfca cd | dgfeb fgced dcafebg agdfce
ecdafgb bgade bgd fabd gbaef bd egadbf ceadg cfbgea dgbfec | edagc bdage bfda dbgae
cgeba aefgcd dfceagb dc ced facbed cdfb dbaec adfeb fgedba | fbdea bgcae efbda fcdeag
caegbf dcfga afcedgb afgdcb fcedag ab edbcg dbfa gdcab abg | bag cbdag agb ab
gadfecb befc fgdbea cf fedagc fac bcgfa agbfe adgcb feagcb | gbcda cgbad bcgfa cf
dae aefcb dgabec dacfg afebdc ebfd afecgb aedcf fbcagde ed | ecabfgd bedf bcadef egdacfb
abcf fcega fdgce fegbda eafdcgb ac gabecd gbfae beagfc eca | edbgca edbfga cae gface
bgedcf afcdbg ag gcbaef fcedgab afgdb cfgbd befda acdg agf | fdbga ceadbfg egfbac fag
bad edafc bdeg abcgde ecafgbd gfadcb bgaec db edcba geafcb | cdafe fdcabg cdfageb dbge
geb bgcf agfcde gb befag gfbcae defab gabdec eacfg gabecfd | afbedgc gceabf abgef egacf
fdeac acg cfegda cbeadf fegdbca ag acefg adge bfdgca gbcfe | gecdaf efgca cgbafd cfgeb
begdf bdfgc ebcf adfcg cdegbf dgeafb cbg cb bfeagdc gbdcea | acgedb fagdbe bgfde dfgac
afcgdb aedc bedgf egafcd afdge gecaf fcgebda gda da ceabfg | gedbf gacfe adg agdef
bdeafg acgebdf dgcfa fa cafe dgfbc gaf edagfc cgabde agedc | fadbge bdgfcea dgcefa cgeda
gacfbde defag cbgef gbca efadcb afcbeg cgbdfe ba eba gefab | fegab abe ebfcgd fbgae
gfbdec fbcade cabdegf ebdg bfd dcfgb cadgf gebcf db fcabge | eacdgfb bdge ebdg ecafdb
egcfba fdcae fcdaeb gcfdae fabd ba cdeab fcdbaeg eba decbg | cabed facbed cbade dfcea
agdfe acfdeg fcdbeg dgf gdaec df cadf ceagbfd abgfe gbdaec | bgcdef abfge gbefcd df
dgebf afgdb bfa fcbade af dcafgb agcf gcbda dabgecf gadecb | dbcga abcdge fadgbce eagcbd
dbcga bea egacdf dageb be efbd abgfce gadfe fcbegad aebfdg | eba be bdage bedga
gcbd egbdafc fgc fbgedc gc aedbfg cefgb fedbg cfaegd efabc | bface egfbc bgdacfe gc
fdgae dc ced gbcd gbcefd decfba becfg ecfabg cgfde gbcadfe | edbcfg cd cafgbe cdegbf
geadcf efg gadef fg fecad acbgfe dagecbf dcgf gadbe afbdce | edafcg bgcfade gfdc abedg
edcba ecgdb aedbcf aebf dabfc fdacge badgfc ae ead gfcbade | gcebdaf degbc ebfadc efdcba
caedbg dbgec dcbf ecgdbf cgdef dgf bgaedcf egacf afebgd fd | cdfbeg ecfbgda gcbfed cbgedf
bdag acdeb ecgdb egbcfa fcdbega gb dbecag cbdafe ebg gdcef | bedgc abdfcge efagcb cdgef
bfade bfdga dfbgec cagbfd dfgacbe gba gacf ag gdcbf abgedc | cfag abdcge ebafd abefd
agcdef bcfda bdgca aebdfcg eagdc gb dbg gbea cedagb bfgdec | dfabgec eafdgc acdge gdecab
ebacdfg cgbfad egdbc bdgfa daef bae gabed febadg ae gacbef | fagdb daebgf fgdab bea
dcgbf cbedf degc bgc fcgdbea fbadg cfdgbe gc efacgb fdbace | edfcb cfeabg cg edbacf
bcdagfe dfacb cbfdge bgcdf fdgeac acd agbd acbef da dcgfba | cbgdf dgab ad gbdcaf
edgfbca gbe be ceba bdaecg gefcd dbagc egbadf dcegb dfgabc | gcfde gfceabd gfbcad eagfdb
dfcbg fcdga efgb gecbfd gecbda dfbgaec bedfc dgb bfadce bg | dbcgf bfge dacgf dfgcbe
cga ebdgaf bfdag cbgfe bfadgc gbfeadc ac cagfb cdba cgadef | bfdag agedcf aegcbfd gcfab
gcead gfdbec cbdge gabfce eb dfgcba ecfdgab befd dgcbf ebg | cdgae fbdegc bcdge gcdfb
da ebgfda fcdeagb acgbfe gedba dbegc agdecf dae bafeg bfda | gdafec fegba edfbga aed
fbagec gdfba bcg gebcfd cbafg bcdfgea ebac cefag faedcg bc | gfbac baegcfd fabegc dgeacbf
dfbeag fabdg fdbe abdeg eb dcega gcefba cfdbag dgcfbae geb | cgbafd bdef dcage eb
abdcfe gba dagf cagbefd fdbea bfega ag dafbge dcbeag gfceb | afbcde ecgfb dfbega bdgeca
dg dge gdbc cfdae bcgae cbdage abgfde cfedagb cdgae eacgbf | eabgc bgeac bcdg cagbe
bfd dgaeb gcabde fd bfgce dgfa dbefg gadbfce dbafge fabcde | edfcba efbcg df bdf
gcbfd agbecd fedc edcbfg fdb eadfbg bcfag fd gedcb gbacdef | fgbac bdagfec gfbdaec aegbdc
cgbdfe ce acedg adcfeg bcfegad fcea ceg abdeg afbdcg fcgda | ecg aefdcg dacge ec
egbdfca efacb agcdbe cbaed bdafgc dfaecg debg bd bda agced | geadbc efbca gabfdc bd
edcfb bfgecd ecabfd eca abcd fgeab fedcag abfedgc ac bfeca | bgfae eca cdgfeb bfaec
cadef cebdga ebd bacdfe dagfec dbfa fcebg ecbgfad db fdecb | befcd acgdeb abdf dbfa
gbfecda bfd cdagb fbdacg fgdbec gbaf cbadf aefdc eadcgb bf | adcgb dfbca agbf dafcb
abedg acbgf fd gaefcb cagbdef cdaf dcbfeg bdagf dgcbfa bfd | gbadcef df fadgb gabcdf
fbgadc dbcfg dcega cgdfe cfedbg caefgb ef gef gfaebcd fedb | fcedg febd gbcdfa cdgfab
bg adgb fcaedbg cgfda fcbae bcg dfacge fgcdba decgfb bcagf | cgafb dbfcage bdgcfe dbga
ed afbdg acefb aecbfg cfdabeg gedfcb fcdabe ecda def ebdfa | gbafd gedfcb dfegcb dfgba
gcdfae efgdb abdfcg fde gdabf dbcegfa fedgab efab ef ebdcg | eafb gbfda gacedf cbdge
edagfc gfaecbd bgced cbegfd cb gefdc badgfc bdc efbc adegb | dgbec cegafd dcbgef dgecb
gedcab beg eg cafgb fbegc gdfe bdgfec gabcedf bdafce bcdfe | gcefb afcbed ge gdfe
fdbagc cdgefab edacf ceba ea cfbaed ecfdg abfegd adfbc eda | ea beadfc abcgdef ecdgf
fbc gfbacd fecda cbdfa dbgf dceabg gbcdfea efbgac cdabg bf | gbdf agbced gaedbc gfdb
aegcb bfgecd fbegad fcagdbe dfgce fcbd gdeacf fb bgf bgfce | bgafedc gcdaef dgfcbe eadgcf
def dbce agdfeb ed fdgbcea dgcfb aegfc dfgbac gecfd cdfgbe | cdfegb ed cdgfaeb ebdc
ecbfad afdb eagbfc caebgdf edfcb dgfec cgdbae bd bcafe bdc | bfacegd fbdcae dbc bgfdcea
dfgba eg cfbead gde bfedgc dceab cabfdge gdbae aecg aedgcb | egd adebg gde debcfa
cadfb egfb afgbc adbegc ecgadf ecagb fga ecfgab bgacfde fg | afcbg cgfba gf dacbge
cegbf dec efcdg facdge dfae dcbafge ed adgfc fdbagc cgaebd | dce gbcfe afcdgbe dcgabf
cfabe fa eabgfc cgaf fba gfbce dcfbge edbgcfa efbadg acebd | cfebga bfa fedcagb abfce
acdfb dbagf afcebgd beag afgedb gfdea gbd fgdcbe gb cdeagf | dafge gafed gfacde gb
gdfeac dfgce ebfadcg edagc dacf baegdc fd gdf egcfb dabgef | degfbac cfdeg daebgf cdeagb
fcgabe ebcd ecfdg dabcgf gfecabd cfd ebcfg dc bcfegd adefg | agedf cd bcefg dc
cbaeg bcfea fb feb gfaceb gdfbec dagbec gfab deacf bgcaedf | acbfge bf afgb efb
egdba cadgfbe efadbg cg begc bfgdca caegd abcegd dcg acdfe | fbegcad bceg gcd cdgebaf
agbcd dafcgb cfdg ceadb fdgeba eafbcg gfdab gbc bgdcafe cg | gfadcbe fbagd decba gc
gfbec adgc dge egdcfa abgfde abgdfce dg eacfdb fdaec gdfce | fecgd gd fgcbe acdef
fdbceg bfedc af fegcad cbdag daf efab dabcef fbeadgc cbfad | fcabd cbfad gcfdbe gadbc
ebgac fabdeg bedca cg aegdcfb geacbf ceg efgba facg fbedcg | gecdfb aebfg fagc ceg
eagb dcgeb ba abd cbeagd cdgafb fecbdga fgedcb dacfe dacbe | bdfeacg cadgbf bgedfc fcdae
dbac cfgae cbfgde ba bga gcfbad dcfebga gcdbf feagbd cgbaf | cgafb fagbdc dcfbg afgebd
adbgfe fgb egdafc cbafd dfegbc edgafbc egfcd cegb gfcdb gb | bg fdacge efcadg egdfba
agfbe cgaefdb gbdf edagb afg cdgafe gfedab fg egcdba fabec | fcbdega fag agf agf
cae edabfc cdfega ca cfba gadeb fdecb ecagfbd cbgdef acdeb | facdeb fgecad bfadce efcgbad
facb dbfec dceabf efgcd cb aecfbgd dgfbae eafdb ebdagc bcd | bfedc cdegab cbdfe adefb
fecbga badgc dga bfgadec dg geacb cgedba dfebag gcde fcabd | decg gda baceg gd
gecbd gcbdfea cbfg bg bge egbcdf dcbef beafdc cdeag bfgeda | agcde dceag gfabde beg
gefba gacfbe facdeg cg cgab gfc efbdcag cfgeb cefbd afegbd | gfc afbge dafegb aebcgf
egd aebfgd gefc ge gedbfca bdcfe dfcegb gecdb edbacf cdbag | dacbg edcfbg bcdfe ge
df ebdga gcdefa cfbd bgdfa afgcb dfeagbc bfacgd fgd facebg | gaebd gecbaf fdcb dfbag
fag cegfdab cadebg dabcgf abefg eafc ecafgb ebacg fbdge fa | ebfag dbcgfae gbecad fdbgac
gecbd gefa gfc fabdcg bfaecd ecgafbd fg befgc efabc gecbfa | gfae gcf egbdafc edgbc
bfgae fcebag fdbe fd fedag adgfbc fdbgea ecagd agcbfed fad | fgabe dfeb ebfd gefba
ecdgfb afge bdage ebcda eg abdgef fgadb gabcfd dge bcdagef | gdebfa eg agfbdec afeg
dbc acefd cegadbf fbcde afcb fdcaeg bc acdbge febdac debfg | ceafd caefd gfbaced dcb
fecgb fgacd gdcefba de gbafce febd gecfd edc fecdbg ecbgda | becdfg ed becgad gcdeba
agfdbe fbac bcd edacfb bgecfd cb cdeba cfgdeab acged edfab | cb dbc adbce bdfae
dgacef bdfa ecgbf ab fegba dgafe efdbgca abg acebdg afbdge | gefda ecgafd ecfbg begcad
gbfcea bgfa afdebc aegdcb af bgcdfea bgeac gecdf afc fgace | bfgecad fca afc abfg
cfe dgcfe fdagcb gebdc egfa gbcdefa dcebfa fe agdfc fgaedc | dfgac fe fce fcdbega
fecgab gcbdef cbaefgd dfgbe dgcfe eb bfe dbfga edbc acfegd | fbdeg fdgeca agefbc egcfd
aecd bac dfacbe cafdb ac gefcab gfcdbe fcbde gbdaf cbdegaf | gcdbefa fbgcae ecad fagecb
eagbf cagedf geafdbc fdgbce adefb aecfd adcb deb faecbd bd | ebd deabf fadcge cdagfe
gadefcb bfcga eadcgb egdbfc deag ecg edabc acedbf eg abgce | gdea bagce gce gce
agcfe bec gecab gbeda facb dbfgec fcegab gfacde gfdaceb cb | cb cefdgb cadefgb bc
fdgb df egabcf fegda cedga fegab efdbac def cgdefba gafedb | facdbe bafceg agedf fdebga
fdaecg dbcafg abfgec dc caefg cedg aecfd abdgfce eadbf fcd | cgefab gbfacd fbegac fabecg
dfecabg bcfeag gdafce dagbfc cdefb dca gafbc dbag da bcadf | dca bagfce cbfda cdagbf
dabfgec febadg gbcdae gfbe adebf ef cabdf edfgca ebgda eaf | gfacbed abgced egdfca egfb
cfdea dc fdagebc fegcbd agdc gcfade fcabe cfd efdgba gfead | bdfcge agdc dgefa cfd
gbfca febadcg bc bdegcf beca begaf gabedf cgfbae cgfad cbg | bcg dcaefgb dfgceb afebdg
cg cgbf bedcg fcagde adefbcg efdgcb ebacd cgd edbfg egdafb | cfgedb bgcf acegdf cfgb
bfad adcgbe faceg fagbdc df afdgc efcbdag dfbegc gfd badcg | gfd abgcd fgd abfd
bcadgf cgdfb adgcb gf gafc dcfeabg bdagce dgf dfbega cebdf | adcbfg gfca fdcbe gfac
daebf fcabgde bgdcef gab ga edbcga afcg ebfga aefbgc egbcf | ebcfg ebcgfa fcgebd agcf
gedab fg afgecb cfgd feg cfabed befdc fgdcbe gdceafb befgd | ecafbg ecfbd febdc gf
gcbadf ce gfcdb fec feabd gbecdf dbcfage febdc agcdfe egbc | bafed gcbe gefcbd egcb
cbdfg gbcafd feb gaecf gcbfe gfbdae be fcgabde bdgcef becd | abgedf gdfebc aedgfb fbe
edacgbf fbdae edgac bg gab bagdec dagfce fadbgc cebg bgdea | aedgb bg cebg adcge
bda gbfea gedcab gfbceda bgdfa bfcdg febcdg bagcfd cfad da | dbecag bgfdc gdbfa facd
ceabd dabfe acgde bc fbca cgbfde febgda dfecagb ebafdc bdc | cbdae dbfea fgdceb gbfedc
acg fadbgce cfgdeb dbgce efdca ag dbga cagbef egacd cdgbea | gdecabf gcbfea ecbdg gac
dfgacb ged egfdab efcgbda gcefbd debga ebadc ge aegf gfdab | cdfgab cdafgb bfcgda ge
gdbeca bcfde eafg cefbgad dgafcb ge degfb adefbg dgfba bge | bdfeg begdf ebfadg eabdfgc
cfbdgae gbaec bagfcd gfcba dcaeg bacfeg dbeacf bec efgb eb | ceb egcab cagfb dgcae
fdcagb gdaecb fcadg bcdgaef gd dfcea gbcaf aecgfb cgd dgbf | gfadc gfdb badgfc gbfca
bcafed defgcab fgcb cfbae cagde agebfc gbcae gbe gadefb gb | bg gfcaeb bg aedgc
bcfadeg gbfd eacfbd gdafe afebg bafegd geafcd gebca fb bfa | agdefc eafbdg fgead gedaf
fcgeabd dgeaf ebgfad gceafb geadc ac aec ebcdg cafd adgefc | gbfadec egcbd acfd dgabef
eac fbdgec ea eadb cbaeg agfdecb eafdgc dcgbe bcfga adcbeg | aec abde eac ea
cedb gcadbe ed edgfbac fbega abdge gacdb fdacgb dcefag aed | cdagb gbcafd gdefac gedafc
cae agfbec ae begcd feab fadgbc ecfadg ecabg cdgbefa cabfg | ecgba afcgb efba dgeafc
fecgabd badf efadg abedfg fea fdgceb fedgb aebfgc af gdcae | ebfgca fae fea af
bgedac gbfae dcbefg acbf fb gaedf cagedbf aebgc gebfca fgb | aedgf fdgea bgfae bfdgeac
bagcfe eadcb gb facegdb bgc fdcag efbcad beadgc bgadc ebgd | gbc fbcage becad gbfcae
cagde gbca dcgbea gc efcad dbgfea dfgecb caedbfg adgbe cdg | gc egdfbc bcga cg
efgcbd aebgcd dfe eadfb abged abdcefg bacfd ef bdfaeg faeg | fgae cgdaeb adefb afcbd
cgbfea gcbda gefdcb eadf febag bdf fd febgad fadbegc bgadf | gebcfd fgdbea dfb dbf
dg cdgfe gecfad dfbce bgefda fgd eafcg efcbag dgac bcdagfe | afdebg gfd aecgf gfcea
edfga cgefda gcdbe fbae ab egdabf cfgbda deabg bad adefcgb | ebadg agdeb fbae gbdae
gbdcfa gbade ebgac ecbfa adfgbec badecf bgc gcef fbgace gc | gc fcge gacefdb ebgca
acefgd ebgaf egfda egcd bgfaecd dgcfa ed abcefd efd abdgcf | def ebgaf fed befcda
fecag cag cfagbd agfcbde fabec eabg fdcge cgbfea fadebc ag | cfgbda dfgbeca efdcg dfgcaeb
bgecfda ecdag dgfcbe bdgac adbgf afgdbe bfac gdcabf cbd bc | bc acbgefd fagcdb fcba
ba efacg cfbgde cfdbg bacgf bgfdea abf bcad cfbeadg bgcfad | bcdgef fdgaeb ab cagfe
cbdaf bafdgc eacbd fd dcf fcabg bcdfeg bedgcfa dgfa agbecf | dfga bgfac df agdf
edcabf cadbf bgfdcea bgcfad bgcde dfg fg bdfgc cgadfe abfg | adfecg gfbcd acgdfeb cbgdaf
badgfc dg bagcde bceda dbcfea beafg debag adg fcbadeg edgc | gebda dcagbf dceba egfab
edcf dacfbg dbcefag fbd df bdegfc ebdagc bgfea begcd bfedg | dgebc cbgde dgfbac bdf
bdcfg acegfb acebfd fdcag egadcf da daf cgeaf edag bafcegd | gdcaf fgaec bafdec fdaecb
cagefd fde gdfba fcgaedb abefd ef dafbeg ebfg afcbgd ebdca | egdbfa bceda dfabg bdace
fcgb gcdae bg gfadeb cbfegd fcabed bdg gcdbe fdcbe adbefcg | dgb gfbdec befdc gfbc
fabde bgfde bgecda cedgb gf cabfdeg ebfdgc fgbeac fcdg gfb | fgbde efdgb edgbc afedb
ebacf cedfab fde eabfd fd baecgf bagde agdebfc dfcb egfadc | ebfdgac acdegf abdefc ebfad
gdcb cd cfgae cgbedf fbegd efgdc fadcbe gfeacdb dce dfagbe | gbcd cde gacfe gefdbc
geb afedgb bfegca bgade eg dgef gadbc eafdb afebdcg fcbead | cagbd defcba dbafec geafcb
fgbdc gbfade gba gefa dbafg cbadge bfgecda ga edbafc dfabe | afbdeg dgfba bgdfa dcfgb
agef badce ef febad cabfdg gdfab cbgeafd egfbdc defgba fbe | abefd geaf gafdbe ebcad
gd gad bfdg gecba fedabcg fcgeda aedfbc adebf agbdfe dgeab | ebcdaf agd gcfbdae egacb
cedbfa ba cgbfa abf dfegac cafeg gabe befgca ebdfcag cbfdg | fab aegcf faecbd fdcebga
daefg febdga aegfdbc efgdca eafb bge eb agbdec febgd gbdfc | gbcfd edbfg egb bacegd
fbdecg bdafec cfaeb gedac def dceaf abfd fd gfecba gfdaecb | ebfac bdfa gbcdef fd
fcbga cbdfeag bafgce fdacg dc fcd fcgbad fadge efcdgb cdab | gaefcb bafegcd acdb abcgf
ecgabd acgdf abge ae bcfead fgdaceb ead daegc bcdefg gcbed | egba gaecd cgadbe gdbce
aegfbc gf afebdc agebdfc ebcdf feg dcefg bdfg dcgea bgedcf | edbfcg fdgb bdgafce febgcd
dcg bcdefa dbfceg fdeacg acbeg bfeagcd cfdae dg dfag agdec | dfcgea dg egdca edagc
agcfeb fagbed fdbceg bag gebfd afgd agbfcde acbed ag badeg | cbdea fcedgb gfebac abcegf
adbegfc fgdace cadge afcg ebdfca ebgad eac bfgdce cgefd ca | acged fcedg ebagd dgacefb
gfeadb gfeab bcfa agebcfd aebgfc fecgbd efc fc gadec egcfa | fgbace fc debgcf afgceb
bacg ab cfgbd bfadge acfed fgdecb agedcbf fcabgd bfa dbafc | gfdcb cbga abcg afecbdg
ge beagc cfgabd bgaced bgcda fbadgec age ecdagf egbd fcbae | aeg gedbac cadfeg fbgcda
dgbafe abfdgec gdf fd agebcf gadec dfcgba befd edfga aebfg | abcgefd dagec dfg df
edfcga bcegd baeg cbgdae agdbc gce acgdebf eg gfadbc cefbd | dcbeg bgedc cedbg edcbg
adfbe dg cagef dfgb gdbeca aefdg gda edcbaf fdgeba gdecbfa | cdabef eafbd bafcegd dg
cd edcbfg dbage fcaebdg facd bgcfea afbedc afbec dbc adecb | adegb bcefag abegd cbgedf
gdcbf da ecgbfda adc adgb bagcfd dfbcea egfac dfbceg gdacf | gdba cfdbg adbfec fcaeg
bcg bc gdcbae dbca abfgde gdeba ebfacdg egcba faceg dgfbce | geadb badecg ecbfgd baecg
"""
}

186
2021/day9.swift Normal file
View file

@ -0,0 +1,186 @@
@main
struct Day9: Puzzle {
let matrix = Self.input.compactMap { $0.wholeNumberValue }
let width = 100
let height = 100
subscript(x: Int, y: Int) -> Int {
get {
return matrix[x + width * y]
}
}
func isMinimum(x: Int, y: Int) -> Bool {
let value = self[x, y]
let smallerLeft = x == 0 || self[x - 1, y] > value
let smallerRight = x == width - 1 || self[x + 1, y] > value
let smallerTop = y == 0 || self[x, y - 1] > value
let smallerBottom = y == height - 1 || self[x, y + 1] > value
return smallerLeft && smallerRight && smallerTop && smallerBottom
}
func run() {
var lowPoints: [(Int, Int)] = []
var riskSum = 0
for y in 0..<height {
for x in 0..<width {
if isMinimum(x: x, y: y) {
riskSum += self[x,y] + 1
lowPoints.append((x,y))
}
}
}
print("Part 1:", riskSum)
let basins = lowPoints.map { basinSize(x: $0.0, y: $0.1) }
.sorted(by: { $0 > $1 })
.prefix(3)
.reduce(1, *)
print("Part 2:", basins)
}
func basinSize(x: Int, y: Int) -> Int {
basinAt(Point(x: x, y: y)).count
}
subscript(point: Point) -> Int {
self[point.x, point.y]
}
func basinAt(_ p: Point) -> Set<Point> {
var set: Set<Point> = []
findBasin(at: p, value: self[p] - 1, visited: &set)
return set
}
func findBasin(at point: Point, value: Int, visited: inout Set<Point>) {
guard !visited.contains(point) else { return }
guard point.x >= 0 && point.y >= 0 && point.x < width && point.y < height else { return }
let currentValue = self[point]
guard currentValue != 9 && currentValue > value else { return }
visited.insert(point)
findBasin(at: point.left, value: currentValue, visited: &visited)
findBasin(at: point.up, value: currentValue, visited: &visited)
findBasin(at: point.right, value: currentValue, visited: &visited)
findBasin(at: point.down, value: currentValue, visited: &visited)
}
struct Point: Hashable {
var x: Int
var y: Int
var left: Point { .init(x: x - 1, y: y) }
var right: Point { .init(x: x + 1, y: y) }
var up: Point { .init(x: x, y: y - 1) }
var down: Point { .init(x: x, y: y + 1) }
}
static let input = """
5796798621237995498765434567987542999765679987545679109878999877899789876532123456998999876887899921
4645976434456789349654321298997679898654698987635678998767897656789698765432012347897899865676798799
3234987545978993298795410989998989789543256897646789498756789546896579877842123456976789954345985678
4356798679989999019987329878999798679765345689856991296545890134789467998956899967895698643239874579
6467999789999898934976598967987676568996457999967999987636789345691346789769987898934987651098763456
7598997999987796899989987959876543456789569898998998765125678956910298899898776799123998862987652345
8789876789865685578999876645987665567998689656789876554034899999891989998987564679099789879876543456
9898765498974324456799865534598786788998798645889985432126954987789878987654323567988698989987664678
9989997987543212367987654323459897899549895434569876556437899876599967996543212459876587898998798789
9877989997662101456798765434567998967932987624778989787548999989679459987687301349875456967899899893
8765678998983232367899896765788939346891295434999999899659879699894349899796532656954344756910923964
9874789019894343456789949878999321235799987545678945998778964578942139769898543769843212345891949765
7965678998765764567893433989876542346987898656789236799889653989943298652999678989754523456789898976
6598789109976975698921012398999763457896559867997647989998799899874569541098799798765676579896687897
5439898929989989789962123457899894568989435998998799878999989798765798432149898649987787899965456989
5212987898999999899854336568999989879979326569109989567898879689878987544234995434599898999874345679
4309875987999876998765687679989878998765412458929875467987854599989698955679989323459979899993214589
5996954676799984329877788989878969899874324567899765359876743489996549877789878912398767789984323456
9875432545789743212989899998967659789985595678998868249965312679865435998898767899598954698765467567
2994321036897654101996936987654543679999989789997854198764301569979423459999954678997643789978578978
0987432128998763219875424698763212478998978999876543298773212458998901267898767889598732345988789989
9876544346789979329876512349998401569897569234987994987654343457897892478999878993349891234899998695
9989875679899898939984701459886212398786456946799889998795464567896789989896999321234989345678987544
9997987889998797998743212598765323987654397899989778999989978679945698999765678930349878956999898433
8986798999987676789655329679876439876541289998878656899867898791239956799954345959499867897899765321
7845679219878565678976598999987556998732378997667545798656789892398745999765237898987656789998654310
6434798998765454567897987898798677899543467896553234987545878989499636878992156987975345899219964324
0123987999874343456789876799549789987654598998421056989434767879976521767989249876543234998909878434
4339876798955102367898985678939891099967899876542159875323854568965410156978956997684456797899989545
5498765986543234488987894799012999129878999987653249986210123467894321249769767898795867956789199656
6599754397654545589996789899993998949999998799767998975351237998995432398758998989986788946999098789
7988732198765657678975698999879876898946999544979877989876456789987643459647899876097899534878999899
9876546019878767889984567899968765667939896532989966592987887893298754598756789965198997323456799989
0998757898989899992099789988754543457898789540199854301298998984129866789998999954249986412345899878
1299898987699954954299999876543212348987698921598763212349999873234977995679439765356897601234988767
2989999876569999895989986987654103467896567892349854334568989964345698934678921976897996532349875756
9879899988698988789878995498976215989995456789498999965689879899456789323567890197998987656756994345
8765677899987677697667896329865423499989997995987878897798768778968895438678991298969398967899873201
8654546789766563459548994219876534578977789104986567789949654567899976657899789349543219988932965412
8643437899854312998435689101987645689665679323975345678959868778957987798934679959654301299549876723
6532126778969409876424578912498756796553568939863203456899979989646799899012567898976432358956987894
8544434567998912998535689843569867989432459949954212568999989594535798989123467987976563567969799985
9655566789897893479697897654567979879421267898767343467898795443123497679935679876899674789997659876
9776789998656799567989998765678998967992349929876556578987654321012989567896798765678985999876642989
9988899875434678979878999879889987657889458919997967689798865452199867456987987654567896798765431096
8799943989545789999868799989999876545678967898789898797659979874987654347899999753459987979876532145
7679969998656789987657678994323965432789979987679789896535989995998321234568939894567899764987687236
7567898959767893976543467995909876645678989996545678965423599989899754359679421986789968973199875345
3467987644978912987784989879899987856789999987434569653213679876789866468789439898993459792012989466
6569999533989999798896797867789898987897999876424678962102398765898977578996598769322375679933496578
8678998921296789679987896545698769399986789764312398993923499654567897689398999954201234567894987689
9799867890145679542998987896789954234995698765101256789894987543458999791249899895312347899995798789
2988656891237789869879998998996895999876789864212345996789987652377899910299756789436556799989999890
3977545789347899998767899989434999879989898987423467895678993210466789321987645679987867989767899921
9865437896556789999946799876545998767999987976534789954589997621245679459876437898798979876545978943
9976545789697899987834689989699767656899876989675678932679876432556789598943218997659989765434567894
9989867899989999986545678995987654545998965498797889321996997543457898797654323789943596986546789965
8896978999879878997668789213986543234987894349898996549875698665678909998965534567892345697656798987
7645989898765655689779898901987662129876789234989998698954539987899919879877645689931257898767967899
9869998789964334578995937892397654398945689345678979987643012398967898767998776798890234999898957898
6998789679892123457894325943498765987834568956889467898952134579656899656549887897789656799999546457
5987654597651012349975434799579989865423567897992378999763245678947999743435998996549767987989432347
4398753299843123457896545678992099654512455789209989398754356789439879842124999987638989876678954456
1239854987653238768998956999989298743101234678998994298765768899598765431029892196547898965466795567
0123995799768545678969869878979349543213455789997899109976779998789987532139789987656987654345789979
3235986999879656789756999867668956975434696899986987912987899999894697643298678999987898543234567898
9945799876998798897645987654456899876595989989765976899998999886989987659987567998998999864365788977
8799895995439899986534598732367968988989878979954695678919998785468998798766456997899898765479899766
5678923994323999876545987543478957999976556767893254589101987674378789899854349876799649876678987645
4567919876214899987689998954989546898765432356789123678919986543245679998765467965678999987789298756
3467898765436789998998999879995434999896521237895019789998997532135998899977569876789988798992109867
2369929876545699999987899999876545689943210235994298999997989949239876789989778989899976569993298989
3458910997676789899876989212987676899656432346789987889886979898949965679999899998998665478989987897
4567899998787895798765678923598789998789545497899996779765765787898764567878921987999543289567976546
6878978999898934989894569654589890249899876569999884568954654656989843458967899876998992123458997997
7989569899919129878989678979678931239964998978998763477943212349879932123458998765876789012567919989
8991356779909019767678989989899842498643239989987542356799302345965431014568919954345692139898929879
9410124567898998654567891294998753679654134699876521245678913459876432323589109765457789256799598767
4321267898987549876899910123987654598763245698776410234569865567997943434578999988767899767895349654
6532356789876434987898943235698765679854659987654321345678976778999894565689989999878949878943298765
7656467893987545998967896545699887998767998998765432456889988999998789689789878989999432989752129976
8767589954899756899459987756989998919879897899876548668994599989987678999898769878997643496543234988
9879678975798967987598999899878959102998766999988767899123678978986567894987653967898765789759449999
2989789996987898998987899954969643213459945789799878943239989865454478923986542656949896899898998931
1299899989776999129976899769878965425678996999656989965398798754342349435965421248956997899987687899
0467999876545789098865689879989876566789987898768997897987659983210456949878543367899898999876546797
2378988965434678987674778998999998987893598999879876798965434975672569898987665456789789998695435896
4499877994323569876523467897988999999912459999989965689896323497883479767999787578995679876564326345
9987656789212489986313568966767897899106598989999984878789212598965998956799898989664868975432101234
8798878994323478965423459954456986798919987678999873165678943679879876545689939996543459876875212345
9659989765434568976796567893299765987898765569898762054567894578998765434696549987654667998994323476
8934599879876899997898879954987654496987654346789943123489965679539854323589998998767898989889456567
7895678998987899998999998769876543345798765767898894344567896789329875434567897689998999878778967678
6796989987698969799998789878998632134899878998946799465679987898919987645679976578999098767669898799
5789995799549347689987678999986721023999999769434878987989998967898998767989787459989298653456789910
4567894698432134567988566789875432335698798653212967999997859456987679878998643212578987832345679891
3779992976553235679977455698998765487987689864343458999876543237897569989797652101459876543456789789
9889689987664346798765323456789887569876598765454569986987652145789698795698768892368987956567895678
4994567899865457999974313345699998798765439879875678955698767234999987654229878765456799767878934589
2123456999876567899875101256789109999984321989989899543249878945678996542101989877667899878989123699
"""
}