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

8
day01/.gitignore vendored Normal file
View File

@@ -0,0 +1,8 @@
.DS_Store
/.build
/Packages
xcuserdata/
DerivedData/
.swiftpm/configuration/registries.json
.swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata
.netrc

15
day01/Package.swift Normal file
View File

@@ -0,0 +1,15 @@
// swift-tools-version: 6.2
// The swift-tools-version declares the minimum version of Swift required to build this package.
import PackageDescription
let package = Package(
name: "day01",
targets: [
// Targets are the basic building blocks of a package, defining a module or a test suite.
// Targets can depend on other targets in this package and products from dependencies.
.executableTarget(
name: "day01"
),
]
)

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