From 59111af1cbafde8ebe0f40a39d53c7cf3afe97b8 Mon Sep 17 00:00:00 2001 From: Sven Weidauer Date: Sun, 20 Dec 2020 14:11:45 +0100 Subject: [PATCH] Day 20 part 2 --- day20/main.swift | 110 +++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 93 insertions(+), 17 deletions(-) diff --git a/day20/main.swift b/day20/main.swift index 7db0727..f4e264c 100644 --- a/day20/main.swift +++ b/day20/main.swift @@ -166,27 +166,15 @@ extension Board.Cell { return result } } - +let start = Date() guard let solution = Board(tiles: tilesById.keys).solve() else { fatalError() } +let end = Date() + +print("Took", end.timeIntervalSince(start)) let e = solution.size - 1 -let ts = tilesById.first!.value.size -for y in 0...e { - for l in 0.. CompleteImage { + CompleteImage(board: self, flipped: flipped, rotation: rotation) + } +} + + +extension Board.CompleteImage { + var size: Int { + board.size * (board.cells[0].chosen!.end - 1) + } + + subscript(x: Int, y: Int) -> Bool { + get { + var coord = flipped ? (x: size - 1 - x, y: y) : (x: x, y: y) + coord = rotation.get(x: coord.x, y: coord.y, size: size) + + let tileSize = board.cells[0].chosen!.end - 1 + let (tileX, offsetX) = coord.x.quotientAndRemainder(dividingBy: tileSize) + let (tileY, offsetY) = coord.y.quotientAndRemainder(dividingBy: tileSize) + let tile = board[tileX, tileY].chosen! + return tile[offsetX + 1, offsetY + 1] + } + } +} + +for y in 0.. Bool { + return self[x + 18, y] && + self[x + 0, y + 1] && self[x + 5, y + 1] && self[x + 6, y + 1] && self[x + 11, y + 1] && self[x + 12, y + 1] && self[x + 17, y + 1] && self[x + 18, y + 1] && self[x + 19, y + 1] && + self[x + 1, y + 2] && self[x + 4, y + 2] && self[x + 7, y + 2] && self[x + 10, y + 2] && self[x + 13, y + 2] && self[x + 16, y + 2] + } + + func countMonsters() -> Int { + var count = 0 + for y in 0.. Int { + var count = 0 + for y in 0.. 0 { + let monsterTiles = monsters * 15 + let result = ones - monsterTiles + print(result) + break outer + } + } +} +