blob: b2a77f41df1a4d1eb7b6d773de53ea3f09a71e86 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
|
#!/usr/bin/swift
import Foundation
guard CommandLine.arguments.count >= 2 else {
fatalError("Usage: ./Day_01 <input.txt>")
}
let moves = try! String(contentsOfFile: CommandLine.arguments[1], encoding: .utf8)
.split(separator: "\n")
.compactMap { line in
guard let first = line.first,
let magnitude = Int(line[line.index(after: line.startIndex)...])
else { fatalError("L13 : [ERROR] Failed to read magnitude error") }
let sign = (first == "R" ? 1 : -1)
return sign * magnitude
}
var dialValue = 50
var count = 0
for move in moves {
dialValue = ((dialValue + move) % 100 + 100) % 100
if dialValue == 0 {
count += 1
}
}
print("=======Part 1=======\n\(count)")
dialValue = 50
count = 0
for move in moves {
let magnitude = abs(move)
let effectivePosition = move > 0 ? dialValue : (100 - dialValue) % 100
count += (effectivePosition + magnitude - 1) / 100
dialValue = ((dialValue + move) % 100 + 100) % 100
if dialValue == 0 {
count += 1
}
}
print("=======Part 2=======\n\(count)")
|