-
Notifications
You must be signed in to change notification settings - Fork 0
/
day11-part2.swift
executable file
·87 lines (69 loc) · 1.95 KB
/
day11-part2.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#!/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 y = row
let x = col
let rackID = x + 10
var powerLevel = (rackID * y + serialNumber) * rackID
powerLevel = ((powerLevel / 100) % 10) - 5
return powerLevel
}
var serialNumber = 9005
var powerValues: [Point : Int] = [:]
var totalPower: [Point : Int] = [:]
// Pre calculate power values
for y in 1...300 {
for x in 1...300 {
powerValues[Point(y, x)] = calculatePowerLevel(y, x, serialNumber)
}
}
for y in 1...300 {
for x in 1...300 {
var power = 0
power += powerValues[Point(y, x)]!
power += totalPower[Point(y - 1, x), default: 0]
power += totalPower[Point(y, x - 1), default: 0]
power -= totalPower[Point(y - 1, x - 1), default: 0]
totalPower[Point(y, x)] = power
}
}
var maxPower = Int.min
var maxX = -1
var maxY = -1
var finalSize = -1
for y in 1...300 {
for x in 1...300 {
// Stores the total power value of square from x,y to bottom right corner
let maxSquareSize = 300 - max(x, y)
for size in 0..<maxSquareSize {
var power = 0
power += totalPower[Point(y + size, x + size)]!
power += totalPower[Point(y, x)]!
power -= totalPower[Point(y + size, x)]!
power -= totalPower[Point(y, x + size)]!
if power > maxPower {
maxPower = power
maxX = x
maxY = y
finalSize = size
}
}
}
}
print("\(maxX + 1),\(maxY + 1),\(finalSize)")
print(maxPower)