day 2 part 2

This commit is contained in:
Andrew Glaze
2025-12-12 17:22:22 -05:00
parent ed6ae9333a
commit fc40e61e6d
2 changed files with 58 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,60 @@ 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 cnt = sCur.count
guard cnt > 1 else { continue }
nextLength: for checkOffset in 0...((cnt / 2) - 1) {
let sub = sCur[sCur.startIndex...sCur.index(sCur.startIndex, offsetBy: checkOffset)]
for windowOffset in stride(from: sub.count, to: cnt, by: sub.count) {
guard windowOffset + (sub.count) <= cnt else { break nextLength }
let winStartIdx = sCur.index(sCur.startIndex, offsetBy: windowOffset)
let winEndIdx = sCur.index(winStartIdx, offsetBy: sub.count - 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 = """