day 1 part 1
This commit is contained in:
8
day01/.gitignore
vendored
Normal file
8
day01/.gitignore
vendored
Normal 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
15
day01/Package.swift
Normal 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"
|
||||
),
|
||||
]
|
||||
)
|
||||
47
day01/Sources/day01/day01.swift
Normal file
47
day01/Sources/day01/day01.swift
Normal 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()
|
||||
}
|
||||
}
|
||||
}
|
||||
4525
day01/Sources/day01/input.swift
Normal file
4525
day01/Sources/day01/input.swift
Normal file
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user