Skip to content

Commit 282224e

Browse files
committed
add utils and beatify solution for day1
1 parent 142aed0 commit 282224e

File tree

5 files changed

+78
-18
lines changed

5 files changed

+78
-18
lines changed

Sources/Array+Utils.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import Foundation
2+
3+
extension Array where Element == Int {
4+
var sum: Int {
5+
self.reduce(0, +)
6+
}
7+
}

Sources/Day01.swift

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,27 @@
11
import Algorithms
22

33
struct Day01: AdventDay {
4-
// Save your data in a corresponding text file in the `Data` directory.
54
var data: String
65

7-
var lines: [[Substring.SubSequence]] {
8-
data.split(separator: "\n").map { $0.split(separator: " ") }
6+
var lines: [[Int]] {
7+
data.lines().map { $0.integers() }
98
}
109

1110
var left: [Int] {
12-
lines.map { Int($0[0]) ?? 0 }.sorted()
11+
lines.map { $0[0] }.sorted()
1312
}
1413

1514
var right: [Int] {
16-
lines.map { Int($0[1]) ?? 0 }.sorted()
15+
lines.map { $0[1] }.sorted()
1716
}
1817

19-
// add here any computed values useful for the challenge
20-
2118
func part1() -> Int {
22-
var sum = 0
23-
for (one, two) in zip(left, right) {
24-
sum += abs(one - two)
25-
}
26-
return sum
19+
zip(left, right).map { l, r in abs(l - r) }.sum
2720
}
2821

2922
func part2() -> Int {
30-
var similarity = 0
31-
for elem in left {
32-
let occurrences = right.filter { $0 == elem }.count
33-
similarity += elem * occurrences
34-
}
35-
return similarity
23+
left.map { elem in
24+
elem * right.filter { $0 == elem }.count
25+
}.sum
3626
}
3727
}

Sources/String+Parsing.swift

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import Algorithms
2+
import Foundation
3+
4+
extension StringProtocol {
5+
func trimmed() -> String {
6+
self.trimmingCharacters(in: .whitespacesAndNewlines)
7+
}
8+
9+
func lines() -> [SubSequence] {
10+
self.split(separator: "\n")
11+
}
12+
13+
func integers(separator: String = " ") -> [Int] {
14+
self.split(separator: separator).map { Int($0) }.compactMap { $0 }
15+
}
16+
17+
func doubles(separator: String = " ") -> [Double] {
18+
self.split(separator: separator).map { Double($0) }.compactMap { $0 }
19+
}
20+
}

Tests/Array+Utils.swift

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import Testing
2+
3+
@testable import aoc
4+
5+
@Suite("Array extensions")
6+
struct ArrayExtensions {
7+
@Test("Sum")
8+
func testSum() async throws {
9+
#expect([1, 2, 3, 4, 5, 6].sum == 21)
10+
#expect([].sum == 0)
11+
#expect([].sum == 0)
12+
}
13+
14+
}

Tests/String+Parsing.swift

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import Testing
2+
3+
@testable import aoc
4+
5+
@Suite("String extensions")
6+
struct StringExtensions {
7+
@Test("Trimming")
8+
func testTrimming() async throws {
9+
#expect(" hello ".trimmed() == "hello")
10+
#expect("\n\nhello\n".trimmed() == "hello")
11+
}
12+
13+
@Test("Lines Parsing")
14+
func testLinesParsing() async throws {
15+
#expect("".lines() == [])
16+
#expect("\n\n".lines() == [])
17+
#expect("1\n2\n3\n".lines() == ["1", "2", "3"])
18+
#expect("1\n\n2".lines() == ["1", "2"])
19+
}
20+
21+
@Test("Value parsing")
22+
func testValueParsing() async throws {
23+
#expect("".integers() == [])
24+
#expect("a".integers() == [])
25+
#expect("a 1 toto 1.3".integers() == [1])
26+
#expect("a 1 toto 1.3".doubles() == [1.0, 1.3])
27+
#expect("1 2 3 4".integers() == [1, 2, 3, 4])
28+
}
29+
}

0 commit comments

Comments
 (0)