Add 2015
This commit is contained in:
parent
c7deb3c71d
commit
ddf5ec2274
56 changed files with 6756 additions and 1 deletions
69
2015/Day9.playground/Contents.swift
Normal file
69
2015/Day9.playground/Contents.swift
Normal file
|
@ -0,0 +1,69 @@
|
|||
var input: [(from: String, to: String, distance: Int)] = [
|
||||
("Tristram", "AlphaCentauri", 34),
|
||||
("Tristram", "Snowdin", 100),
|
||||
("Tristram", "Tambi", 63),
|
||||
("Tristram", "Faerun", 108),
|
||||
("Tristram", "Norrath", 111),
|
||||
("Tristram", "Straylight", 89),
|
||||
("Tristram", "Arbre", 132),
|
||||
("AlphaCentauri", "Snowdin", 4),
|
||||
("AlphaCentauri", "Tambi", 79),
|
||||
("AlphaCentauri", "Faerun", 44),
|
||||
("AlphaCentauri", "Norrath", 147),
|
||||
("AlphaCentauri", "Straylight", 133),
|
||||
("AlphaCentauri", "Arbre", 74),
|
||||
("Snowdin", "Tambi", 105),
|
||||
("Snowdin", "Faerun", 95),
|
||||
("Snowdin", "Norrath", 48),
|
||||
("Snowdin", "Straylight", 88),
|
||||
("Snowdin", "Arbre", 7),
|
||||
("Tambi", "Faerun", 68),
|
||||
("Tambi", "Norrath", 134),
|
||||
("Tambi", "Straylight", 107),
|
||||
("Tambi", "Arbre", 40),
|
||||
("Faerun", "Norrath", 11),
|
||||
("Faerun", "Straylight", 66),
|
||||
("Faerun", "Arbre", 144),
|
||||
("Norrath", "Straylight", 115),
|
||||
("Norrath", "Arbre", 135),
|
||||
("Straylight", "Arbre", 127),
|
||||
]
|
||||
|
||||
var distances: [String: [String: Int]] = input.reduce(into: [:]) { result, input in
|
||||
let (from, to, distance) = input
|
||||
|
||||
result[from, default: [:]][to] = distance
|
||||
result[to, default: [:]][from] = distance
|
||||
}
|
||||
|
||||
func totalDistance(route: [String]) -> Int {
|
||||
var distance = 0
|
||||
var current = route[0]
|
||||
for place in route.dropFirst() {
|
||||
distance += distances[current]![place]!
|
||||
current = place
|
||||
}
|
||||
|
||||
return distance
|
||||
}
|
||||
|
||||
func combinations(_ s: Set<String>) -> [[String]] {
|
||||
if s.count <= 2 {
|
||||
return [Array(s)]
|
||||
}
|
||||
|
||||
return s.reduce([]) { result, first in
|
||||
let rest = s.subtracting([first])
|
||||
|
||||
return result + combinations(rest).map { sub in
|
||||
var result = [first]
|
||||
result.append(contentsOf: sub)
|
||||
return result
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
combinations(Set(distances.keys))
|
||||
.map { totalDistance(route: $0) }
|
||||
.max()
|
||||
|
4
2015/Day9.playground/contents.xcplayground
Normal file
4
2015/Day9.playground/contents.xcplayground
Normal file
|
@ -0,0 +1,4 @@
|
|||
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
|
||||
<playground version='5.0' target-platform='ios' buildActiveScheme='true' importAppTypes='true'>
|
||||
<timeline fileName='timeline.xctimeline'/>
|
||||
</playground>
|
7
2015/Day9.playground/playground.xcworkspace/contents.xcworkspacedata
generated
Normal file
7
2015/Day9.playground/playground.xcworkspace/contents.xcworkspacedata
generated
Normal file
|
@ -0,0 +1,7 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<Workspace
|
||||
version = "1.0">
|
||||
<FileRef
|
||||
location = "group:">
|
||||
</FileRef>
|
||||
</Workspace>
|
Loading…
Add table
Add a link
Reference in a new issue