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