remove string methods
This commit is contained in:
@@ -3,14 +3,15 @@
|
|||||||
|
|
||||||
@main
|
@main
|
||||||
struct day01 {
|
struct day01 {
|
||||||
static func parseInput(_ input: String) -> [(Direction, Int)] {
|
static func parseInput(_ input: [StaticString]) -> [(Direction, Int)] {
|
||||||
return input.split(whereSeparator: {char in char.isNewline})
|
input.map({ line in
|
||||||
.map({ line in
|
let turns = line.withUTF8Buffer { tmp in
|
||||||
return (Direction(line.first!), Int(line.dropFirst())!)
|
atoi(str: [UInt8](tmp.dropFirst()))
|
||||||
|
}
|
||||||
|
return (Direction(line.utf8Start[0]), turns)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static func part1(_ input: [(Direction, Int)]) {
|
static func part1(_ input: [(Direction, Int)]) {
|
||||||
var cur = 50
|
var cur = 50
|
||||||
var count = 0
|
var count = 0
|
||||||
@@ -59,16 +60,30 @@ struct day01 {
|
|||||||
part1(parsed)
|
part1(parsed)
|
||||||
part2(parsed)
|
part2(parsed)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static func atoi(str: [UInt8]) -> Int {
|
||||||
|
var result = 0
|
||||||
|
|
||||||
|
for char in str {
|
||||||
|
if char >= 0x30 && char <= 0x39 {
|
||||||
|
result = result * 10 + (Int(char) - 0x30)
|
||||||
|
} else {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return result
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
enum Direction {
|
enum Direction {
|
||||||
case Left
|
case Left
|
||||||
case Right
|
case Right
|
||||||
|
|
||||||
init(_ char: Character) {
|
init(_ char: UInt8) {
|
||||||
self = switch char {
|
self = switch char {
|
||||||
case "L": Direction.Left
|
case 0x4c: Direction.Left
|
||||||
case "R": Direction.Right
|
case 0x52: Direction.Right
|
||||||
default: fatalError()
|
default: fatalError()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user