From f35667631aca011ee5f99772e99f9264e06f7ca1 Mon Sep 17 00:00:00 2001 From: NSDepression Date: Tue, 2 Dec 2025 17:15:20 +0530 Subject: Day 1 --- Day 1/Day_01.swift | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100755 Day 1/Day_01.swift (limited to 'Day 1/Day_01.swift') 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 ") +} + +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 -- cgit v1.2.3