Files
aoc24/day22/d22p1.swift
2024-12-22 10:10:13 -08:00

19 lines
564 B
Swift

import Foundation
func readInput(_ filePath: String) throws -> [Int] {
return try String(contentsOfFile: filePath, encoding: .ascii)
.split(separator: "\n").compactMap { Int($0) }
}
func rand(seed: Int, n: Int) -> Int {
return (0..<n).reduce(seed) { prev, _ in
var res = ((prev << 6) ^ prev) & 0b111111111111111111111111
res = ((res >> 5) ^ res)
return ((res << 11) ^ res) & 0b111111111111111111111111
}
}
let seeds = try readInput(CommandLine.arguments[1])
print(seeds.map { rand(seed: $0, n: 2000) }.reduce(0, +))