From 4c68223c42d60390047f34dfaed783c8764fd746 Mon Sep 17 00:00:00 2001 From: Dory Date: Sun, 22 Dec 2024 11:11:59 -0800 Subject: [PATCH] d22p2 --- day22/d22p2.swift | 30 ++++++++++++++++++++++++++++++ day22/test2.txt | 4 ++++ 2 files changed, 34 insertions(+) create mode 100644 day22/d22p2.swift create mode 100644 day22/test2.txt diff --git a/day22/d22p2.swift b/day22/d22p2.swift new file mode 100644 index 0000000..9a97d7e --- /dev/null +++ b/day22/d22p2.swift @@ -0,0 +1,30 @@ +import Foundation + +func readInput(_ filePath: String) throws -> [Int] { + return try String(contentsOfFile: filePath, encoding: .ascii) + .split(separator: "\n").compactMap { Int($0) } +} + +func haggle(_ seeds: [Int]) -> [[Int]: [Int: Int]] { + var bananas: [[Int]: [Int: Int]] = [:] + for (monkey, seed) in seeds.enumerated() { + var prevs = [Int.min, Int.min, Int.min, Int.min] + var lastPrice = seed + for _ in 0..<2000 { + var price = ((lastPrice << 6) ^ lastPrice) & 16777215 + price = ((price >> 5) ^ price) + price = ((price << 11) ^ price) & 16777215 + prevs[0] = prevs[1]; prevs[1] = prevs[2]; prevs[2] = prevs[3] + prevs[3] = (price % 10) - (lastPrice % 10) + lastPrice = price + if let _ = bananas[prevs, default: [:]][monkey] { continue } + bananas[prevs, default: [:]][monkey] = price % 10 + } + } + return bananas +} + +let seeds = try readInput(CommandLine.arguments[1]) +let bananas = haggle(seeds) +print(bananas.values.map { $0.values.reduce(0, +) }.max() ?? "ERROR") + diff --git a/day22/test2.txt b/day22/test2.txt new file mode 100644 index 0000000..201df76 --- /dev/null +++ b/day22/test2.txt @@ -0,0 +1,4 @@ +1 +2 +3 +2024