-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathday07.py
executable file
·71 lines (54 loc) · 1.13 KB
/
day07.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
63
64
65
66
67
68
69
70
71
#!/usr/bin/env python3
from utils.all import *
advent.setup(2021, 7)
if 'debug' not in map(str.lower, sys.argv):
fin = advent.get_input()
else:
fin = io.StringIO('''\
16,1,2
''')
eprint(*fin, sep='', end='----- end of input -----\n\n'); fin.seek(0, 0)
timer_start()
ans = 0
try: ints = read_ints(fin); fin.seek(0, 0)
except: pass
try: lines = get_lines(fin); fin.seek(0, 0)
except: pass
try: mat = read_char_matrix(fin); fin.seek(0, 0)
except: pass
def fuel(ints, x):
tot = 0
for i in ints:
tot += abs(i - x)
return tot
def fuel2(ints, x):
tot = 0
for i in ints:
start = min(x, i)
end = max(x, i)
m = end - start +1
lol = m * (m-1) // 2
tot += lol
return tot
minf, minx = INFINITY, INFINITY
for x in range(min(ints), max(ints) + 1):
f = fuel(ints, x)
print(x, f)
if f < minf:
minf = f
minx = x
ans = minf
print(minx)
advent.print_answer(1, ans)
# wait('Submit? ')
# advent.submit_answer(1, ans)
minf, minx = INFINITY, INFINITY
for x in range(min(ints), max(ints) + 1):
f = fuel2(ints, x)
if f < minf:
minf = f
minx = x
ans = minf
advent.print_answer(2, ans)
# wait('Submit? ')
# advent.submit_answer(2, ans)