We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
0 parents commit 0366a77Copy full SHA for 0366a77
.gitignore
@@ -0,0 +1,5 @@
1
+*
2
+!.gitignore
3
+!*.swift
4
+!*.md
5
+!*.txt
01.swift
@@ -0,0 +1,28 @@
+import Foundation
+
+guard var input = try? String(contentsOf: URL(fileURLWithPath: "01.txt")) else {
+ fatalError("File error")
+}
6
7
+let groups = input.components(separatedBy: "\n\n")
8
9
+let elfs: [[Int]] = groups.map { group in
10
+ return group.components(separatedBy: .newlines).map { line in
11
+ guard let value = Int(line) else {
12
+ fatalError("Parse error")
13
+ }
14
+ return value
15
16
17
18
+let sums = elfs.map { $0.reduce(0, +) }
19
+let topThree = sums.sorted().suffix(3)
20
21
+let answer1 = topThree.last!
22
+let answer2 = topThree.reduce(0, +)
23
24
+print(answer1)
25
+assert(answer1 == 69289)
26
27
+print(answer2)
28
+assert(answer2 == 205615)
0 commit comments