Compare commits
7 Commits
59667eac06
...
main
Author | SHA1 | Date | |
---|---|---|---|
a2f903affc | |||
c1e2a28ac5 | |||
5c6f10c81e | |||
6eff8af332 | |||
067decada0 | |||
df55eec6d8 | |||
199bc88505 |
38
d09/part1.py
38
d09/part1.py
@@ -1,38 +0,0 @@
|
|||||||
import sys
|
|
||||||
import networkx as nx
|
|
||||||
|
|
||||||
G = nx.Graph()
|
|
||||||
D = {}
|
|
||||||
for l in sys.stdin.readlines():
|
|
||||||
l = l[:-1]
|
|
||||||
I = l.split(' ')
|
|
||||||
G.add_edge(I[0], I[2], weight=int(I[4]))
|
|
||||||
#G.add_edge(I[2], I[0], weight=int(I[4]))
|
|
||||||
D[(I[0], I[2])]=int(I[4])
|
|
||||||
D[(I[2], I[0])]=int(I[4])
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def dfs_visit(graph, node, visited):
|
|
||||||
visited[node] = True
|
|
||||||
for neighbor in graph.neighbors(node):
|
|
||||||
if not visited[neighbor]:
|
|
||||||
dfs_visit(graph, neighbor, visited)
|
|
||||||
|
|
||||||
def dfs(graph):
|
|
||||||
visited = node: False for node in graph.nodes
|
|
||||||
for node in graph.nodes:
|
|
||||||
if not visited[node]:
|
|
||||||
dfs_visit(graph, node, visited)
|
|
||||||
|
|
||||||
|
|
||||||
# Call the DFS function and visit all nodes exactly once
|
|
||||||
dfs(G)
|
|
||||||
|
|
||||||
tsp = nx.approximation.traveling_salesman_problem
|
|
||||||
|
|
||||||
L = tsp(G)#, cycle=False)
|
|
||||||
print(L)
|
|
||||||
S = sum(D[(a,b)] for (a,b) in zip(L[1:],L[2:]))
|
|
||||||
|
|
||||||
print(S)
|
|
22
d09/run.py
Normal file
22
d09/run.py
Normal file
@@ -0,0 +1,22 @@
|
|||||||
|
import sys
|
||||||
|
from itertools import permutations as perm
|
||||||
|
|
||||||
|
C = set() # cities
|
||||||
|
D = dict() # distances
|
||||||
|
|
||||||
|
for line in sys.stdin.readlines():
|
||||||
|
c1, _, c2, _, d = line[:-1].split()
|
||||||
|
d = int(d)
|
||||||
|
D[(c1, c2)] = d
|
||||||
|
D[(c2, c1)] = d
|
||||||
|
C.add(c1)
|
||||||
|
C.add(c2)
|
||||||
|
|
||||||
|
S = 999999
|
||||||
|
L = 0
|
||||||
|
for r in perm(C): # brut force all possible routes
|
||||||
|
l = sum(D[(c1, c2)]
|
||||||
|
for c1, c2 in zip(r, r[1:]))
|
||||||
|
S = min(l, S)
|
||||||
|
L = max(l, L)
|
||||||
|
print(S, L)
|
7
d10/run.py
Normal file
7
d10/run.py
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
import sys
|
||||||
|
from itertools import groupby as gb
|
||||||
|
|
||||||
|
r = sys.argv[1]
|
||||||
|
for _ in range(int(sys.argv[2])):
|
||||||
|
r = "".join(f'{n}{c}' for c,n in map(lambda x: (x[0],len(list(x[1]))), gb(r)))
|
||||||
|
print(len(r))
|
40
d11/run.py
Normal file
40
d11/run.py
Normal file
@@ -0,0 +1,40 @@
|
|||||||
|
import sys
|
||||||
|
|
||||||
|
letters = 'abcdefghijklmnopqrstuvwxyz'
|
||||||
|
doubles = [c+c for c in letters]
|
||||||
|
straights = [a+b+c for a,b,c in zip(letters, letters[1:], letters[2:])]
|
||||||
|
next_letter = {c1: c2 for c1, c2 in zip(letters, letters[1:]+'a')}
|
||||||
|
|
||||||
|
|
||||||
|
def is_valid(s):
|
||||||
|
# cannot contain i, o, or l
|
||||||
|
if 'i' in s or 'o' in s or 'l' in s:
|
||||||
|
return False
|
||||||
|
# must include two different pairs of letters
|
||||||
|
if sum([d in s for d in doubles]) < 2:
|
||||||
|
return False
|
||||||
|
# must include a straight of length 3 or greater
|
||||||
|
if not any([d in s for d in straights]):
|
||||||
|
return False
|
||||||
|
return True
|
||||||
|
|
||||||
|
|
||||||
|
def next_password(s):
|
||||||
|
s = s[:-1] + next_letter[s[-1]] # increment the last letter
|
||||||
|
for i in range(-1, -8, -1):
|
||||||
|
if s[i] == 'a': # increment n-1 letter is n letter changed to 'a'
|
||||||
|
s = s[:i-1] + next_letter[s[i-1]] + s[i:]
|
||||||
|
else:
|
||||||
|
break
|
||||||
|
return s
|
||||||
|
|
||||||
|
|
||||||
|
password = sys.argv[1]
|
||||||
|
while not is_valid(password):
|
||||||
|
password = next_password(password)
|
||||||
|
print(password)
|
||||||
|
|
||||||
|
password = next_password(password)
|
||||||
|
while not is_valid(password):
|
||||||
|
password = next_password(password)
|
||||||
|
print(password)
|
3
d12/cmd.txt
Normal file
3
d12/cmd.txt
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
|
||||||
|
jq '.' input | grep -o -E '\-?[0-9]+' | sum0
|
||||||
|
|
23
d12/run.py
Normal file
23
d12/run.py
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
import sys
|
||||||
|
import json
|
||||||
|
|
||||||
|
|
||||||
|
def sum_of_item(item, skip_red=False):
|
||||||
|
|
||||||
|
if isinstance(item, list):
|
||||||
|
return sum(sum_of_item(i, skip_red) for i in item)
|
||||||
|
|
||||||
|
if isinstance(item, dict):
|
||||||
|
if skip_red and 'red' in item.values():
|
||||||
|
return 0
|
||||||
|
return sum(sum_of_item(i, skip_red) for i in item.values())
|
||||||
|
|
||||||
|
if isinstance(item, str):
|
||||||
|
return 0
|
||||||
|
|
||||||
|
if isinstance(item, int):
|
||||||
|
return item
|
||||||
|
|
||||||
|
abacus = json.load(sys.stdin)
|
||||||
|
print(sum_of_item(abacus))
|
||||||
|
print(sum_of_item(abacus, skip_red=True))
|
26
d13/run.py
Normal file
26
d13/run.py
Normal file
@@ -0,0 +1,26 @@
|
|||||||
|
import sys, os
|
||||||
|
from collections import defaultdict as dd
|
||||||
|
from itertools import permutations as PERM
|
||||||
|
|
||||||
|
N:set[str] = set()
|
||||||
|
D:dict[tuple[str,str],int] = dd(int)
|
||||||
|
|
||||||
|
for l in sys.stdin.readlines():
|
||||||
|
[a,s,b] = l[:-1].split(" ")
|
||||||
|
D[(a,b)] += int(s)
|
||||||
|
D[(b,a)] += int(s)
|
||||||
|
N.add(a)
|
||||||
|
N.add(b)
|
||||||
|
|
||||||
|
if len(sys.argv)>1 and sys.argv[1]=="2":
|
||||||
|
# add you
|
||||||
|
for a in N:
|
||||||
|
D[(a,'Y')] = 0
|
||||||
|
D[('Y',a)] = 0
|
||||||
|
N.add('Y')
|
||||||
|
|
||||||
|
def score(chain:list[str]) -> int:
|
||||||
|
return sum(D[(a,b)] for (a,b) in zip(chain, chain[1:])) \
|
||||||
|
+ D[(chain[0],chain[-1])]
|
||||||
|
|
||||||
|
print(max(score(p) for p in PERM(N)))
|
41
d14/run.py
Normal file
41
d14/run.py
Normal file
@@ -0,0 +1,41 @@
|
|||||||
|
from math import log10
|
||||||
|
import sys, os
|
||||||
|
from dataclasses import dataclass
|
||||||
|
|
||||||
|
|
||||||
|
@dataclass
|
||||||
|
class Rein:
|
||||||
|
name: str
|
||||||
|
speed: int
|
||||||
|
duration: int
|
||||||
|
rest: int
|
||||||
|
distance: int = 0
|
||||||
|
score: int = 0
|
||||||
|
|
||||||
|
D:list[Rein] = []
|
||||||
|
|
||||||
|
for l in sys.stdin.readlines():
|
||||||
|
L = l[:-1].split(" ")
|
||||||
|
D.append(Rein(L[0],int(L[3]),int(L[6]),int(L[13])))
|
||||||
|
|
||||||
|
howlong = int(sys.argv[1])
|
||||||
|
|
||||||
|
lead_distance = 0
|
||||||
|
for t in range(howlong):
|
||||||
|
# increment each rein
|
||||||
|
for rein in D:
|
||||||
|
period = rein.duration + rein.rest
|
||||||
|
(p, m) = divmod(t, period)
|
||||||
|
# is flying or at rest ?
|
||||||
|
if m < rein.duration:
|
||||||
|
rein.distance += rein.speed
|
||||||
|
lead_distance = max(lead_distance, rein.distance)
|
||||||
|
# compute score
|
||||||
|
for rein in D:
|
||||||
|
if rein.distance == lead_distance:
|
||||||
|
rein.score += 1
|
||||||
|
# part1
|
||||||
|
print(lead_distance)
|
||||||
|
# part2
|
||||||
|
print(max(rein.score for rein in D))
|
||||||
|
|
40
d15/run.py
Normal file
40
d15/run.py
Normal file
@@ -0,0 +1,40 @@
|
|||||||
|
from dataclasses import dataclass
|
||||||
|
import sys
|
||||||
|
|
||||||
|
@dataclass
|
||||||
|
class Ing:
|
||||||
|
capacity: int
|
||||||
|
durability: int
|
||||||
|
flavor: int
|
||||||
|
texture: int
|
||||||
|
calorie: int
|
||||||
|
|
||||||
|
def __add__(self, j):
|
||||||
|
return Ing(self.capacity+j.capacity,self.durability+j.durability, self.flavor+j.flavor, self.texture+j.texture,self.calorie+j.calorie)
|
||||||
|
|
||||||
|
def pIng(self, p:int):
|
||||||
|
return Ing(self.capacity*p, self.durability*p, self.flavor*p, self.texture*p, self.calorie*p)
|
||||||
|
|
||||||
|
def score(self):
|
||||||
|
if self.capacity<0 or self.durability<0 or self.flavor<0 or self.texture<0:
|
||||||
|
return 0
|
||||||
|
return self.capacity * self.durability * self.flavor * self.texture
|
||||||
|
|
||||||
|
L:list[Ing] = list()
|
||||||
|
|
||||||
|
for l in sys.stdin.readlines():
|
||||||
|
l = l.replace(",","").split(" ")
|
||||||
|
L.append(Ing(int(l[2]),int(l[4]),int(l[6]),int(l[8]),int(l[10])))
|
||||||
|
|
||||||
|
M1 = M2 = 0
|
||||||
|
for p1 in range(1,98):
|
||||||
|
for p2 in range(1,98):
|
||||||
|
for p3 in range(1,98):
|
||||||
|
p4 = 100-(p1+p2+p3)
|
||||||
|
i:Ing = L[0].pIng(p1)+L[1].pIng(p2)+L[2].pIng(p3)+L[3].pIng(p4)
|
||||||
|
s = i.score()
|
||||||
|
M1 = max(M1, s)
|
||||||
|
if i.calorie == 500:
|
||||||
|
M2 = max(M2, s)
|
||||||
|
|
||||||
|
print(M1, M2)
|
Reference in New Issue
Block a user