Skip to content

Commit 0366a77

Browse files
committed
Days 1 and 2
0 parents  commit 0366a77

File tree

6 files changed

+4919
-0
lines changed

6 files changed

+4919
-0
lines changed

.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
*
2+
!.gitignore
3+
!*.swift
4+
!*.md
5+
!*.txt

01.swift

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import Foundation
2+
3+
guard var input = try? String(contentsOf: URL(fileURLWithPath: "01.txt")) else {
4+
fatalError("File error")
5+
}
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

Comments
 (0)