day 2 part 2
This commit is contained in:
BIN
day02/day02
BIN
day02/day02
Binary file not shown.
@@ -4,6 +4,74 @@ import Foundation
|
|||||||
|
|
||||||
@main
|
@main
|
||||||
struct Day02 {
|
struct Day02 {
|
||||||
|
static func run() {
|
||||||
|
print("Parsing...")
|
||||||
|
let parsed = parseInput(realInput)
|
||||||
|
//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 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)
|
#if hasFeature(Embedded)
|
||||||
static func main() {
|
static func main() {
|
||||||
gfxInitDefault()
|
gfxInitDefault()
|
||||||
@@ -32,48 +100,6 @@ struct Day02 {
|
|||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
static func run() {
|
|
||||||
print("Parsing...")
|
|
||||||
let parsed = parseInput(realInput)
|
|
||||||
print("Running Part 1...")
|
|
||||||
part1(parsed)
|
|
||||||
//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) {
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
let testInput = """
|
let testInput = """
|
||||||
|
|||||||
Reference in New Issue
Block a user