• 0 Posts
  • 13 Comments
Joined 11 months ago
cake
Cake day: August 23rd, 2023

help-circle

  • I don't understand the purpose of this library. The article makes it sound like the problem is circular dependencies, which is not something that is addressed by the library in any way. Deleting and regenerating all the migrations instead of squashing is something I’ve done before (it often generates a much simpler squashed migration than squashmigrations does), but it is very much a manual process, for example you must review the generated migration and bring in any runpython/runsql operations, because makemigrations won’t do that. And the replaces directive built in to Django handles fixing the history across environments already.



  • Python

    Easy one today

    code
    import pathlib
    
    base_dir = pathlib.Path(__file__).parent
    filename = base_dir / "day9_input.txt"
    
    with open(base_dir / filename) as f:
        lines = f.read().splitlines()
    
    histories = [[int(n) for n in line.split()] for line in lines]
    
    answer_p1 = 0
    answer_p2 = 0
    
    for history in histories:
        deltas: list[list[int]] = []
        last_line: list[int] = history
    
        while any(last_line):
            deltas.append(last_line)
            last_line = [last_line[i] - last_line[i - 1] for i in range(1, len(last_line))]
    
        first_value = 0
        last_value = 0
        for delta_list in reversed(deltas):
            last_value = delta_list[-1] + last_value
            first_value = delta_list[0] - first_value
    
        answer_p1 += last_value
        answer_p2 += first_value
    
    print(f"{answer_p1=}")
    print(f"{answer_p2=}")
    


  • (python) Much easier than day 3.

    code
    import pathlib
    
    base_dir = pathlib.Path(__file__).parent
    filename = base_dir / "day4_input.txt"
    
    with open(base_dir / filename) as f:
        lines = f.read().splitlines()
    
    score = 0
    
    extra_cards = [0 for _ in lines]
    n_cards = [1 for _ in lines]
    
    for i, line in enumerate(lines):
        _, numbers = line.split(":")
        winning, have = numbers.split(" | ")
    
        winning_numbers = {int(n) for n in winning.split()}
        have_numbers = {int(n) for n in have.split()}
    
        have_winning_numbers = winning_numbers & have_numbers
        n_matches = len(have_winning_numbers)
    
        if n_matches:
            score += 2 ** (n_matches - 1)
    
        j = i + 1
        for _ in range(n_matches):
            if j >= len(lines):
                break
            n_cards[j] += n_cards[i]
            j += 1
    
    answer_p1 = score
    print(f"{answer_p1=}")
    
    answer_p2 = sum(n_cards)
    print(f"{answer_p2=}")