Advent of Code 2024 | Day 07 "Bridge Repair"

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

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

  • @mattisovereighteen
    @mattisovereighteen 2 месяца назад +11

    For you, I'm glad you're getting paid by a sponsor. You do great work and you should be paid for it. I'll gladly take the slight annoyance if it means you're benefiting from your own hard work

  • @kayebennett7867
    @kayebennett7867 2 месяца назад

    So elegant, I didn't think of looking at the input from end to start... Once you know it, it seems so natural :D

    • @QuotePilgrim
      @QuotePilgrim 2 месяца назад +1

      There is no real advantage to going backwards. You can implement the exact same algorithm _she_ implemented in the video by going from the start of the list.
      Think about it, if you have two lists A and B that are really the exact same list but reversed relative to each other, what the backwards algorithm does to solve list A would be the same thing the forwards algorithm does to solve list B and vice versa.

    • @hyper-neutrino
      @hyper-neutrino  2 месяца назад +1

      this is untrue because the operators are left-to-right, also *she. i may be misunderstanding your approach here though so if you implemented it this way i would like to see how you solved it

    • @QuotePilgrim
      @QuotePilgrim 2 месяца назад

      @@hyper-neutrino Oh, right, computer science degree, I should have known, my bad.
      Anyway yeah I just forgot to consider that because there's no order of operations it matters whether you go forwards or backwards. If it was like a real mathematical equation, if you flip the whole thing around you still get the same result. 2 + 3 * 4 + 5 and 5 + 4 * 3 + 2 are the same thing in math so my brain just assumed it to be true for the puzzle. Of course it's not, I was just being dumb.

    • @kayebennett7867
      @kayebennett7867 2 месяца назад

      ​@@QuotePilgrim Also, even if the list were analysed by operation precedence, this would be wrong once you consider non-commutative operations (such as concatenation). The only reason why handling the input in reverse is more useful than forward is because two of the operations (multiplication and concatenation, and technically addition also) don't have a well-defined inverse operation, which allows us to reduce the amount of nodes to search in the ternary tree.

  • @pranavtaysheti7858
    @pranavtaysheti7858 2 месяца назад +8

    But how did elephant 🐘 run away with operator?

    • @TheFrogfather1
      @TheFrogfather1 2 месяца назад +2

      It's a good idea not to try to overanalyse the stories :P

  • @zekrodev
    @zekrodev 2 месяца назад +5

    Really cool to see that you got a sponsor for the video. WEll deserved payment for the consistently great work.

    • @hyper-neutrino
      @hyper-neutrino  2 месяца назад

      thanks! also that's monaspace krypton IIRC (monaspace.githubnext.com/)

  • @SebastianRuhs
    @SebastianRuhs 2 месяца назад

    Thank you for another great explanation of how to solve the puzzle!

  • @shrugalic
    @shrugalic 2 месяца назад +1

    I did it the exact way you showed first, but in Rust, so it's not nearly as compact. I like how concise Python is

    • @TheFrogfather1
      @TheFrogfather1 2 месяца назад

      The length of my Object Pascal solution would make you cry :P

    • @shrugalic
      @shrugalic 2 месяца назад +1

      @ Oh. My second programming language was Pascal. The memories…

    • @TheFrogfather1
      @TheFrogfather1 2 месяца назад

      @@shrugalic The day job is React/JS so AOC is about the only time I use Pascal these days.

  • @galactic_fx
    @galactic_fx 2 месяца назад +5

    chapters!

    • @hyper-neutrino
      @hyper-neutrino  2 месяца назад +6

      yeah, i realize i've been forgetting to put those in. i remembered because i had a sponsor segment and so i wanted to let people skip it more easily :)

    • @shrugalic
      @shrugalic 2 месяца назад

      @@hyper-neutrinoappreciated, ty

  • @rastislavsvoboda4363
    @rastislavsvoboda4363 2 месяца назад

    clever, going backwards
    I'm just going "straightforward" ;-)
    def matches1(target, array):
    if len(array) == 1:
    return target == array[0]
    a = array[0]
    b = array[1]
    tail = array[2:]
    return (matches1(target, [a + b] + tail)
    or matches1(target, [a * b] + tail))
    def matches2(target, array):
    if len(array) == 1:
    return target == array[0]
    a = array[0]
    b = array[1]
    tail = array[2:]
    return (matches2(target, [a + b] + tail)
    or matches2(target, [a * b] + tail)
    or matches2(target, [int(str(a) + str(b))] + tail))

  • @avagata9309
    @avagata9309 2 месяца назад

    so a DFS

  • @deathxunnat4693
    @deathxunnat4693 Месяц назад

    whats wrong with my code
    from collections import deque
    def read_input(file_path):
    result = {}
    with open(file_path, 'r') as file:
    for line in file:
    key, value = line.split(':')
    key = int(key.strip())
    value = deque(map(int, value.strip().split()))
    result[key] = value
    return result
    def re(a, b):
    if len(b) == 1:
    return b[0]
    f = b.popleft()
    s = b.popleft()
    # Addition
    sum_val = f + s
    b.appendleft(sum_val)
    add = re(a, b)
    if add == a:
    return a
    b.popleft()
    # Subtraction
    diff_val = f - s
    b.appendleft(diff_val)
    subtract = re(a, b)
    if subtract == a:
    return a
    b.popleft()
    # Multiplication
    prod = f * s
    b.appendleft(prod)
    product = re(a, b)
    if product == a:
    return a
    b.popleft()
    # Division (ensure no division by zero)
    if s != 0:
    div_val = f // s
    b.appendleft(div_val)
    divide = re(a, b)
    if divide == a:
    return a
    b.popleft()
    # Restore the deque
    b.appendleft(s)
    b.appendleft(f)
    return prod
    def ist(p):
    return re(p[0], p[1]) == p[0]
    def main():
    file_path = 's.txt'
    data = read_input(file_path)
    # Print the map to verify the result
    ss = 0
    for key, value in data.items():
    if ist((key, value)):
    ss += key
    print(f'this makes a valid pair {key}')
    print(f'Sum of all the keys is: {ss}')
    if __name__ == '__main__':
    main()
    can someone help