#!/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)")