Skip to content

Instantly share code, notes, and snippets.

@z3bi
Created December 20, 2022 18:08
Show Gist options
  • Save z3bi/fd1e514802101e7b773d54998451ed81 to your computer and use it in GitHub Desktop.
Save z3bi/fd1e514802101e7b773d54998451ed81 to your computer and use it in GitHub Desktop.
import UIKit
var input =
"""
2,2,2
1,2,2
3,2,2
2,1,2
2,3,2
2,2,1
2,2,3
2,2,4
2,2,6
1,2,5
3,2,5
2,1,5
2,3,5
"""
let lines = input.split(whereSeparator: \.isNewline)
struct Cube: Equatable, Hashable {
let x: Int
let y: Int
let z: Int
}
var cubes: Set<Cube> = []
for line in lines {
let coords = line.split(separator: ",")
cubes.insert(Cube(x: Int(String(coords[0]))!, y: Int(String(coords[1]))!, z: Int(String(coords[2]))!))
}
var surfaceArea = 0
for cube in cubes {
let top = Cube(x: cube.x, y: cube.y + 1, z: cube.z)
let bottom = Cube(x: cube.x, y: cube.y - 1, z: cube.z)
let left = Cube(x: cube.x - 1, y: cube.y, z: cube.z)
let right = Cube(x: cube.x + 1, y: cube.y, z: cube.z)
let front = Cube(x: cube.x, y: cube.y, z: cube.z + 1)
let back = Cube(x: cube.x, y: cube.y, z: cube.z - 1)
let check: Set<Cube> = [top, bottom, left, right, front, back]
let intersect = cubes.intersection(check)
surfaceArea += (6 - intersect.count)
}
print(surfaceArea)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment