-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday11.nim
41 lines (33 loc) · 1.1 KB
/
day11.nim
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
import strformat, threadpool
const
input = 4172
gridLimit = 300
type
Solution = tuple[x, y, size, sum: int]
Grid = array[gridLimit+1, array[gridLimit+1, int]]
let coordinates = 1 .. gridLimit
var cells, sums: Grid
for y in coordinates:
for x in coordinates:
let power = ((x + 10) * y + input) * (x + 10) div 100 mod 10 - 5
cells[y][x] = power
sums[y][x] = power + sums[y-1][x] + sums[y][x-1] - sums[y-1][x-1]
proc mostPowerfulSquare(size = 3): Solution =
for y in size .. gridLimit:
for x in size .. gridLimit:
let sum = sums[y][x] - sums[y-size][x] - sums[y][x-size] + sums[y-size][x-size]
if sum > result.sum:
result = (x-size+1, y-size+1, size, sum)
proc mostPowerfulSquareOfAnySize(): Solution =
var solutions: array[gridLimit, FlowVar[Solution]]
for size in coordinates:
solutions[size-1] = spawn mostPowerfulSquare(size)
for s in solutions:
let res = ^s
if res.sum > result.sum:
result = res
let
first = mostPowerfulSquare()
second = mostPowerfulSquareOfAnySize()
echo fmt"{first.x},{first.y}"
echo fmt"{second.x},{second.y},{second.size}"