d11
This commit is contained in:
62
day11/d11p1.swift
Normal file
62
day11/d11p1.swift
Normal file
@@ -0,0 +1,62 @@
|
||||
import Foundation
|
||||
|
||||
class Node : CustomStringConvertible {
|
||||
var value: Int
|
||||
var next: Node?
|
||||
init(_ value: Int, next: Node? = nil) {
|
||||
self.value = value
|
||||
self.next = next
|
||||
}
|
||||
var description: String {
|
||||
let nextStr = if let next { String(describing: next) } else { "nil" }
|
||||
return "(\(value))->\(nextStr)"
|
||||
}
|
||||
func evolve() -> Int {
|
||||
if value == 0 {
|
||||
value = 1
|
||||
return 1
|
||||
}
|
||||
// I don't have log10() unless I import Graphics, which is overkill...
|
||||
let s = String(value)
|
||||
if s.count.isMultiple(of: 2) {
|
||||
let secondHalf = s.count/2 ..< s.count
|
||||
let newNode = Node(Int(String(Array(s)[secondHalf]))!, next: next)
|
||||
value = Int(String(Array(s)[0 ..< s.count/2]))!
|
||||
next = newNode
|
||||
return 2
|
||||
}
|
||||
value = value*2024
|
||||
return 1
|
||||
}
|
||||
}
|
||||
|
||||
func readInput(_ filePath: String) throws -> Node {
|
||||
let content = try String(contentsOfFile: filePath, encoding: .ascii)
|
||||
.trimmingCharacters(in: .whitespacesAndNewlines)
|
||||
let head: Node = Node(-1)
|
||||
var current = head
|
||||
content.split(separator: " ").forEach { i in
|
||||
print(i)
|
||||
let new = Node(Int(i)!)
|
||||
current.next = new
|
||||
current = new
|
||||
}
|
||||
return head.next!
|
||||
}
|
||||
|
||||
func evolve(_ chain: Node) -> Int {
|
||||
var next: Node? = chain
|
||||
var count = 0
|
||||
while let node = next {
|
||||
next = node.next
|
||||
count += node.evolve()
|
||||
}
|
||||
return count
|
||||
}
|
||||
|
||||
let nums = try readInput(CommandLine.arguments[1])
|
||||
print(nums)
|
||||
for i in 1...25 {
|
||||
let count = evolve(nums)
|
||||
print("\(i): \(count)") // - \(nums)")
|
||||
}
|
39
day11/d11p2.swift
Normal file
39
day11/d11p2.swift
Normal file
@@ -0,0 +1,39 @@
|
||||
import Foundation
|
||||
|
||||
struct NumTimes : Hashable {
|
||||
let num: Int
|
||||
let times: Int
|
||||
}
|
||||
|
||||
func readInput(_ filePath: String) throws -> [Int] {
|
||||
return try String(contentsOfFile: filePath, encoding: .ascii)
|
||||
.trimmingCharacters(in: .whitespacesAndNewlines)
|
||||
.split(separator: " ").map { Int($0)! }
|
||||
}
|
||||
|
||||
func evolve(_ num: Int) -> [Int] {
|
||||
if num == 0 { return [1] }
|
||||
let s = String(num)
|
||||
if s.count.isMultiple(of: 2) {
|
||||
return [
|
||||
Int(String(Array(s)[s.count/2 ..< s.count]))!,
|
||||
Int(String(Array(s)[0 ..< s.count/2]))!
|
||||
]
|
||||
}
|
||||
return [num * 2024]
|
||||
|
||||
}
|
||||
|
||||
var cache: [NumTimes: Int] = [:]
|
||||
|
||||
func count(_ num: Int, times: Int) -> Int {
|
||||
if times == 0 { return 1 }
|
||||
let key = NumTimes(num: num, times: times)
|
||||
if let cached = cache[key] { return cached }
|
||||
cache[key] = evolve(num).map { n in count(n, times: times-1) }.reduce(0, +)
|
||||
return cache[key]!
|
||||
}
|
||||
|
||||
let nums = try readInput(CommandLine.arguments[1])
|
||||
print(nums)
|
||||
print(nums.map { count($0, times: 75) }.reduce(0, +))
|
1
day11/input.txt
Normal file
1
day11/input.txt
Normal file
@@ -0,0 +1 @@
|
||||
0 27 5409930 828979 4471 3 68524 170
|
1
day11/test.txt
Normal file
1
day11/test.txt
Normal file
@@ -0,0 +1 @@
|
||||
125 17
|
Reference in New Issue
Block a user