2022 Day 1

This commit is contained in:
Sven Weidauer 2022-12-01 10:39:48 +01:00
parent 8ae21d721e
commit 5ef21c471d
2 changed files with 28 additions and 0 deletions

1
2022/.gitignore vendored Normal file
View file

@ -0,0 +1 @@
*.input

27
2022/day1.swift Normal file
View file

@ -0,0 +1,27 @@
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, +))