From 5ef21c471dde5c04d84f4cbfd730d9371ff8b77e Mon Sep 17 00:00:00 2001 From: Sven Weidauer Date: Thu, 1 Dec 2022 10:39:48 +0100 Subject: [PATCH] 2022 Day 1 --- 2022/.gitignore | 1 + 2022/day1.swift | 27 +++++++++++++++++++++++++++ 2 files changed, 28 insertions(+) create mode 100644 2022/.gitignore create mode 100644 2022/day1.swift diff --git a/2022/.gitignore b/2022/.gitignore new file mode 100644 index 0000000..63ff173 --- /dev/null +++ b/2022/.gitignore @@ -0,0 +1 @@ +*.input diff --git a/2022/day1.swift b/2022/day1.swift new file mode 100644 index 0000000..b331e47 --- /dev/null +++ b/2022/day1.swift @@ -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, +)) \ No newline at end of file