day 2 part 2
This commit is contained in:
BIN
day02/day02
BIN
day02/day02
Binary file not shown.
@@ -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 = """
|
||||
|
||||
Reference in New Issue
Block a user