forked from lxylxy123456/AdventOfCode2024
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paths.py
More file actions
125 lines (114 loc) · 2.38 KB
/
s.py
File metadata and controls
125 lines (114 loc) · 2.38 KB
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# Youtube: https://youtu.be/g8bZojvOaUg
import argparse, math, sys, re, functools, operator, itertools, heapq
from collections import defaultdict, Counter, deque
#sys.setrecursionlimit(100000000)
#A = list(map(int, input().split()))
#T = int(input())
def read_lines(f):
while True:
line = f.readline()
if not line:
break
assert line[-1] == '\n'
yield line[:-1]
def main():
parser = argparse.ArgumentParser()
parser.add_argument('-1', '--one', action='store_true', help='Only part 1')
parser.add_argument('-2', '--two', action='store_true', help='Only part 2')
parser.add_argument('input_file', nargs='?')
args = parser.parse_args()
if args.input_file is not None:
f = open(args.input_file)
else:
f = sys.stdin
lines = list(read_lines(f))
if not args.two:
print(part_1(lines))
if not args.one:
print(part_2(lines))
def part_1(lines):
s = 0
line, = lines
f = []
s0 = 0
s1 = False
for i in line:
if s1:
s0 += 1
s1 = False
c = None
else:
s1 = True
c = s0
for _ in range(int(i)):
f.append(c)
p0 = 0
p1 = len(f) - 1
while p0 < p1:
if f[p0] is not None:
p0 += 1
continue
if f[p1] is None:
p1 -= 1
continue
f[p0], f[p1] = f[p1], f[p0]
for index, i in enumerate(f):
if i is not None:
s += index * i
return s
def part_2(lines):
s = 0
line, = lines
f = [] # (size, id)
s0 = 0
s1 = False
for i in line:
if s1:
s0 += 1
s1 = False
c = None
else:
s1 = True
c = s0
f.append([int(i), c])
f.append([0, None])
for fid in range(s0, -1, -1):
# Find block to be moved.
for index, (size, i) in enumerate(f):
if i == fid:
break
else:
raise ValueError('Not found')
old_index = index
old_size = size
# Find empty space to move to.
for index, (size, i) in enumerate(f):
if i is None and size >= old_size:
break
else:
continue
new_index = index
new_size = size
# Don't move to later.
if new_index > old_index:
continue
# Remove old.
assert f[old_index - 1][1] is None
assert f[old_index + 1][1] is None
f[old_index - 1][0] += f[old_index][0] + f[old_index + 1][0]
f.pop(old_index)
f.pop(old_index)
# Add new.
f[new_index][0] -= old_size
f.insert(new_index, [old_size, fid])
f.insert(new_index, [0, None])
pos = 0
for size, fid in f:
for _ in range(size):
if fid is not None:
s += pos * fid
pos += 1
# print(f)
return s
if __name__ == '__main__':
main()