Advent of Code 2020 Day 9 - using Python

Поделиться
HTML-код
  • Опубликовано: 15 сен 2024

Комментарии • 6

  • @umarpatel5809
    @umarpatel5809 3 года назад +1

    what does data[i] do

    • @dylancodes1669
      @dylancodes1669  3 года назад

      data[i] will access an element in the input list. In this case, data[i] represents the number that we are checking is corrupted or not.

  • @ccall48
    @ccall48 3 года назад

    you really gotta update that java dude or mute the notification lol

  • @masterbe591
    @masterbe591 3 года назад +1

    I cheated for part one and used combinations-method 😋
    import itertools, time
    def puzzle_input():
    with open("input.txt") as f:
    data = [int(line.replace("
    ", "")) for line in f]
    return data
    puzzle = puzzle_input()
    def solution_1(data):
    width = 25
    for x in range(width, len(data)):
    sums = list(map(sum, list(itertools.combinations(data[x - width:x], 2))))
    if data[x] not in sums:
    return data[x]
    def solution_2(data):
    for x in range(1, data.index(sum_to_find)):
    for y in range(0, data.index(sum_to_find)):
    if sum(data[y:y + x]) == sum_to_find:
    return min(data[y:y + x]) + max(data[y:y + x])
    start = time.perf_counter()
    print(solution_1(puzzle), time.perf_counter() - start)
    sum_to_find = solution_1(puzzle)
    start = time.perf_counter()
    print(solution_2(puzzle), time.perf_counter() - start)

    • @dylancodes1669
      @dylancodes1669  3 года назад +1

      Dang those solutions are so short! I've never tried itertools before.