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)
|
||||
Reference in New Issue
Block a user