2022 Day 1
This commit is contained in:
parent
8ae21d721e
commit
5ef21c471d
2 changed files with 28 additions and 0 deletions
1
2022/.gitignore
vendored
Normal file
1
2022/.gitignore
vendored
Normal file
|
@ -0,0 +1 @@
|
|||
*.input
|
27
2022/day1.swift
Normal file
27
2022/day1.swift
Normal 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, +))
|
Loading…
Add table
Reference in a new issue