From b8945e61a35ec548b03a48a0682a448d7d6c4557 Mon Sep 17 00:00:00 2001 From: Dory Date: Sun, 15 Dec 2024 09:43:51 -0800 Subject: [PATCH] make d14p2 better --- day14/d14p2.swift | 36 ++++++++++++++++++++++++------------ 1 file changed, 24 insertions(+), 12 deletions(-) diff --git a/day14/d14p2.swift b/day14/d14p2.swift index b78b272..fe54950 100644 --- a/day14/d14p2.swift +++ b/day14/d14p2.swift @@ -35,31 +35,43 @@ func readInput(_ filePath: String) throws -> [Robot] { return robots } -func complexity(_ input: String) -> Int { - var last: Character = " " - return input.reduce(0, { s, c in - if c != last { last = c; return s + 1 } else { return s } - }) +func variance(_ bots: [Robot]) -> Int { + let (sX, sY) = bots.reduce((0, 0)) { s, bot in (s.0 + bot.x, s.1 + bot.y) } + let (mX, mY) = (sX/bots.count, sY/bots.count) + return bots.reduce(0) { s, bot in + s + (mX-bot.x)*(mX-bot.x) + (mY-bot.y)*(mY-bot.y) } } -func printRobots(_ robots: [Robot]) -> String { - var screen: [[String]] = Array(repeating: Array(repeating: " ", count: 101), count: 103) - robots.forEach { robot in screen[robot.y][robot.x] = "█" } +func printRobots(_ bots: [Robot]) -> String { + var screen: [[String]] = Array( + repeating: Array(repeating: " ", count: 101), + count: 52 + ) + bots.forEach { bot in + let (q, r) = bot.y.quotientAndRemainder(dividingBy: 2) + if screen[q][bot.x] == " " { + screen[q][bot.x] = (r == 0) ? "▀" : "▄" + } else { + screen[q][bot.x] = "█" + } + } return screen.map { row in row.joined() }.joined(separator: "\n") } var bots = try readInput(CommandLine.arguments[1]) var i = 0 +var minSigma = Int.max while true { i += 1 for i in 0..