This commit is contained in:
2024-12-22 11:11:59 -08:00
parent 83979ce5d2
commit 4c68223c42
2 changed files with 34 additions and 0 deletions

30
day22/d22p2.swift Normal file
View File

@@ -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")

4
day22/test2.txt Normal file
View File

@@ -0,0 +1,4 @@
1
2
3
2024