d22p2
This commit is contained in:
30
day22/d22p2.swift
Normal file
30
day22/d22p2.swift
Normal 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
4
day22/test2.txt
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
1
|
||||||
|
2
|
||||||
|
3
|
||||||
|
2024
|
Reference in New Issue
Block a user