Last active
December 9, 2019 03:06
-
-
Save marksands/0814f7a401c910096de747bd68e6decc to your computer and use it in GitHub Desktop.
Advent of Code 2019 Day 8 Vision Detection
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 PlaygroundSupport | |
import UIKit | |
import Foundation | |
import Vision | |
func part2layer() -> [String] { | |
let layers = Array(input).chunks(ofSize: 25 * 6) | |
var picture = Array(repeating: "2", count: 25 * 6) | |
layers.forEach { layer in | |
picture = zip(picture, layer).map { p, l in | |
return p == "2" ? String(l) : p | |
} | |
} | |
return picture | |
} | |
func layerToImage(_ layer: [String]) -> UIImage { | |
let scale = 1 | |
UIGraphicsBeginImageContextWithOptions(CGSize(width: 25 * scale, height: 6 * scale), true, 0) | |
defer { UIGraphicsEndImageContext() } | |
let context = UIGraphicsGetCurrentContext() | |
layer.chunks(ofSize: 25).enumerated().forEach { (i, row) in | |
for (j, element) in row.enumerated() { | |
if element == "0" { | |
context?.setFillColor(UIColor.black.cgColor) | |
} else { | |
context?.setFillColor(UIColor.white.cgColor) | |
} | |
context?.fill(CGRect(x: j*scale, y: i*scale, width: scale, height: scale)) | |
} | |
} | |
return UIGraphicsGetImageFromCurrentImageContext()! | |
} | |
func part2() -> String { | |
let semaphore = DispatchSemaphore(value: 0) | |
var result = "" | |
let request = VNRecognizeTextRequest(completionHandler: { request, error in | |
guard let observations = request.results as? [VNRecognizedTextObservation] else { return } | |
for observation in observations { | |
guard let candidate = observation.topCandidates(1).first else { continue } | |
result += candidate.string | |
} | |
semaphore.signal() | |
}) | |
request.recognitionLevel = .accurate | |
let picture = part2layer() | |
let handler = VNImageRequestHandler(cgImage: layerToImage(picture).cgImage!, options: [:]) | |
try? handler.perform([request]) | |
semaphore.wait() | |
return result | |
} | |
let result = part2() | |
print("Found", result) | |
let view = UIImageView(image: layerToImage(part2layer())) | |
view.frame = CGRect(x: 0, y: 0, width: 10 * 25, height: 10 * 6) | |
PlaygroundPage.current.liveView = view |
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
extension Strideable { | |
public func advanced(by stride: Stride, limitedBy: Self) -> Self { | |
return distance(to: limitedBy) < stride ? limitedBy : self.advanced(by: stride) | |
} | |
} | |
extension Range where Bound : Strideable { | |
public func chunks(ofSize chunkSize: Bound.Stride) -> [Range] { | |
return stride(from: self.lowerBound, to: self.upperBound, by: chunkSize).map { currentStride in | |
let strideEnd = currentStride.advanced(by: chunkSize, limitedBy: self.upperBound) | |
return currentStride ..< strideEnd | |
} | |
} | |
} | |
extension Collection where Index: Strideable { | |
public func chunks(ofSize chunkSize: Index.Stride) -> [SubSequence] { | |
return (self.startIndex ..< self.endIndex).chunks(ofSize: chunkSize).map { self[$0] } | |
} | |
} |
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
public let input = "202201222022222222222222210222222222221222220202222222022222222222222221222200221022222222222220222222220220202221222222222222222221222221222212022202202220222222222222222222221220222222220222222212222222022222222222222222222220220122222222222220222222221220212221222222222222222221222221222202122222212211222022222222222222212220222222222222221212222222022222222222222222222221221122222222222221222222202220222220222222222222222221222220222212022222222220222022222222222202221220222222222222220202222222022222222222222221222202222022222222222222222222200222222221222222222222221222222222222222222202212220222022222222222212220220222222221222221202222222222222222222222222222202221222222222222221222222202221202221222222222222222220222220222212122222212212222222202222222200220222222222221222222222222222122222222222222222222210221022222222202221222222211220202222222222222222220222222222222222222202222200222022212222222212202221222222222222221212222222022222222222222220222211220222222222212220222222212222212220222222222222222220222220222222022202202211222022222222222221221221202222222222222202222222222222222222222220222211222122222222222221222212220222212221222222222222222221222211222212122222202210222122202222222212211220202222220222221202222222122222222222222220222221221222222222222222222212200220222222222222222222221220222202222212122202202221222222202222222222211222222222220222222202222222122222222222222221222212221122222222202220222202201222212220222222222222221220222212222222022222222200222122212222222200212220212222220222220222222222022222222022222220222202220222222222212221222212211220202220222222222222220221222200222222022222212200222022210222222211222221212222220222220212022222022222222222222222222220220022222222222220222212221221212220222222222222222221222212222212222212202211222222211222222200202220212222221222221202022222222222222022222221222212222022222222202220222202221222212220222222222222222222222200222212222212212220222222221222222210220222222222221222221202022222022222222222222222222220222222222222212222222212220220212222222222222222221222222221222212222202222221122122211222222211221220222222222222221202122222222222222122222221222222221122222222212222222222212221212222222222222222221220222221222222022222212211122222220222222210212222202222220222221222022222022222222022222222222220222122222222212220222212201220222222222222222222222220222202222202122212212221222122201222222220222220202222220222222212122222122222222122222220222211221022212222222221222202222222202220222222222222220221222200222212222222212211222222221222222202200222222222222202220222022222122222222022222221222202222122222222222221222202212222222220222222222222220222222211222222222202222210222122200222222212201220202222220202221212122222222222222222222221222222220222202222222220222222212222222221222222222222222222222220222222222222212210022022202222222221222220202222221202221202222222122222222122222220222201221122222222202222222222221220212221222222222222222220222200222202222222212201122222220222222221222222212222222212220222022222022222222022222222222201221122212222202221222212202221212222222222222222221222222211222202122212222201222222202222222212200222200222221202222222022222022222222222022221222220222222212222222222222202222221212222222222222222222222222212222212022212222222122022221222222210221200220222220222220202222222222222222122122222222220222022222222202222222222200222212222222222222222210221222221222222222222202221022122220222222211212201220222222022220212022222022222220222022220222210221022202222222221222212212220212221220222222222212221222200222202122222212211222122210222222200221212201221220112220212022202222222221022022220222210221122202222212220222212211220202222220222222222201221222221222212222202222221122222222222222210220201220222221112222212022222022222220222222222222201221122212222202222222222211221222222221222222222221222222200222202022222222202022122202222222221221202202221220202221222222212122222221222022221220222221022202222202221222222221220202221220222222222202222222221222222122202212202122022220222222210200202202222220122222212122202022222221220222220221201222122212222222222222212211220202221222222222222200220222211222222222210222202022122201222222220211220201220221202220222022202222222220020122222222220220222212222222222222202212221212221222222222222222222222220220222122222212212122122222222222222201202200221221002221212222222222222221221122220222221221122202222202221222222222221222222220222222222200220222211222222222221202200222222211222222222212212222221221112221102222222122222222020122220221221221022222222202220222222210221212220221222222222210222222221220202022211202222222022202222222222202221220221221122222012022202222222221222222222220220220122202222222220222222202222202222221222222222210222222221221202122200222201022222201222222202201221221221222012220002122222122222221121122220220222221122212222222222222202221221222221222222222222221221222212221202022222222211022022202222222220222220211222221202222002222212022222220021122222221201221222212222212220222222201222212221221222022222220222222221221202222222222220022122222222222211200211200220220202220002222212122222222022222222222210221022202222202220222212212221222220222222022222210221222200221202122212222202122122211222222202211221221222221102211222222212122222221120122221222200220122202222202221222202221222222222220222222222220220222201221202022220222222022222222022222221201210200222220212201212122212122222221121022222220210222222212222212222222212210220222220221222222222211221222211120202222220212202222122202222222200220202200022221222202122022212022222221222222222221210221022212222202221222202211222202221021222222222220222222201120212022210212212022222201022222200221221221020220212210212022212022222221221022222222222221122222222222221222212210221202220220222022222221220222210222212122201212200222122210222222201211212222021222002221222222212122222220022122220221221220222212220222221222202200120222222022222222222201220222210220222222220212200022122202222222202201201200122221022212202112212022222220020022220221222222122212220212222222202200021202220121222022222212222212221121212022202222212022222201222222212210201222220221122211012022222122222221121222220220201221122202222202222222222200021222220021222222222220222212212120202022201212200222122202122222210221210210220222002201012022202122222220021022222220200222022212222202221222212200121222220022222022222222221222210022212122222212212122222211222222212200201200021220012211202022202022222222122122222220220222222222220212221222202212020202222222222222222200222212222120222022201222221022022201221222200210220211220220122200202022212122220221022222222222222222022202221212221222222201020202202121222022222210221222200022202222212222212122022220122222210210200212020222012222102002212022221220122222222220222221022202221212222222212202220212220122222022222200221222211022212222221222221022022200222222200220220221122221102222022222212122220220122022221222222222222202220212221222202221021222212221222222222211221212212122202022201202201022022201121222021200220200220222222212102212222022222222220222220222201220222212220202222222222220221212221220222222221221221222202121212122222222211022021222020222212212201211222221022220002102212122222222021022222222210220022202220222220222212220121202220022222222222221222202211122222122202222212122221221122222111212222211122220212222112222202122221221222022220221212222022222222222222222222222022212201220222022220110220212200121202122222212220022021201122222022212212222021222012210222212222122221220020222222220221222122222220222221222212221220212212121222022220000221212212021222222202222221122020200120222010202202211220220202210202202202122220220100022222222221222122222221222222222222212022222202122222222220221221212210221212122221200221122221202020222022200200211121220022212222202202122222220020022222220200221122212222212222222202201222212200022222222222102222202220020222122202220211122222220222222121202211220021220222201202122222022222221000122221220201222022222220222220222210222220222221120222022220201222112221021212022202221201022122201121222202212220221220222212201212012212122221220122022222221221222222202220202221222221222222202221220222122222120220102202122222222222201221122022200122222220211222210220222022210112112212212222221102122221220212221122202220222222222212222121202201022222022221101222122202120200122212202211022121210022222200221221200022221222220012022202112220222120022222220220220222200222222222222220201120222221021222022222000221222201222201222221222201222220211020222220202211220201220212200202202212122220221200222222221202221222202221212221222212202021222211020222122220101221012212222201222202212022122121220022222221110212211210221112222202002222012220222002222220220222220222202220212220222212200121202211121222222222220222222221121200022222210210222222201221222122002211201121220112211022202212122221222102122222220220221022212220202222222222220122222220222222222221221220202210220200022212211121222122212222222020221221200100220222212002102212122222220012022221221211220122210220222220222220221121222202020222222220011222022200020200022212122211222221210120222002222220202220222002212102002222202220222210022222221201221202221220202221222202201021222201021222022220210221002221121221122222100212122121211122222021011221221201220002201002002212202221220111022222220201221012202221202222222210221122212201220222022220110220202222122221222211112200222120212221222222202212200220221022201012102222212221222202022220211210221110221222202221222200211121222212121222022222021222102201222202222211010220222221212021222020012212221201222122222102002212212222221120022220201201220110202222212220222212202220222222021222021220210222122200121200122220010122012222210222222110112210202002200112222022112222112222220202122220010222221201200222222222222211210122202202120222221221101221022201220202022220112110002021211222222100021221201201220212212122002222012220221211022221200210220012200220212222220200221022212201022222020220210222102211021212022221200012212021201221222101202221212011222222222100112212112221221221022220210202221021221220202220222212222220222201111222220220200220212210120201122220012012022120210121222212100210222122202102211121122222202221222101222221212200222210202220202120222202221122222211102222122222220220002200221210022210220001112120202020222122012201212010222202212110202202112220220222022221001221220000220222202220220200211022202211211222222220120222112222222222022210101210211020200021222120122222211001202102202100102222102221220101022222120221221120212220222020222200211122202210001222121222222220022210121212222212012012122022222221222121200201220222201002211020102202222221222012121221211211222110212221012222220212201222212201000222022222122222102201220222122201111221020021201120222102112220221002222222201200102222222222222012221220210222200200212220112122222211211021212220210222220222212222112021222221222201211010001022210021222022120202202200201002221120002222022222221220120220110221200022200221212121222201211222212201222222022222021222002011021200122201100112110220211222222010122211222020221112210112122202202220221211121220201210212222220221222021221221220121202221202222022220101222122101221110122212000220002022221020222201012212110022221202211122012212112220221210020220210211211110200220202021222211220021222201200222122221100221012001120022022221212000222221202120222100010211021012011022212110012202202222220210122221210202202011222220012022222212201220202202010222220222211221222011220221022200000001222020222120222202010210010000211022202001012202102222220001120220220212210020220221012202220201211022202222012222022221112222202111220200222221012000012022210222222022222211020101222222002201202222122222220222220221120222222220202222112012222210202120222221220222122222001221012210122122220200120010100220222022222000120220102112002222222010202222222220220020221220020201201222221220012112222200221221212221211222022222121221102002022202022121110012010221212021222212001221202220000202210210122212022221221001122221120220200212201220202220221212222122212220222222221221001221222021020111221020011202111221220120222200201220010000020002012121022202001222222001020221101201022021201220202000222202212020212220211222221222210221212202021222121112022211201220222220221021111221211010122022202020012212121221221020120222220220220222200210002101221222221221202201122222121221111222102122121220021211001210021222202101220011102210112122021012020200112202201220222111121220222200122210200222002001220201210222212201020222221221020220122110021222000022122001120221212210220101210221120120010202020222121202102202222120020222022220102101020200012111220220221120212212000222120221010222212001020211210200020110012222221121220222002222122001121210201100212222020202221010220220000220110101002202212011220210200121202202021222222222120222222220221212201022110101202021212202220210012210112112012102121122102202222222221002221222112210112122022221112110221200210121202212111222020220002221222022021211111002022111110120222221221220212201121122101022211120000222022211222101022220102221222210210202212000021212221220212210101222122222001222222120022021111022222101120220222020220212001221201022102002022020110212021201220112122222002211112002101200202222122220202022222200222222021221101221002121020111122011021210200221202200220102111202120211201100111120100202000212221112021220200210211120201121112210021210222120222210110222202022012220122011220120200202011112012220202200122212001221121122002110102002001202221212222212022220200222122022102100212110121200201021222221111222221120002222212221221011010001001012111121221100221001200202001100121121120120200012000200222110021221111222011221010110112102120211210122212202022222101121122221112000022002202121121010111120200200221001011212220212122010122001222002001201220011022222101220212111000002002120122211210021202212112222110122001221222022222220010011002222100221202121220121212210011211202100111022022102110221220101100221200201022121210011222101221220220221212220000222001021100220222010022122202220220211101122201211222212000201211112112220202020222112212200220220201220012202000210120012022121220201200021212200211222110022010210212112222121201220221110100221212100220122101221001101001020200111121112122201221212110221112200022211111020022222220201212020222222121222000021211211212012221202210201012022101121211102121002211200202221222202020111101112201212222010100221022202020022001112022022022221202022202222122222212121021220102010120200221020221112120122212210122011221220101220000021012021102122002200220002110221221212100220000010112020120212211020212220121222200021212202112200121201002121221102001122221200121001002202020022102111210222221022101222222012220222110220222211210201102120122212221121202210021222222021002202012212021112120111020011111201112102102212202020011021111000202212221000200121102110022000220101221122110102000022212020012102001021012010002202202002010222201201112100" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment