jour 7 qui marche pas
This commit is contained in:
71
day5.py
Normal file
71
day5.py
Normal file
@ -0,0 +1,71 @@
|
||||
import math as m
|
||||
# l = ["47|53",
|
||||
# "97|13",
|
||||
# "97|61",
|
||||
# "97|47",
|
||||
# "75|29",
|
||||
# "61|13",
|
||||
# "75|53",
|
||||
# "29|13",
|
||||
# "97|29",
|
||||
# "53|29",
|
||||
# "61|53",
|
||||
# "97|53",
|
||||
# "61|29",
|
||||
# "47|13",
|
||||
# "75|47",
|
||||
# "97|75",
|
||||
# "47|61",
|
||||
# "75|61",
|
||||
# "47|29",
|
||||
# "75|13",
|
||||
# "53|13",
|
||||
# "",
|
||||
# "75,47,61,53,29",
|
||||
# "97,61,53,29,13",
|
||||
# "75,29,13",
|
||||
# "75,97,47,61,53",
|
||||
# "61,13,29",
|
||||
# "97,13,75,29,47"]
|
||||
|
||||
with open('aoc_2024/day5.txt') as f:
|
||||
l = f.read().splitlines()
|
||||
|
||||
|
||||
def extract_rules(l):
|
||||
rules = []
|
||||
data = []
|
||||
for line in l:
|
||||
if len(line) == 5:
|
||||
rules.append(line.split("|"))
|
||||
elif len(line)>0:
|
||||
data.append(line.split(","))
|
||||
return rules, data
|
||||
|
||||
def check_rule(line, rule):
|
||||
if rule[0] in line and rule[1] in line :
|
||||
if line.index(rule[0]) < line.index(rule[1]):
|
||||
return True
|
||||
else :
|
||||
return False
|
||||
return True
|
||||
|
||||
rules, data = extract_rules(l)
|
||||
print(rules, data)
|
||||
cnt_glob = 0
|
||||
for line in data:
|
||||
cnt = 0
|
||||
for rule in rules:
|
||||
ok = check_rule(line, rule)
|
||||
# print(rule, ok)
|
||||
if not ok:
|
||||
print(line, rule, ok)
|
||||
break
|
||||
|
||||
cnt +=1
|
||||
|
||||
if cnt == len(rules):
|
||||
mid = m.floor(len(line)/2)
|
||||
cnt_glob += int(line[mid])
|
||||
|
||||
print(cnt_glob)
|
||||
98
day6.py
Normal file
98
day6.py
Normal file
@ -0,0 +1,98 @@
|
||||
l = ["....#.....",
|
||||
".........#",
|
||||
"..........",
|
||||
"..#.......",
|
||||
".......#..",
|
||||
"..........",
|
||||
".#..^.....",
|
||||
"........#.",
|
||||
"#.........",
|
||||
"......#..."]
|
||||
|
||||
global up, down, left, right
|
||||
up,down,left,right = "^","v","<",">"
|
||||
dir = [up,down,left,right]
|
||||
act_dir = None
|
||||
global obstacles
|
||||
obstacles = []
|
||||
pose = None
|
||||
# global path
|
||||
path = []
|
||||
global h,w
|
||||
h,w = len(l),len(l[0])
|
||||
|
||||
def move_up(pose, path):
|
||||
print(path)
|
||||
out_of_map = False
|
||||
while pose[0] - 1 not in obstacles: #or pose[0] != 0:
|
||||
print(pose, path,pose in path)
|
||||
if pose not in path:
|
||||
path.append(pose)
|
||||
pose[0] = pose[0] - 1
|
||||
print(pose)
|
||||
if pose[0] == 0:
|
||||
out_of_map = True
|
||||
break
|
||||
print(path)
|
||||
act_dir = right
|
||||
return pose, act_dir, out_of_map, path
|
||||
|
||||
def move_down(pose):
|
||||
out_of_map = False
|
||||
while pose[0] + 1 not in obstacles or pose[0] == h-1:
|
||||
pose[0] = pose[0] + 1
|
||||
print(pose in path)
|
||||
if pose not in path:
|
||||
path.append(pose)
|
||||
if pose[0] == h-1:
|
||||
out_of_map = True
|
||||
act_dir = left
|
||||
return pose, act_dir, out_of_map
|
||||
|
||||
def move_left(pose):
|
||||
out_of_map = False
|
||||
while pose[1] - 1 not in obstacles or pose[1] == 0:
|
||||
pose[1] = pose[1] - 1
|
||||
if pose not in path:
|
||||
path.append(pose)
|
||||
if pose[1] == 0:
|
||||
out_of_map = True
|
||||
act_dir = up
|
||||
return pose, act_dir, out_of_map
|
||||
|
||||
def move_right(pose):
|
||||
out_of_map = False
|
||||
while pose[1] + 1 not in obstacles or pose[1] == w - 1:
|
||||
pose[1] = pose[1] + 1
|
||||
if pose not in path:
|
||||
path.append(pose)
|
||||
if pose[1] == w - 1:
|
||||
out_of_map = True
|
||||
act_dir = down
|
||||
return pose, act_dir, out_of_map
|
||||
|
||||
|
||||
for i in range(len(l)):
|
||||
for j in range(len(l[0])):
|
||||
if l[i][j] in dir:
|
||||
act_dir = l[i][j]
|
||||
pose = [i,j]
|
||||
path.append(pose)
|
||||
elif l[i][j] == "#":
|
||||
obstacles.append([i,j])
|
||||
print(obstacles)
|
||||
print(path)
|
||||
|
||||
out_of_map = False
|
||||
while not out_of_map:
|
||||
if act_dir == up:
|
||||
pose, act_dir, out_of_map, path = move_up(pose,path)
|
||||
elif act_dir == down:
|
||||
pose, act_dir, out_of_map = move_down(pose)
|
||||
elif act_dir == left:
|
||||
pose, act_dir, out_of_map = move_left(pose)
|
||||
elif act_dir == right:
|
||||
pose, act_dir, out_of_map = move_right(pose)
|
||||
|
||||
print(len(path))
|
||||
|
||||
73
day8.py
Normal file
73
day8.py
Normal file
@ -0,0 +1,73 @@
|
||||
import numpy as np
|
||||
from collections import Counter
|
||||
l = ["............",
|
||||
"........0...",
|
||||
".....0......",
|
||||
".......0....",
|
||||
"....0.......",
|
||||
"......A.....",
|
||||
"............",
|
||||
"............",
|
||||
"........A...",
|
||||
".........A..",
|
||||
"............",
|
||||
"............"]
|
||||
|
||||
with open('aoc_2024/day8.txt') as f:
|
||||
l = f.read().splitlines()
|
||||
freqs = {}
|
||||
positions = []
|
||||
nodes = []
|
||||
|
||||
def check_in_map(coords,l):
|
||||
if coords[0] >= 0 and coords[0] <= len(l) - 1:
|
||||
if coords[1] >= 0 and coords[1] <= len(l[0]) - 1:
|
||||
return True
|
||||
return False
|
||||
|
||||
|
||||
for i in range(len(l)):
|
||||
for j in range(len(l[i])):
|
||||
if l[i][j] != '.':
|
||||
freq = l[i][j]
|
||||
if freq not in freqs.keys():
|
||||
freqs[freq] = [[i,j]]
|
||||
else :
|
||||
freqs[freq].append([i,j])
|
||||
positions.append([i,j])
|
||||
print(freqs)
|
||||
|
||||
for freq in freqs.keys():
|
||||
for index,coords in enumerate(freqs[freq]):
|
||||
for index2,coords2 in enumerate(freqs[freq]):
|
||||
if index!=index2:
|
||||
diff = np.array(coords2) - np.array(coords)
|
||||
print(freq,diff)
|
||||
# node1 = (coords - diff)
|
||||
# node2 = (coords2 + diff)
|
||||
# in_map1 = check_in_map(node1,l)
|
||||
# in_map2 = check_in_map(node2,l)
|
||||
# if in_map1 and node1.tolist() not in nodes:
|
||||
# nodes.append(node1.tolist())
|
||||
# if in_map2 and node2.tolist() not in nodes :
|
||||
# nodes.append(node2.tolist())
|
||||
# print("nodes",node1,node2)
|
||||
# print("list nodes", nodes)
|
||||
k = 1
|
||||
in_map = True
|
||||
while in_map:
|
||||
node = coords-k*diff
|
||||
in_map = check_in_map(node,l)
|
||||
if in_map and node.tolist() not in nodes:
|
||||
nodes.append(node.tolist())
|
||||
k+=1
|
||||
in_map = True
|
||||
k = 0
|
||||
while in_map:
|
||||
node = coords+k*diff
|
||||
in_map = check_in_map(node,l)
|
||||
if in_map and node.tolist() not in nodes:
|
||||
nodes.append(node.tolist())
|
||||
k+=1
|
||||
|
||||
print(len(nodes))
|
||||
Reference in New Issue
Block a user