Initial commit
This commit is contained in:
commit
97a4330bc0
110 changed files with 7006 additions and 0 deletions
41
2024/5/one.py
Normal file
41
2024/5/one.py
Normal file
|
@ -0,0 +1,41 @@
|
|||
#!/usr/bin/env python3
|
||||
|
||||
import sys
|
||||
|
||||
input_file = sys.argv[1]
|
||||
|
||||
with open(input_file) as fd:
|
||||
lines = [line.rstrip() for line in fd.readlines()]
|
||||
|
||||
page_mode = False
|
||||
orders: list[tuple[int, int]] = list()
|
||||
updates: list[list[int]] = list()
|
||||
|
||||
for line in lines:
|
||||
if not page_mode:
|
||||
if line == "":
|
||||
page_mode = True
|
||||
else:
|
||||
order = tuple(int(a) for a in line.split("|"))
|
||||
assert len(order) == 2
|
||||
orders.append(order)
|
||||
else:
|
||||
update = list(int(a) for a in line.split(","))
|
||||
updates.append(update)
|
||||
|
||||
total = 0
|
||||
for update in updates:
|
||||
for fi, se in orders:
|
||||
try:
|
||||
ifi = update.index(fi)
|
||||
ise = update.index(se)
|
||||
except ValueError:
|
||||
continue
|
||||
if ifi > ise:
|
||||
break
|
||||
else:
|
||||
imid = int(len(update)/2)
|
||||
mid = update[imid]
|
||||
total += mid
|
||||
print(total)
|
||||
|
57
2024/5/two.py
Normal file
57
2024/5/two.py
Normal file
|
@ -0,0 +1,57 @@
|
|||
#!/usr/bin/env python3
|
||||
|
||||
import sys
|
||||
|
||||
input_file = sys.argv[1]
|
||||
|
||||
with open(input_file) as fd:
|
||||
lines = [line.rstrip() for line in fd.readlines()]
|
||||
|
||||
page_mode = False
|
||||
orders: list[tuple[int, int]] = list()
|
||||
|
||||
class Page:
|
||||
def __init__(self, pn: int):
|
||||
self.pn = pn
|
||||
|
||||
def __lt__(self, other: "Page") -> bool:
|
||||
a = self.pn
|
||||
b = other.pn
|
||||
for fi, se in orders:
|
||||
if a == fi and b == se:
|
||||
return True
|
||||
elif a == se and b == fi:
|
||||
return False
|
||||
raise RuntimeError
|
||||
|
||||
|
||||
updates: list[list[Page]] = list()
|
||||
|
||||
for line in lines:
|
||||
if not page_mode:
|
||||
if line == "":
|
||||
page_mode = True
|
||||
else:
|
||||
order = tuple(int(a) for a in line.split("|"))
|
||||
assert len(order) == 2
|
||||
orders.append(order)
|
||||
else:
|
||||
update = list(Page(int(a)) for a in line.split(","))
|
||||
updates.append(update)
|
||||
|
||||
total = 0
|
||||
for update in updates:
|
||||
update_sorted = sorted(update)
|
||||
|
||||
if update == update_sorted:
|
||||
continue
|
||||
|
||||
update = update_sorted
|
||||
|
||||
# Add
|
||||
imid = int(len(update)/2)
|
||||
mid = update[imid]
|
||||
total += mid.pn
|
||||
|
||||
print(total)
|
||||
|
Loading…
Add table
Add a link
Reference in a new issue