Skip to content

Instantly share code, notes, and snippets.

@groob
Last active December 9, 2019 15:51
Show Gist options
  • Select an option

  • Save groob/6e8514d1128d9ae810b57af07355087a to your computer and use it in GitHub Desktop.

Select an option

Save groob/6e8514d1128d9ae810b57af07355087a to your computer and use it in GitHub Desktop.
import Foundation
enum Pixel: Int {
case black = 0
case white = 1
case transparent = 2
}
let width = 25
let height = 6
let size = width * height
let input = try
String(contentsOfFile: "data.txt")
.trimmingCharacters(in: .whitespacesAndNewlines)
input
.enumerated()
.reduce(into: Array(repeating: [Pixel](), count: input.count / size)) { partial, current in
let pixel = Pixel(rawValue: Int(String(current.1))!)!
partial[current.0 / size].append(pixel)
}
.reduce(into: Array(repeating: Pixel.transparent, count: size)) { partial, layer in
for (idx, pixel) in layer.enumerated() {
if pixel != .transparent && (partial[idx] == .transparent) {
partial[idx] = pixel
}
}
}
.enumerated()
.forEach {
if $0.0 != 0 && $0.0 % width == 0 { print("") }
print($0.1 == .white ? "█" : " ", terminator: "")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment