summaryrefslogtreecommitdiffstats
path: root/Day 1/Day_01.swift
diff options
context:
space:
mode:
authorNSDepression <avimanyu.apple@gmail.com>2025-12-02 17:15:20 +0530
committerNSDepression <avimanyu.apple@gmail.com>2025-12-02 17:15:20 +0530
commitf35667631aca011ee5f99772e99f9264e06f7ca1 (patch)
tree77252ffb184000b3f61f5b2d1fd60ea4cd4134ed /Day 1/Day_01.swift
downloadAoC-2025-f35667631aca011ee5f99772e99f9264e06f7ca1.tar.gz
AoC-2025-f35667631aca011ee5f99772e99f9264e06f7ca1.zip
Day 1
Diffstat (limited to 'Day 1/Day_01.swift')
-rwxr-xr-xDay 1/Day_01.swift39
1 files changed, 39 insertions, 0 deletions
diff --git a/Day 1/Day_01.swift b/Day 1/Day_01.swift
new file mode 100755
index 0000000..b2a77f4
--- /dev/null
+++ b/Day 1/Day_01.swift
@@ -0,0 +1,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)") \ No newline at end of file