143 lines
4.1 KiB
Swift
143 lines
4.1 KiB
Swift
#if !hasFeature(Embedded)
|
|
import Foundation
|
|
#endif
|
|
|
|
@main
|
|
struct Day02 {
|
|
static func run() {
|
|
guard let input = realInput else {
|
|
print("input sttring nil")
|
|
return
|
|
}
|
|
print("Parsing...")
|
|
let parsed = parseInput(input)
|
|
print("Running Part 1...")
|
|
part1(parsed)
|
|
print("Running Part 2...")
|
|
part2(parsed)
|
|
print("Finished!")
|
|
}
|
|
|
|
typealias ParsedInput = [(UInt64, UInt64)]
|
|
|
|
static func parseInput(_ input: String) -> ParsedInput {
|
|
input.split(separator: ",")
|
|
//.filter({ line in
|
|
// let split = line.split(separator: "-")
|
|
// return String(split[0]).count % 2 == 0 || String(split[1]).count % 2 == 0
|
|
//})
|
|
.map({ line in
|
|
let split = line.split(separator: "-")
|
|
return (UInt64(split.first!)!, UInt64(split.last!)!)
|
|
})
|
|
}
|
|
|
|
static func part1(_ input: ParsedInput) {
|
|
var sum: UInt64 = 0
|
|
for (lhs, rhs) in input {
|
|
for cur in lhs...rhs {
|
|
let cnt = String(cur).count
|
|
guard cnt % 2 == 0 else { continue }
|
|
let low = cur % UInt64(pow(10, (Double(cnt) / 2)))
|
|
let hi = low * UInt64(pow(10, (Double(cnt) / 2)))
|
|
if cur == hi + low {
|
|
sum += cur
|
|
}
|
|
}
|
|
}
|
|
print("Part 1: \(sum)")
|
|
}
|
|
|
|
static func part2(_ input: ParsedInput) {
|
|
var sum: UInt64 = 0
|
|
for (lhs, rhs) in input {
|
|
for cur in lhs...rhs {
|
|
let scur = String(cur)
|
|
let curCount = scur.count
|
|
guard curCount > 1 else { continue }
|
|
|
|
nextLength: for checkOffset in 0...((curCount / 2) - 1) {
|
|
let sub = scur[scur.startIndex...scur.index(scur.startIndex, offsetBy: checkOffset)]
|
|
let subCount = sub.count
|
|
|
|
for windowOffset in stride(from: subCount, to: curCount, by: subCount) {
|
|
guard windowOffset + subCount <= curCount else { break nextLength }
|
|
let winStartIdx = scur.index(scur.startIndex, offsetBy: windowOffset)
|
|
let winEndIdx = scur.index(winStartIdx, offsetBy: subCount - 1)
|
|
let window = scur[winStartIdx...winEndIdx]
|
|
if sub != window {
|
|
continue nextLength
|
|
}
|
|
}
|
|
sum += cur
|
|
break
|
|
}
|
|
}
|
|
}
|
|
print("Part 2: \(sum)")
|
|
}
|
|
|
|
#if hasFeature(Embedded)
|
|
static func main() {
|
|
gfxInitDefault()
|
|
consoleInit(GFX_TOP, nil)
|
|
|
|
guard romfsInit() == 0 else {
|
|
print("Couldn't init RomFS")
|
|
return
|
|
}
|
|
|
|
testInput = stringFromFile("romfs:/test.txt")
|
|
realInput = stringFromFile("romfs:/input.txt")
|
|
|
|
run()
|
|
|
|
while aptMainLoop() {
|
|
hidScanInput()
|
|
let kDown = hidKeysDown()
|
|
if kDown & KEY_START != 0 {
|
|
break
|
|
}
|
|
|
|
gfxFlushBuffers()
|
|
gfxSwapBuffers()
|
|
}
|
|
|
|
romfsExit()
|
|
gfxExit()
|
|
}
|
|
|
|
static func stringFromFile(_ path: String) -> String? {
|
|
let file = fopen(path, "r")
|
|
var ret = ""
|
|
var buf = [CChar](repeating: 0, count: 100)
|
|
while fgets(&buf, 100, file) != nil {
|
|
ret += String(cString: buf)
|
|
}
|
|
guard feof(file) != 0 else {
|
|
print("Error reading file \(path)")
|
|
return nil
|
|
}
|
|
while ret.first?.isNewline ?? false {
|
|
ret.removeFirst()
|
|
}
|
|
while ret.last?.isNewline ?? false {
|
|
ret.removeLast()
|
|
}
|
|
return ret
|
|
}
|
|
#else
|
|
static func main() {
|
|
testInput = try? String(contentsOfFile: "romfs/test.txt").trimmingCharacters(in: .whitespacesAndNewlines)
|
|
realInput = try? String(contentsOfFile: "romfs/input.txt").trimmingCharacters(in: .whitespacesAndNewlines)
|
|
|
|
run()
|
|
}
|
|
#endif
|
|
|
|
}
|
|
|
|
var testInput: String? = nil
|
|
|
|
var realInput: String? = nil
|