day 2 part 2

This commit is contained in:
Andrew Glaze
2025-12-12 17:22:22 -05:00
parent ed6ae9333a
commit 1de452e4c2
2 changed files with 59 additions and 33 deletions

Binary file not shown.

View File

@@ -4,40 +4,13 @@ import Foundation
@main
struct Day02 {
#if hasFeature(Embedded)
static func main() {
gfxInitDefault()
consoleInit(GFX_TOP, nil)
run()
// Main loop
while aptMainLoop() {
hidScanInput()
let kDown = hidKeysDown()
if kDown & KEY_START != 0 {
break // break in order to return to hbmenu
}
gfxFlushBuffers()
gfxSwapBuffers()
}
gfxExit()
}
#else
static func main() {
run()
}
#endif
static func run() {
print("Parsing...")
let parsed = parseInput(realInput)
print("Running Part 1...")
part1(parsed)
//part2(parsed)
print("Running Part 2...")
part2(parsed)
print("Finished!")
}
@@ -45,10 +18,10 @@ struct Day02 {
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
})
//.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!)!)
@@ -72,8 +45,61 @@ struct Day02 {
}
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)
run()
while aptMainLoop() {
hidScanInput()
let kDown = hidKeysDown()
if kDown & KEY_START != 0 {
break
}
gfxFlushBuffers()
gfxSwapBuffers()
}
gfxExit()
}
#else
static func main() {
run()
}
#endif
}
let testInput = """