-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday7_crabs.py
66 lines (31 loc) · 950 Bytes
/
day7_crabs.py
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
#!/usr/bin/env python
# coding: utf-8
import numpy as np
data_fname = 'day7_crabs.txt'
with open(data_fname, 'r') as datafile:
positions = [int(l) for l in datafile.read().split(',')]
# # Part 1
positions = np.array(positions, dtype=int)
mean = np.median(positions)
mean
f1 = int(np.abs(positions - mean +1).sum())
f2 = int(np.abs(positions - mean).sum())
f3 = int(np.abs(positions - mean -1).sum())
min([f1, f2, f3])
f2
# # Part 2
print(positions.min())
print(positions.max())
# Each crab will have to end up somewhere between 0 and 1898 (let's say 2k)
0, 1, 1+2, 1+2+3, 1+2+3+4
for i in range(5):
print((i+1)*i*.5)
max_size = 2_000
costs = np.zeros((len(positions), max_size), dtype=int)
for crab, p in enumerate(positions):
i1 = np.arange(max_size-p)
costs[crab, p:] = (i1+1) * i1 * .5
i2 = np.arange(p, 0, -1)
costs[crab, :p] = (i2+1) * i2 * .5
costs[1, :10]
costs.sum(axis=0).min()