AoC/2022/day1.swift
2022-12-01 10:39:48 +01:00

27 lines
No EOL
592 B
Swift

import Foundation
let input = try String(contentsOf: URL(fileURLWithPath: "day1.input"))
let scanner = Scanner(string: input)
scanner.charactersToBeSkipped = nil
var currentElf = 0
var allElves: [Int] = []
while !scanner.isAtEnd {
if scanner.scanString("\n") != nil {
allElves.append(currentElf)
currentElf = 0
}
if let calories = scanner.scanInt() {
currentElf += calories
_ = scanner.scanString("\n")
}
}
allElves.append(currentElf)
allElves.sort()
print("Maximum:", allElves.last!)
print("Top 3:", allElves.suffix(3).reduce(0, +))