-
Notifications
You must be signed in to change notification settings - Fork 0
/
day11-part1.swift
executable file
·65 lines (51 loc) · 1.32 KB
/
day11-part1.swift
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#!/usr/bin/swift
import Foundation
class Point: Hashable {
let x: Int
let y: Int
init(_ x: Int, _ y: Int) {
self.x = x
self.y = y
}
static func == (lhs: Point, rhs: Point) -> Bool {
return lhs.x == rhs.x && lhs.y == rhs.y
}
func hash(into hasher: inout Hasher) {
hasher.combine(x)
hasher.combine(y)
}
}
func calculatePowerLevel(_ row: Int, _ col: Int, _ serialNumber: Int) -> Int {
let x = row + 1
let y = col + 1
let rackID = x + 10
var powerLevel = (rackID * y + serialNumber) * rackID
powerLevel = ((powerLevel / 100) % 10) - 5
return powerLevel
}
func totalPower(_ x: Int, _ y: Int, _ grid: inout [Point : Int], _ serialNumber: Int) -> Int {
var result = 0
for i in 0..<3 {
for j in 0..<3 {
result += grid[Point(x + i, y + j), default: calculatePowerLevel(x + i, y + j, serialNumber)]
}
}
return result
}
var serialNumber = 9005
var powerValues: [Point : Int] = [:]
var maxPower = Int.min
var maxX = -1
var maxY = -1
for x in 0..<296 {
for y in 0..<296 {
let power = totalPower(x, y, &powerValues, serialNumber)
if power > maxPower {
maxPower = power
maxX = x
maxY = y
}
}
}
print("\(maxX + 1),\(maxY + 1)")
print(maxPower)