Skip to content

Commit fe973c0

Browse files
committed
improve code for day1
1 parent 05428ef commit fe973c0

File tree

1 file changed

+9
-6
lines changed

1 file changed

+9
-6
lines changed

Sources/Day01.swift

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,22 +8,25 @@ struct Day01: AdventDay {
88
data.split(separator: "\n").map { $0.split(separator: " ") }
99
}
1010

11+
var left: [Int] {
12+
lines.map { Int($0[0]) ?? 0 }.sorted()
13+
}
14+
15+
var right: [Int] {
16+
lines.map { Int($0[1]) ?? 0 }.sorted()
17+
}
18+
1119
// add here any computed values useful for the challenge
1220

1321
func part1() -> Int {
14-
let listRight = lines.map { Int($0[0]) ?? 0 }.sorted()
15-
let listLeft = lines.map { Int($0[1]) ?? 0 }.sorted()
1622
var sum = 0
17-
for (one, two) in zip(listLeft, listRight) {
23+
for (one, two) in zip(left, right) {
1824
sum += abs(one - two)
1925
}
2026
return sum
2127
}
2228

2329
func part2() -> Int {
24-
let left = lines.map { Int($0[0]) ?? 0 }.sorted()
25-
let right = lines.map { Int($0[1]) ?? 0 }.sorted()
26-
2730
var similarity = 0
2831
for elem in left {
2932
let occurrences = right.filter { $0 == elem }.count

0 commit comments

Comments
 (0)