Last active
December 15, 2016 17:10
-
-
Save DeepFriedTwinkie/c0b6ab38a60bc6b63cb2bfbc6589ab8b to your computer and use it in GitHub Desktop.
AdventOfCode.com 2016/Day 3 Solution (http://adventofcode.com/2016/day/3) (Part 1)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
struct Triangle { | |
let vertices: [Int] | |
init?(vertices:[Int]) { | |
guard vertices.count == 3 else { return nil } | |
guard vertices[0] + vertices[1] > vertices[2], | |
vertices[0] + vertices[2] > vertices[1], | |
vertices[2] + vertices[1] > vertices[0] else { | |
return nil | |
} | |
self.vertices = vertices | |
} | |
} | |
// Day 3: | |
let testInput = " 330 143 338" | |
let filePath = Bundle.main.path(forResource: "Day3", ofType: "txt") | |
let inputData = FileManager.default.contents(atPath: filePath!) | |
if let input = String(data: inputData!, encoding: .utf8) { | |
// Split each line into a string that might represent an array of vertices | |
let triangleStrings = input.components(separatedBy: "\n") | |
let triangles = triangleStrings.map { (vertexString) -> [Int] in // For each line in the input file... | |
return vertexString.components(separatedBy: CharacterSet(charactersIn: " ")) // Split the string into strings by spaces... | |
.flatMap({ return Int($0) }) // Convert those strings to Ints... | |
} | |
.flatMap({ return Triangle(vertices:$0) }) // Convert those Ints to Triangles | |
print("\(triangles.count)") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This one used a resource text file as input.