• # Python sans brûlage de CPU ni explosion de RAM

    Posté par . En réponse au message [Doublon] Advent of Code 2023 : Day 5. Évalué à 1. Dernière modification le 05 décembre 2023 à 16:01.

    Une solution dont je ne suis pas peux fier, puisqu’elle est économe en ressource et prend moins d'une seconde à terminer. Et j'ai enfin commencé à utiliser la POO :

    #!/bin/python3
    class Range:
     def __init__(self,start,end,pas=0):
     if end <= start:
     raise ValueError
     self.start = start
     self.end = end
     self.pas = pas
     def contain(self, n):
     if type(n) == Range:
     if self.start <= n.start and self.end >= n.end:
     return True
     return False
     if self.start <= n and n < self.end:
     return True
     return False
     def ins(self, thisrange):
     if thisrange.contain(self.start) and thisrange.contain(self.end-1):
     return True
     return False
     def before(self, thisrange):
     if thisrange.contain(self.start) == False and thisrange.contain(self.end-1):
     return True
     return False
     def after(self, thisrange):
     if thisrange.contain(self.start) and thisrange.contain(self.end-1) == False:
     return True
     return False
     def not_ins(self, thisrange):
     if thisrange.contain(self.start) == False and thisrange.contain(self.end-1) == False and thisrange.contain(self) == False:
     return True
     return False
     def apply(self, thisrange):
     if self.pas == 0:
     return ValueError
     thisrange.start = thisrange.start + self.pas
     thisrange.end = thisrange.end + self.pas
     return thisrange
    def get_seeds(seeds):
     seeds = seeds.split(":")[1].split()
     for s in range(len(seeds)):
     seeds[s] = int(seeds[s])
     return seeds
    def get_seeds_range(seeds):
     seeds_range = []
     seeds = get_seeds(seeds)
     for i in range(0,len(seeds),2):
     seeds_range.append(Range(seeds[i],seeds[i]+seeds[i+1]))
     return seeds_range
    def mapping(almanac,seedlist):
     changed = [False for i in range(len(seedlist))]
     almanac = almanac.split(":")[1].split("\n")[1:]
     for c in almanac:
     if c == "" :
     continue
     c = c.split()
     c = [int(i) for i in c]
     for i, s in enumerate(seedlist):
     if s >= c[1] and s < c[1]+c[2] and changed[i] == False:
     newvalue = c[0]+s-c[1]
     seedlist[i] = newvalue
     changed[i] = True
     return seedlist
    def mapping_range(almanac, seedrange):
     almanac = almanac.split(":")[1].split("\n")[1:]
     ranges = []
     for c in almanac:
     if c == "" :
     continue
     c = c.split()
     c = [int(i) for i in c]
     ranges.append(Range(c[1],c[1]+c[2],c[0]-c[1]))
     iterations = 0
     while iterations < len(seedrange):
     changed = [False for i in range(len(seedrange))]
     for i in range(iterations,len(seedrange)):
     s = seedrange[i]
     for r in ranges:
     if changed[i]:
     continue
     if s.ins(r):
     seedrange[i] = r.apply(s)
     changed[i] = True
     elif s.before(r):
     seedrange.append(Range(s.start,r.start))
     seedrange[i] = r.apply(Range(r.start, s.end))
     changed[i] = True
     elif s.after(r):
     seedrange.append(Range(r.end,s.end))
     seedrange[i] = r.apply(Range(s.start, r.end))
     changed[i] = True
     elif s.contain(r):
     seedrange.append(Range(s.start,r.start))
     seedrange.append(Range(r.end, s.end))
     seedrange[i] = r.apply(Range(r.start, r.end))
     changed[i] = True
     elif s.not_ins(r):
     pass
     else:
     print("Maybe Error with range",r.start,r.end,r.pas,"and seedrange",s.start,s.end)
     iterations += 1
     return seedrange
    def solve1(puzzle,testing=False):
     s=0
     seeds = get_seeds(puzzle[0])
     for changes in puzzle[1:]:
     seeds = mapping(changes, seeds)
     s = min(seeds)
     if testing:
     print(s)
     return s
    def solve2(puzzle,testing=False):
     s = 0
     seeds = get_seeds_range(puzzle[0])
     for changes in puzzle[1:]:
     seeds = mapping_range(changes, seeds)
     s = seeds[0].start
     for r in seeds:
     if r.start < s:
     s = r.start
     if testing:
     print(s)
     return s
    test1 = """seeds: 79 14 55 13
    seed-to-soil map:
    50 98 2
    52 50 48
    soil-to-fertilizer map:
    0 15 37
    37 52 2
    39 0 15
    fertilizer-to-water map:
    49 53 8
    0 11 42
    42 0 7
    57 7 4
    water-to-light map:
    88 18 7
    18 25 70
    light-to-temperature map:
    45 77 23
    81 45 19
    68 64 13
    temperature-to-humidity map:
    0 69 1
    1 0 69
    humidity-to-location map:
    60 56 37
    56 93 4
    """
    result1 = 35
    test2 = test1
    result2 = 46
    def solve(short=False):
     print("----Part 1----")
     if short == False:
     if solve1(test1.split("\n\n"),testing=True) != result1:
     print("Not working.")
     return False
     else :
     print("Maybe working?")
     with open("input.txt",'r') as file:
     lines = file.read().split("\n\n")
     s1 = solve1(lines)
     print(s1)
     print("----Part 2----")
     if short == False:
     if solve2(test2.split("\n\n"),testing=True) != result2:
     print("Not working.")
     return False
     else :
     print("Maybe working?")
     with open("input.txt",'r') as file:
     lines = file.read().split("\n\n")
     s2 = solve2(lines)
     print(s2)
     return s1, s2
    if __name__ == "__main__":
     from sys import argv
     try:
     if argv[1] == "--summary" or argv[1] == "-s":
     solve(short=True)
     except IndexError:
     solve()

    L'informatique n'est pas une science exacte, on n'est jamais à l'abri d'un succès