Advent of Code 2024 | Day 03 "Mull It Over"

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

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

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

    sorry about the poor audio quality; i changed my setup today and a different device took my audio and i didn't notice. i'll have this fixed in the next video but i don't really have time to re-record this one today. thanks! :3

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

    the part2 regex is so clean! You can also use a capturing group for the numbers in the pattern. The numbers still need to be casted to ints tho.

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

      very good point, that probably would've been a much more elegant solution

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

    I'm not familiar with regex so I used classic str matching with indices instead - slower and less compact. Something to practice for the next few days

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

    Could have used capture groups in the findall to get tuples of just the numbers.

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

      true, i usually don't use capture groups unless i need to but i should get more familiar with them

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

      yup this is the strat

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

      Came here to comment the same. Capture groups is just free parsing in this case

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

    I really hope you complete the series this year. I'm doing it as a hobbyist. Today took me awhile because I knew I needed to use a regex but without GPT support it was actually pretty hard to figure out what I needed to write "for the regex". It'd be great to compare daily with people really proficient who explain what they're doing.
    Now to try to do it in golang.

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

      i plan to complete it, yes! and fair enough - you might've noticed from the start of each video that i have copilot but i don't want to use it for AoC and i usually just use it for boilerplate at work. figuring out the regex took me a bit too, TBH

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

      also learning golang could be neat but it's not high on my list - if i'm going to learn a new language it's going to be rust

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

      ​@@hyper-neutrino What is the AoC ruling on copilot / AI? I'm also not using it because I do this mostly to learn to problem-solve. I know last year it was very much forbidden, but I know copilot has become more common-practice since then in industry.

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

      @@hyper-neutrino idm the copilot support... at this point we gotta get used to it cause it's likely here to stay. Personally I take the ghost text but still type everything manually just to get the fingers working if that makes sense.
      Really do appreciate it. I need to go thru your day 04 ceres search. Your final grid solution is elegant.

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

    Gesundheit!

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

    Alternative solution for part 2:
    Replace everything between an don't() and a do() in your 'memory' with an empty string (so basically remove it) and then use the solution of part one on the remaining memory. The regex to do the replacement would be: r"don't\(\).*?do\(\)"

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

      interesting idea, i like it! wouldn't it need to be r"don't\(\).*?(do\(\))?" or do we not need to catch that case?

  • @m.e.6271
    @m.e.6271 2 месяца назад +2

    I didn't read the part about X and Y only containing a max of three digits and just matched the regex to \d+ and it worked. Don't know if I got lucky with my input or that case wasn't tested in any of them.

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

      same here

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

      i think this just wasn't tested for. i also forgot this, but unfortunately i also didn't look at my input (i have a script to load it) and missed that it's not a single line which is what did me in today

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

      Same here 😅

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

      same!

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

    Is there a reason to not do \d+? I'm not sure why we're specifying {1-3}, unless it was in the rules that we should exclude numbers under 1000. Also I didn't use r string and it seems to work the same - "mul\(\d+,\d+\)"

    • @luked.1734
      @luked.1734 2 месяца назад +2

      The problem statement specifies 1-3 digit numbers, but \d+ works because the dataset doesn’t have numbers with 4+ digits anyway

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

    I create almost 1:1 solutuon 😂 but more functional style, maybe you can try FP in Python?

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

    Here's my more or less pythonic solution:
    (spoiler!)
    # pt1
    OPER_RE = re.compile(r"mul\((\d{1,3}),(\d{1,3})\)")
    def mul_match_to_value(m: re.Match) -> int:
    return math.prod(map(int, m.groups()))
    print(sum(map(mul_match_to_value, OPER_RE.finditer(DATA))))
    # pt2
    DO_DONT_RE = re.compile(r"do(n't)?\(\)")
    all_matches_sorted = sorted(
    itertools.chain(
    OPER_RE.finditer(DATA),
    DO_DONT_RE.finditer(DATA),
    ),
    key=lambda m: m.span()[0],
    )
    enabled = True
    res = 0
    for m in all_matches_sorted:
    matched_string = DATA[slice(*m.span())]
    if matched_string.startswith('mul'):
    res += enabled * mul_match_to_value(m)
    else:
    enabled = matched_string == 'do()'
    print(res)