trois premiers jours
This commit is contained in:
21
day1.py
Normal file
21
day1.py
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
import numpy as np
|
||||||
|
|
||||||
|
l = np.loadtxt("day1.txt")
|
||||||
|
# l = np.array([[3,4],
|
||||||
|
# [4,3],
|
||||||
|
# [2,5],
|
||||||
|
# [1,3],
|
||||||
|
# [3,9],
|
||||||
|
# [3,3]])
|
||||||
|
|
||||||
|
a,b = np.sort(l[:,0]),np.sort(l[:,1])
|
||||||
|
dist = np.abs(a - b)
|
||||||
|
tot = np.sum(dist)
|
||||||
|
# print(tot)
|
||||||
|
|
||||||
|
sim = 0
|
||||||
|
for i in a :
|
||||||
|
for j in b :
|
||||||
|
if i == j:
|
||||||
|
sim += i
|
||||||
|
print(sim)
|
||||||
62
day2.py
Normal file
62
day2.py
Normal file
@ -0,0 +1,62 @@
|
|||||||
|
import numpy as np
|
||||||
|
|
||||||
|
with open('day2.txt') as f:
|
||||||
|
l = [line.rstrip().split() for line in f]
|
||||||
|
|
||||||
|
# l = [[7, 6, 4, 2, 1],
|
||||||
|
# [1, 2, 7, 8, 9],
|
||||||
|
# [9, 7, 6, 2, 1],
|
||||||
|
# [1, 3, 2, 4, 5],
|
||||||
|
# [8, 6, 4, 4, 1],
|
||||||
|
# [1, 3, 6, 7, 9]]
|
||||||
|
|
||||||
|
print(l)
|
||||||
|
|
||||||
|
# l = np.loadtxt("day2.txt")
|
||||||
|
|
||||||
|
def test_if_safe(diff,inc):
|
||||||
|
# inc = np.sign(diff)
|
||||||
|
if abs(diff) > 3 or abs(diff) == 0:
|
||||||
|
print("Trop grand")
|
||||||
|
return False
|
||||||
|
elif np.sign(inc) != np.sign(diff):
|
||||||
|
print("Changement de sens")
|
||||||
|
return False
|
||||||
|
|
||||||
|
return True
|
||||||
|
|
||||||
|
def loop(list):
|
||||||
|
for k in range(len(list)-1):
|
||||||
|
diff2 = int(list[k+1])-int(list[k])
|
||||||
|
if k == 0:
|
||||||
|
inc2 = np.sign(diff2)
|
||||||
|
|
||||||
|
print("diff = ", diff2)
|
||||||
|
|
||||||
|
|
||||||
|
safe2 = test_if_safe(diff2,inc2)
|
||||||
|
if not safe2 :
|
||||||
|
break
|
||||||
|
inc2 = np.sign(diff2)
|
||||||
|
print("safe in loop", safe2)
|
||||||
|
return safe2
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
cnt = 0
|
||||||
|
|
||||||
|
for report in l:
|
||||||
|
incs = []
|
||||||
|
inc = 0
|
||||||
|
|
||||||
|
safe = True
|
||||||
|
print(report)
|
||||||
|
for k in range(len(report)):
|
||||||
|
report2 = report.copy()
|
||||||
|
report2.pop(k)
|
||||||
|
safe = loop(report2)
|
||||||
|
if safe :
|
||||||
|
cnt+=1
|
||||||
|
break
|
||||||
|
|
||||||
|
print("Cnt = ", cnt)
|
||||||
72
day3.py
Normal file
72
day3.py
Normal file
@ -0,0 +1,72 @@
|
|||||||
|
# data = "xmul(2,4)&mul[3,7]!^don't()_mul(5,5)+mul(32,64](mul(11,8)undo()?mul(8,5))"
|
||||||
|
|
||||||
|
PART1 = False
|
||||||
|
with open('day3.txt') as f:
|
||||||
|
l = [line.rstrip() for line in f]
|
||||||
|
data = ""
|
||||||
|
for line in l:
|
||||||
|
data += line
|
||||||
|
|
||||||
|
mul = []
|
||||||
|
|
||||||
|
if PART1:
|
||||||
|
|
||||||
|
for i in range(len(data)):
|
||||||
|
window = data[i:i+4]
|
||||||
|
print("window=",window)
|
||||||
|
if window == "mul(":
|
||||||
|
for j in range(i+4,min(i+4+8,len(data))):
|
||||||
|
print(data[j])
|
||||||
|
if data[j]==")":
|
||||||
|
to_test = data[i+4:j]
|
||||||
|
|
||||||
|
# print("to_test=", to_test)
|
||||||
|
to_test = to_test.split(",")
|
||||||
|
# print("to_test=", to_test)
|
||||||
|
if len(to_test)==2:
|
||||||
|
try :
|
||||||
|
a,b = int(to_test[0]),int(to_test[1])
|
||||||
|
print("ab = ", a, b)
|
||||||
|
mul.append(a*b)
|
||||||
|
|
||||||
|
|
||||||
|
except :
|
||||||
|
# print("not int")
|
||||||
|
pass
|
||||||
|
|
||||||
|
else :
|
||||||
|
do = True
|
||||||
|
for i in range(len(data)):
|
||||||
|
window_do = data[i:i+7]
|
||||||
|
print("window_do=",window_do)
|
||||||
|
if window_do == "don't()":
|
||||||
|
do = False
|
||||||
|
elif "do()" in window_do:
|
||||||
|
do = True
|
||||||
|
window = data[i:i+4]
|
||||||
|
print("window=",window)
|
||||||
|
if window== "mul(":
|
||||||
|
for j in range(i+4,min(i+4+8,len(data))):
|
||||||
|
print(data[j])
|
||||||
|
if data[j]==")":
|
||||||
|
to_test = data[i+4:j]
|
||||||
|
|
||||||
|
# print("to_test=", to_test)
|
||||||
|
to_test = to_test.split(",")
|
||||||
|
# print("to_test=", to_test)
|
||||||
|
if len(to_test)==2 and do:
|
||||||
|
try :
|
||||||
|
a,b = int(to_test[0]),int(to_test[1])
|
||||||
|
print("ab = ", a, b)
|
||||||
|
mul.append(a*b)
|
||||||
|
|
||||||
|
|
||||||
|
except :
|
||||||
|
print("not int")
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
print(mul)
|
||||||
|
tot = sum(mul)
|
||||||
|
|
||||||
|
print(tot)
|
||||||
Reference in New Issue
Block a user