day 1 part 1

This commit is contained in:
Andrew Glaze
2025-12-04 20:40:12 -05:00
commit 3144734058
4 changed files with 4595 additions and 0 deletions

View File

@@ -0,0 +1,47 @@
// The Swift Programming Language
// https://docs.swift.org/swift-book
@main
struct day01 {
static func parseInput(_ input: String) -> [(Direction, Int)] {
return input.split(whereSeparator: \.isNewline)
.map({ line in
return (Direction(line.first!), Int(line.dropFirst())!)
})
}
static func part1(_ input: [(Direction, Int)]) {
var cur = 50
var count = 0
for (dir, num) in input {
let last = cur
cur = switch dir {
case .Left: (cur - num) % 100
case .Right: (cur + num) % 100
}
if (cur < 0) { cur += 100 }
print(last, cur, dir, num)
if (cur == 0) { count += 1 }
}
print("Part 1: \(count)")
}
static func main() {
let parsed = parseInput(realInput)
part1(parsed)
}
}
enum Direction {
case Left
case Right
init(_ char: Character) {
self = switch char {
case "L": Direction.Left
case "R": Direction.Right
default: fatalError()
}
}
}

File diff suppressed because it is too large Load Diff