import inputAoC file = inputAoC.get_input_file(1, 2020) x = list(map(int, file.splitlines())) # x = [int(nb) for nb in file.splitlines()] for nb1 in x: nb2 = 2020 - nb1 if nb2 in x: res1 = nb1 * nb2 break print(res1) for i in range(len(x)): nb1 = x[i] objectif = 2020 - nb1 for j in range(i + 1, len(x)): nb2 = x[j] nb3 = objectif - nb2 if nb3 in x[j + 1:]: res2 = nb1 * nb2 * nb3 # nb1, nb2, nb3 break print(res2)
import inputAoC as aoc import my_utils ex = """0,3,6""" start = [int(nb) for nb in aoc.get_input_file(15,2020).split(",")] start_ex = [int(nb) for nb in ex.split(",")] def turn1(start): last = start[-1] new = 0 if last in start[:-1]: new = len(start)-1 - my_utils.rindex(start[:-1],last) start.append(new) return new def play1(start, n=2020): for i in range(len(start), n): turn1(start) # print(i,turn1(start)) return start[-1] def turn2(mem, last_elem, turn): if last_elem in mem: new = turn - mem[last_elem] mem[last_elem] = turn else: new = 0 mem[last_elem] = turn #print("last_elem =", last_elem,"c'était le tour", turn,"(+1 cf tour 0)") #print(mem)
6 10 3 18 5 1 12 20 15 19 3 15 0 2 22 9 18 13 17 5 19 8 7 25 23 20 11 10 24 4 14 21 16 12 6 14 21 17 24 4 10 16 15 9 19 18 8 23 26 20 22 11 13 6 5 2 0 12 3 7""" input = ex input = aoc.get_input_file(4,2021) input = input.split('\n\n') ### Récolte et mise en forme des données numbers = list(map(int,input[0].split(','))) boards = input[1:] for x in range(len(boards)): board = boards[x].split('\n') for i in range(len(board)): row = board[i].strip().replace(" ", " ").split(" ") board[i] = list(map(int,row)) boards[x] = board #print(numbers) for board in boards:
n avec e -|- w s Les voisins de (x,y) sont : nw=(x,y-1), ne=(x+1,y-1), e=(x+1,y), se=(x,y+1), sw=(x-1,y+1), w=(x-1,y) Par exemple, les six voisins de (0,0) sont : (0,-1), (1,-1), (1,0), (0,1), (-1,1), (-1,0) hex_grid est un dict de dict, afin d'avoir des coordonnées négatives """ import inputAoC as aoc import my_utils raw_tiles = aoc.get_input_file(24,2020).splitlines() raw_tiles_ex = """sesenwnenenewseeswwswswwnenewsewsw neeenesenwnwwswnenewnwwsewnenwseswesw seswneswswsenwwnwse nwnwneseeswswnenewneswwnewseswneseene swweswneswnenwsewnwneneseenw eesenwseswswnenwswnwnwsewwnwsene sewnenenenesenwsewnenwwwse wenwwweseeeweswwwnwwe wsweesenenewnwwnwsenewsenwwsesesenwne neeswseenwwswnwswswnw nenwswwsewswnenenewsenwsenwnesesenew enewnwewneswsewnwswenweswnenwsenwsw sweneswneswneneenwnewenewwneswswnese swwesenesewenwneswnwwneseswwne enesenwswwswneneswsenwnewswseenwsese
import inputAoC as aoc combien, input = 150, aoc.get_input_file(17, 2015) #combien, input = 25, "20\n15\n10\n5\n5" input = list(map(int, input.splitlines())) def resultats(combien, nombres): lens = [] for i in range(2**len(nombres)): x = bin(i)[2:] #pour enlever le '0b' du début x = '0' * (len(nombres) - len(x)) + x #pour remettre les 0 non significatifs somme = 0 for i in range(len(nombres)): if x[i] == '1': somme += nombres[i] if somme == combien: lens.append(sum([int(b) for b in x])) res1 = len(lens) res2 = lens.count(min(lens)) return res1, res2 res1, res2 = resultats(combien, input) print(res1, res2)
import inputAoC as aoc from math import prod ex = """Butterscotch: capacity -1, durability -2, flavor 6, texture 3, calories 8 Cinnamon: capacity 2, durability 3, flavor -2, texture -1, calories 3""" instrs = aoc.get_input_file(15, 2015).split("\n") instrs_ex = ex.split("\n") class Ingredient: def __init__(self, instr: str): instr = instr.replace(": ", ", ") props = [prop.split() for prop in instr.split(", ")] self.name = props[0][0] self.capacity = int(props[1][1]) self.durability = int(props[2][1]) self.flavor = int(props[3][1]) self.texture = int(props[4][1]) self.calories = int(props[5][1]) def get_proprietes(self) -> [int]: proprietes = [] proprietes.append(self.capacity) proprietes.append(self.durability) proprietes.append(self.flavor) proprietes.append(self.texture) proprietes.append(self.calories) return proprietes def print(self) -> str:
import inputAoC as aoc from collections import deque initial_decks = aoc.get_input_file(22, 2020).split("\n\n") deck1 = [int(card) for card in initial_decks[0].splitlines()[1:]] deck2 = [int(card) for card in initial_decks[1].splitlines()[1:]] class Deck: """Objet qui représente une p(f)ile de cartes""" def __init__(self, initial_deck: [int]): self.deck = deque(initial_deck) def play(self) -> int: assert self.is_playable() return self.deck.popleft() def is_playable(self) -> bool: return len(self.deck) > 0 def win(self, card1, card2) -> None: # on a card1 = carte_gagnante """if card1 < card2: card1, card2 = card2, card1""" self.deck.append(card1) self.deck.append(card2) def score(self, indice=1, init=0) -> int: if len(self.deck) == 0: return init last_card = self.deck.pop() return self.score(indice + 1, init + last_card * indice)
import inputAoC input = inputAoC.get_input_file(3).split("\n") height,width = len(input),len(input[0]) res = 0 for i in range(height): res += input[i][3*i%width] == "#" print(res) ratios = ((1,1), (1,5), (1,7), (2,1)) #, (1,3)) #res = 1 for ratio in ratios: res_ratio = 0 for lig in range(ratio[0], height, ratio[0]): col = ratio[1] * lig // ratio[0] res_ratio += input[lig][col % width] == "#" #print(res_ratio) res *= res_ratio print(res)
import inputAoC as aoc start = aoc.get_input_file(10, 2015) print(start) def look_and_say(string): res = "" n = len(string) indice = 0 while indice < n: carac = string[indice] compt = 1 while indice + compt < n and string[indice + compt] == carac: compt += 1 res += str(compt) + carac indice += compt return res """# Fonction itérative, avec une réécriture de string res = "" while string: carac = string[0] n = len(string) compt = 1 while compt < n and string[compt] == carac: compt += 1 res += str(compt) + carac string = string[compt:] return res""" """# Fonction récursive:
import inputAoC as aoc ex = """forward 5 down 5 forward 8 up 3 down 8 forward 2""" data = aoc.get_input_file(2,2021).splitlines() #data = ex.splitlines() instrs = [instr.split(" ") for instr in data] horizontal = 0 depth = 0 for instr in instrs: dir, x = instr[0], int(instr[1]) if dir == "down": depth += x elif dir == "up": depth -= x elif dir == "forward": horizontal += x res1 = horizontal*depth print(res1) #horizontal = 0 #Ca sera la même distance horizontale depth = 0 aim = 0 for instr in instrs: dir, x = instr[0], int(instr[1]) if dir == "down":
import inputAoC as aoc previous_password = aoc.get_input_file(11, 2015) def plusplus(letter): if letter == "z": return "a" return chr(ord(letter) + 1) def increase(word): letters = [letter for letter in word] last = letters.pop() comptZ = 0 while letters and last == "z": comptZ += 1 last = letters.pop() last = plusplus(last) letters.append(last) res = "".join(letters) + "a" * comptZ return res def tests_increase(): assert increase("xx") == "xy" assert increase("xz") == "ya" assert increase("xx") == "xy" assert increase("zz") == "aa" assert increase("abc") == "abd" assert increase("abzzz") == "acaaa"
import inputAoC as aoc instrs = aoc.get_input_file(8,2020).split("\n") acc1 = 0 i1 = 0 indexes1 = [] while i1 not in indexes1: indexes1.append(i1) instr = instrs[i1].split(" ") if instr[0] == "acc": acc1 += int(instr[1]) i1 += 1 elif instr[0] == "jmp": i1 += int(instr[1]) else: # instr[0] == "nop" i1 += 1 print(acc1) def search_loop(etat) -> (bool,dict): acc = etat["acc"] i = etat["i"] indexes = [i for i in etat["indexes"]] try: while i not in indexes: indexes.append(i) instr = instrs[i].split(" ") if instr[0] == "acc":
import inputAoC as aoc from collections import deque PREAMBULE = 25 nbs = [int(i) for i in aoc.get_input_file(9, 2020).split("\n")] def is_sum(val, nbs): for elem in nbs: if val - elem in nbs: return True return False res1 = int() for index, val in list(enumerate(nbs))[PREAMBULE:]: if not is_sum(val, nbs[index - PREAMBULE:index]): res1 = val break print(res1) res2 = int() acc = 0 queue = deque() index = -1 while not acc == res1: index += 1 elem = nbs[index] acc += elem queue.append(elem) while acc > res1:
def get_wire(instrs, wire): values = dict() instrs = instrs[::] repeat = True while repeat: repeat = False for instr in instrs: i = instr.index(" -> ") expr, value = instr[:i], instr[i + 4:] #len(" -> ") # print(expr, value) val = get_value(expr, values) if val != None: values[value] = val instrs.remove(instr) repeat = True # my_utils.print_dict(values) return values[wire] instrs = aoc.get_input_file(7, 2015).splitlines() res1 = get_wire(instrs, 'a') print(res1) res_b = get_wire(instrs, "b") instrs.remove(str(res_b) + " -> b") instrs.append(str(res1) + " -> b") res2 = get_wire(instrs, 'a') print(res2)
import inputAoC as aoc file = aoc.get_input_file(3, 2015) #file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^v<v^^v<>^^vv<>v<>v>^vv^><v^<<^<>^<>^^^>v^>>>v><<^>>v<^v<>^^<v<><v^v<v>v<><v<vv><<>v<^<^>v<>v^>v>^^<<<^^vv^<><<<>>v>^^<>v>>>><v<v<^^^v<v<v^><<>v^v<>v>><<<<v^<><^<<^>^<vvv<v^^v>>v^vv^><^v^^<>^^><<v^>>vv>^<v^vv<^^v<>>vvv<^v^>>^<v<v>>^>^^<<^>^>^v><>>^<^^v>^>>^^<><>>>^^>^^vvv>v<^^<>v^v^^<v<<^<v^v^<<>v^v<v<<v<>>><<^^^>>v>^vv>^>^^v<>^^<>v^^<><v<v<vvv^<vv<<>v^><<><v<>vv<<^vvvv><<<v>v>v^>v^<>v^>^<v<vvv^>^<>^>^^v<>><<<><v<^^>^v<v>^^v^v<<<^v^<>^<>v>^^>v<v<v>v>^^<<<><<^>v<v<^vv^v><^^<<vv>^<<v><>^>>>>><v^v<<<^>^v^v<<v<>vvv<<>v>v>>^v^v^>><<<<>v^<v<><<>>>^>>^>><<v>" n = "^" e = ">" s = "v" w = "<" assert file.count(n) + file.count(e) + file.count(s) + file.count(w) == len( file) pos = {(0, 0)} x, y = 0, 0 X, Y = 0, 0 santa = True for carac in file: if carac == n: if santa: y += 1 else: Y += 1 elif carac == s: if santa: y -= 1 else: Y -= 1 elif carac == e: if santa: x += 1
import inputAoC as aoc groups = aoc.get_input_file(6).split("\n\n") res1 = 0 for group in groups: x = group.replace("\n", "") letters = set(x) res1 += len(letters) print(res1) res2 = 0 for group in groups: x = group.split("\n") letters = set(x[0]) for word in x[1:]: letters = letters.intersection(set(word)) res2 += len(letters) print(res2)
from os import replace import inputAoC as aoc from math import inf as infty input = """e => H e => O H => HO H => OH O => HH HOH""" input = aoc.get_input_file(19,2015) replacements, molecule = input.split('\n\n') replacements = [repl.split(' => ') for repl in replacements.splitlines()] def get_atoms(molecule): res = [] temp = '' for i in range(len(molecule)): carac = molecule[i] if not('a' <= carac <= 'z') and temp: res.append(temp) temp = '' temp += carac res.append(temp) return res def get_molecule(atoms): return "".join(atoms) atoms = get_atoms(molecule)
import inputAoC as aoc import my_utils import re rules_aoc, matching_aoc = aoc.get_input_file(19, 2020).split("\n\n") ex = '''0: 4 1 6: 0 5 1: 2 3 | 3 2 2: 4 4 | 5 5 3: 4 5 | 5 4 4: "a" 5: "b" 7: 4 5 14: 7 7 21: 7 14 28: 14 14 8: 7 | 4 9: 4 | 7 10: 2 3 | 3 2 11: 3 2 | 2 3'''.splitlines() messages_ex = '''ababbb bababa abbbab aaabbb aaaabbb'''.splitlines() rules = rules_aoc.splitlines() messages = matching_aoc.splitlines()
import inputAoC as aoc input = aoc.get_input_file(22, 2021) def get_cuboid(instr): mode = instr[:3].strip() mode = True if mode == "on" else False instr = instr[3:].strip() coords = list( map(lambda x: list(map(int, x[2:].split('..'))), instr.split(','))) coords.insert(0, mode) return coords instrs = list(map(get_cuboid, input.splitlines())) #for instr in instrs: print(instr) def part1(instr): _, x, y, z = instr r = range(-50, 51) return x[0] in r and x[1] in r and y[0] in r and y[1] in r and z[ 0] in r and z[1] in r def modelisation(instrs): grid = [0] * 101 for x in range(101): grid[x] = [0] * 101 for y in range(101):
def distance(self, time): period = self.fly_time + self.rest_time nb_periods = time // period distance_1period = self.speed * self.fly_time duree_end = min(time % period, self.fly_time) distance_end = self.speed * duree_end return nb_periods * distance_1period + distance_end def print(self): print(self.name, self.speed, self.fly_time, self.rest_time) reindeer_texte = aoc.get_input_file(14, 2015).split("\n") reindeers = [] # Vixen can fly 8 km/s for 8 seconds, but then must rest for 53 seconds. for phrase in reindeer_texte: phrase = phrase.replace(" can fly ", " ") phrase = phrase.replace(" km/s for ", " ") phrase = phrase.replace(" seconds, but then must rest for ", " ") phrase = phrase.replace(" seconds.", "") args = phrase.split() name, speed, fly_time, rest_time = args[0], int(args[1]), int( args[2]), int(args[3]) reindeers.append(Reindeer(name, speed, fly_time, rest_time))
import inputAoC as aoc cups = [int(cup) for cup in aoc.get_input_file(23, 2020)] cups_ex = [3, 8, 9, 1, 2, 5, 4, 6, 7] class LinkedListNode: def __init__(self, val, prev=None, next=None): self.prev = prev self.next = next self.val = val def __repr__(self): res = "val: " + str(self.val) res += " prev: " + str(self.prev.val) res += " next: " + str(self.next.val) return res class Cups: NB_PICKS = 3 def __init__(self, cups, MAX=-1): self.cups = [int(cup) for cup in cups] self.MAX = max(cups) if MAX > 0: self.cups += list(range(self.MAX + 1, MAX + 1)) self.MAX = MAX self.nodes = dict()
import inputAoC as aoc from json import loads import my_utils sues = aoc.get_input_file(16, 2015) # chaque ligne sera au format: # Sue nn: "fieldA": a, "fieldB": b, "fieldC": c sues = sues.replace('children', '"children"') sues = sues.replace('cats', '"cats"') sues = sues.replace('samoyeds', '"samoyeds"') sues = sues.replace('pomeranians', '"pomeranians"') sues = sues.replace('akitas', '"akitas"') sues = sues.replace('vizslas', '"vizslas"') sues = sues.replace('goldfish', '"goldfish"') sues = sues.replace('trees', '"trees"') sues = sues.replace('cars', '"cars"') sues = sues.replace('perfumes', '"perfumes"').splitlines() sues_dict = dict() for sue in sues: index = sue.index(':') nb = int(sue[4:index]) #len('Sue ') sue_json = sue[index + 2:] #len(': ') this_sue = loads("{" + sue_json + "}") sues_dict[nb] = this_sue def is_good1(sue, good_sue) -> bool: for field, n in sue.items():
import inputAoC file = inputAoC.get_input_file(6, 2015) size = 1000 instructs = file.split("\n") grid = [0]*size for i in range(size): grid[i] = [0]*size def turn_on(i,j): #grid[j][i] = 1 grid[j][i] += 1 def turn_off(i,j): #grid[j][i] = 0 if grid[j][i]: grid[j][i] -= 1 def toggle(i,j): #grid[j][i] = 1-grid[j][i] grid[j][i] += 2 def through(p1, p2, act): for i in range(p1[0], p2[0]+1): for j in range(p1[1], p2[1]+1): act(i,j) for instr in instructs: instr = instr.split(" ") p2 = list(map(int, instr[-1].split(","))) p1 = list(map(int, instr[-3].split(","))) if instr[0] == "toggle":
import inputAoC as aoc x = aoc.get_input_file(1, 2015) #x = "((((()(()(((((((()))(((()((((()())(())()(((()((((((()((()(()(((()(()((())))()((()()())))))))))()((((((())((()))(((((()(((((((((()()))((()(())()((())((()(()))((()))()))()(((((()(((()()))()())((()((((())()())()((((())()(()(()(((()(())(()(())(((((((())()()(((())(()(()(()(())))(()((((())((()))(((()(()()(((((()()(()(((()(((((())()))()((()(()))()((()((((())((((())(()(((())()()(()()()()()(())((((())((())(()()))()((((())))((((()())()((((())((()())((())(())(((((()((((()(((()((((())(()(((()()))()))((((((()((())()())))(((()(()))(()()(()(((()(()))((()()()())((()()()(((())())()())())())((()))(()(()))(((((()(()(())((()(())(())()((((()())()))((((())(())((())())((((()(((())(())((()()((((()((((((()(())()()(()(()()((((()))(())()())()))(())))(())))())()()(())(()))()((()(()(())()()))(()())))))(()))(()()))(())(((((()(()(()()((())()())))))((())())((())(()(())((()))(())(((()((((((((()()()(()))()()(((()))()((()()(())(())())()(()(())))(((((()(())(())(()))))())()))(()))()(()(((((((()((((())))())())())())()((((((((((((((()()((((((()()()())())()())())())(())(())))())((()())((()(()))))))()))))))))))))))))())((())((())()()))))))(((()((()(()()))((())(()()))()()())))(())))))))(()(((())))())()())))()()(())()))()(()))())((()()))))(()))))()))(()()(())))))))()(((()))))()(()))(())())))))()))((()))((()))())(())))))))))((((())()))()))()))())(())()()(())))())))(()())()))((()()(())))(())((((((()(())((()(((()(()()(())))()))))))()))()(()((()))()(()))(()(((())((((())())(())(()))))))))())))))))())())))))())))))()()(((())()(()))))))))())))))(())()()()))()))()))(()(())()()())())))))))())()(()(()))))()()()))))())(()))))()()))))()())))))(((())()()))(()))))))))))()()))))()()()))))(()())())()()())()(()))))()(()))(())))))))(((((())(())())()()))()()))(())))))()(()))))(())(()()))()())()))()))()))()))))())()()))())())))(()))(()))))))())()(((())()))))))))()))()())))())))())))()))))))))))()()))(()()))))))(())()(()))))())(()))))(()))))(()())))))())())()()))))())()))))))))(()))))()))))))()(()())))))))()))())))())))())))())))))))())(()()))))))(()())())))()())()))))))))))))))())))()(())))()))())()()(())(()()))(())))())()())(()(()(()))))())))))))))))())(()))()))()))))(())()())()())))))))))))()()))))))))))))())())))))(()())))))))))))())(())))()))))))))())())(()))()))(())))()))()()(())()))))))()((((())()))())())))))()))()))))((()())()))))())))(())))))))))))))))))()))))()()())()))()()))))())()))((()())))())))(()))(()())))))))()))()))))(())))))))(())))))())()()(()))())()))()()))))())()()))))())()))())))))))(()))))()())()))))))))(()))())))(()))()))))(())()))())())(())())())))))))((((())))))()))()))()())()(())))()))()))()())(()())()()(()())()))))())())))))(()))()))))())(()()(())))))(())()()((())())))))(())(())))))))())))))))))()(())))))))()())())())()(()))))))))(()))))))))())()()))()(()))))))()))))))())))))))(())))()()(())()())))))(((())))()((())()))())))(()()))())(())())))()(((()())))))()(()()())))()()(()()(()()))())()(()()()))())()()))()())(()))))())))))())))(())()()))))(()))))(())(()))(())))))()()))()))))())()))()()(())())))((()))())()))))))()()))))((()(()))))()()))))))())))))())(()((()())))))))))))()())())))()))(()))))))(()))(())()())))(()))))))))())()()()()))))(()())))))))((())))()))(()))(())(())()())()))))))))(())))())))(()))()()))(()()))(()))())))()(())))())((()((()(())))((())))()))))((((())())()())))(())))()))))))())(()()((())))())()(()())))))(()())()))())))))))((())())))))))(()(()))())()()(()()(((()(((()())))))()))))))()(())(()()((()()(())()()))())()())()))()())())())))))))(((())))))))()()))))))(((())()))(()()))(()()))))(()(()()((((())()())((()()))))(()(())))))()((()()()())()()((()((()()))(()))(((()()()))(((())))()(((())()))))))((()(())())))(()())(((((()(()))(()((()))(()())()))))(()(()))()(()))(())(((())(()()))))()()))(((()))))(()()()()))())))((()()()(())()))()))))()()))()))))))((((((()()()))))())((()()(((()))))(()(())(()()())())())))()(((()()))(())((())))(()))(()()()())((())())())(()))))()))()((()(())()(()()(())(()))(())()))(())(()))))(())(())())(()()(()((()()((())))((()))()((())))(((()()()()((((()))(()()))()()()(((())((())())(()()(()()()))()((())(())()))())(((()()(())))()((()()())()())(()(())())(((())(())())((())(())()(((()()))(())))((())(()())())(())((()()()((((((())))((()(((((())()))()))(())(()()))()))(())()()))(())((()()())()()(()))())()((())))()((()()())((((()())((())())())((()((()))()))((())((()()(()((()()(((())(()()))))((()((())()(((())(()((())())((())(()((((((())())()(()())()(())(((())((((((()(())(()((()()()((()()(()()()())))()()(((((()()))()((((((()))()(()(()(()(((()())((()))())()((()))(())))()))()()))())()()))())((((())(()(()))(((((((())(((()(((((()(((()()((((())(((())())))(()()()(()(()))()))((((((()))((()(((()(())((()((((()((((((())(((((())))(((()(()))))(((()(((())()((())(()((()))(((()()(((())((((()(()(((((()))(((()(((((((()(()()()(()(()(()()())(())(((((()(())())()())(()(()(()))()(()()()())(()()(()((()))()((())())()(()))((())(()))()(()))()(((()(()(()((((((()()()()())()(((((()()(((()()()((()(((((()))((((((((()()()(((((()))))))(()()()(())(()))(()()))))(())()))(((((()(((((()()(()(()())(((()))((((()((()(()(()((()(()((())))()(((()((()))((()))(((((((((()((()((()(())))()((((()((()()))((())(((()(((((()()(()(()()((()(()()()(((((((())())()())))))((((()()(()))()))(()((())()(()(((((((((()()(((()(()())(()((()())((())())((((()(((()(((()((((()((()((((()(()((((((())((((((((((((()()(()()((((((((((((((()((()()))()((((((((((((())((((()(()())((()(()(()))()(((((()()(((()()))()())(())((()(((((()((())(((((()((()(((((()))()()((((())()((((())(((((((((()(())(()(())))())(()((())(((())(())(())())(()(()(())()()((()((())()(((()(((((()(())))()(((()((())))((()()()(((()(((()((()(()(())(()((()())(()(()(((()(((((((((())(()((((()()))(()((((()()()()(((()((((((((()(()()((((((()(()()(()((()((((((((((()()(((((((()())(())))(((()()))(((((()((()()())(()()((((())((()((((()))))(())((()(()()(((()(()(((()((((()(((((()))())())(()((())()))(((()())((())((())((((()((()((((((())(()((((()()))((((((())()(()))((()(((())((((((((((()()(((((()(((((()((()()()((((())))(()))()((()(())()()((()((((((((((()((())(())(((((()(()(()()))((((()((((()()((()(((()(((((((((()(()((()((()))((((((()(((())()()((()(((((((()())))()()(()((()((()()(((()(()()()()((((()((())((((()(((((((((()(((()()(((()(()(((()(((()((())()(()((()(()(()(()))()(((()))(()((((()((())((((())((((((())(()))(()((((())((()(()((((((((()()((((((()(()(()()()(())((()((()()(((()(((((((()()((()(((((((()))(((((()(((()(()()()(()(((()((()()((())(()(((((((((()(()((()((((((()()((())()))(((((()((())()())()(((((((((((()))((((()()()()())(()()(()(()()))()))(()))(()(((()()))())(()(()))()()((())(()())()())()(()))()))(()()(()((((((())((()(((((((((((()(())()((()(()((()((()(()((()((((((((((()()())((())()(())))((())()())()(((((()(()())((((()((()(())(()))(((())()((()))(((((())(()))()()(()))(((())((((()((((()(())))(((((((()))))())()())(())((())()(()()((()(()))()(()()(()()((()())((())((()()))((((()))()()))(()()(())()()(((((()(())((()((((()))()))(()())())(((()()(()()))(())))))(()))((())(((((()((((()))()((((()))()((())(((())))(((()())))((()(()()((" res = x.count("(") - x.count(")") print(res) i = 0 niv = 0 while niv != -1: niv += 1 if x[i] == "(" else -1 i += 1 print(i)
import inputAoC as aoc docs = aoc.get_input_file(4).split("\n\n") res1 = 0 res2 = 0 def isnumeric(val): try: int(val) return True except ValueError: return False def is_str_nb_in_interv(val, a, b, nbDigits=-1): if nbDigits >= 0 and len(val) != nbDigits and val.isdigit(): return False return isnumeric(val) and a <= int(val) <= b for doc in docs: doc = doc.replace('\n', ' ') doc = doc.split() dic = {} for elem in doc: champ, contenu = elem.split(":") dic[champ] = contenu if dic.keys() == { "byr", "iyr", "eyr", "hgt", "hcl", "ecl", "pid", "cid"
import inputAoC as aoc import my_utils ex = """.#. ..# ###""" start = aoc.get_input_file(17, 2020) def get_pixel_neighbors(grid, x, y, z): res = 0 for k in range(z - 1, z + 2): if not (0 <= k < len(grid)): continue for j in range(y - 1, y + 2): if not (0 <= j < len(grid[k])): continue for i in range(x - 1, x + 2): if not (0 <= i < len(grid[k][j])): continue if not (i == x and j == y and k == z): res += grid[k][j][i] == "#" return res def get_neighbors(grid): res = [0] * len(grid) for z in range(len(grid)): res[z] = [0] * len(grid[z]) for y in range(len(grid[z])):
import inputAoC as aoc print("Ce programme prend un peu de temps, mais il fonctionne") input = int(aoc.get_input_file(20, 2015)) def diviseurs(x): """diviseurs(n): set des diviseurs de n""" F = set() F.add(1) for i in range(1, x + 1): if x % i == 0: F.add(i) return F def house(n): F = diviseurs(n) return sum(F) * 10 def ex(N): for n in range(1, N + 1): print("House", n, "receives", house(n), "gifts") #ex(10) def first_house(N):
import inputAoC as aoc input = "target area: x=20..30, y=-10..-5" input = aoc.get_input_file(17, 2021) def get_target(input): """returns ([x1,x2],[y1,y2])""" #re.format("target area: x={x1}..{x2}, y={y1}..{y2}") #?? xs, ys = input[len('target area: x='):].split(', y=') xs = sorted(map(int, xs.split('..'))) #déjà sorted, list suffit ys = sorted(map(int, ys.split('..'))) return xs, ys target = get_target(input) #print(target) def is_in_target(x, y, target): xs, ys = target return xs[0] <= x <= xs[1] and ys[0] <= y <= ys[1] def launch(vx, vy, target): """returns a boolean indicating if it has reached""" assert (not is_in_target(0, 0, target)) #else not borned x, y = 0, 0 #trajectory = [(x,y)] while y >= target[1][0] and not is_in_target(x, y, target): x += vx
case = layout[i][j] if case == "L" and voisins[i][j] == 0: layout[i][j] = "#" change = True elif case == "#" and voisins[i][j] >= nb_to_empty[tour - 1]: layout[i][j] = "L" change = True return change def simulate(layout, tour): while round(layout, tour): #print_matrice(layout) pass def count_occupied(layout): return sum([line.count("#") for line in layout]) #layout = [list(line) for line in ex.split("\n")] layout = [list(line) for line in aoc.get_input_file(11, 2020).split("\n")] simulate(layout, 1) res1 = count_occupied(layout) print(res1) #layout = [list(line) for line in ex.split("\n")] layout = [list(line) for line in aoc.get_input_file(11, 2020).split("\n")] simulate(layout, 2) res2 = count_occupied(layout) print(res2)
import inputAoC as aoc SUBJECT_NUMBER = 7 card, door = [int(nb) for nb in aoc.get_input_file(25, 2020).splitlines()] def handshake(loop_size, subject_nb=SUBJECT_NUMBER): value = 1 for _ in range(loop_size): value *= subject_nb value %= 20201227 return value assert handshake(8) == 5764801 assert handshake(11) == 17807724 assert handshake(8, handshake(11)) == 14897079 assert handshake(11, handshake(8)) == 14897079 def get_loopsize(value, subject_nb=SUBJECT_NUMBER): val = 1 loop_size = 0 while val != value: loop_size += 1 val *= subject_nb val %= 20201227 return loop_size assert get_loopsize(5764801) == 8