-
Notifications
You must be signed in to change notification settings - Fork 1
/
day7.toit
29 lines (24 loc) · 928 Bytes
/
day7.toit
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
import .file as file
import reader show BufferedReader
main:
reader := BufferedReader
file.Stream.for_read "7.txt"
crabs := (reader.read_line.split ",").map: int.parse it
left := crabs.reduce: | a b | min a b
right := crabs.reduce: | a b | max a b
best_cost := crabs.reduce --initial=0: | sum x | sum + (x - right).abs
for i := left; i < right; i++:
cost := crabs.reduce --initial=0: | sum x | sum + (x - i).abs
best_cost = min cost best_cost
print best_cost
// This is a block constant. It implements the fact that 1 + 2 + ... + n ==
// n * (n + 1) / 2.
cost_block := : | sum x i |
a := (x - i).abs
cost := (a * (a + 1)) / 2
sum + cost
best_cost2 := crabs.reduce --initial=0: | sum x | cost_block.call sum x right
for i := left; i < right; i++:
cost := crabs.reduce --initial=0: | sum x | cost_block.call sum x i
best_cost2 = min cost best_cost2
print best_cost2