day2 part1
This commit is contained in:
85
day02/source/day02.swift
Normal file
85
day02/source/day02.swift
Normal file
@@ -0,0 +1,85 @@
|
||||
#if !hasFeature(Embedded)
|
||||
import Foundation
|
||||
#endif
|
||||
|
||||
@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("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 = """
|
||||
11-22,95-115,998-1012,1188511880-1188511890,222220-222224,1698522-1698528,446443-446449,38593856-38593862,565653-565659,824824821-824824827,2121212118-2121212124
|
||||
"""
|
||||
|
||||
let realInput = """
|
||||
492410748-492568208,246-390,49-90,16-33,142410-276301,54304-107961,12792-24543,3434259704-3434457648,848156-886303,152-223,1303-1870,8400386-8519049,89742532-89811632,535853-567216,6608885-6724046,1985013826-1985207678,585591-731454,1-13,12067202-12233567,6533-10235,6259999-6321337,908315-972306,831-1296,406-824,769293-785465,3862-5652,26439-45395,95-136,747698990-747770821,984992-1022864,34-47,360832-469125,277865-333851,2281-3344,2841977-2953689,29330524-29523460
|
||||
"""
|
||||
Reference in New Issue
Block a user