From 3fdb680747eaccc60e8bc9a2086ab68bcbde23ee Mon Sep 17 00:00:00 2001 From: Marguerite Miallier Date: Sun, 8 Dec 2024 22:00:55 +0100 Subject: [PATCH] jour 7 qui marche pas --- day5.py | 71 +++++++++++++++++++++++++++++++++++++++++ day6.py | 98 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ day8.py | 73 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 242 insertions(+) create mode 100644 day5.py create mode 100644 day6.py create mode 100644 day8.py diff --git a/day5.py b/day5.py new file mode 100644 index 0000000..aea05ad --- /dev/null +++ b/day5.py @@ -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) \ No newline at end of file diff --git a/day6.py b/day6.py new file mode 100644 index 0000000..a3ba0eb --- /dev/null +++ b/day6.py @@ -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)) + diff --git a/day8.py b/day8.py new file mode 100644 index 0000000..c43cf4b --- /dev/null +++ b/day8.py @@ -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))