-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDay01.swift
More file actions
34 lines (27 loc) · 775 Bytes
/
Day01.swift
File metadata and controls
34 lines (27 loc) · 775 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import AOCCore
import Foundation
struct Day01: Day {
let title = "Historian Hysteria"
var rawInput: String?
func part1() throws -> Int {
var left = input().lines.map(\.integers[0])
var right = input().lines.map(\.integers[1])
left.sort()
right.sort()
return zip(left, right)
.map(-)
.map(abs)
.sum
}
func part2() throws -> Int {
let left = input().lines.map(\.integers[0])
let right = input().lines.map(\.integers[1])
let counter = right
.reduce(into: [:]) { result, element in
result[element, default: 0] += 1
}
return left
.map { counter[$0, default: 0] * $0 }
.sum
}
}