ASCII values with ord and delete_if - Day 15 - Advent of Code 2023

Sdílet
Vložit
  • čas přidán 14. 12. 2023
  • In this video, you’ll learn how to solve day 15 of the Advent of Code 2023. The first part involves writing a hash function to convert initialization sequences like "rn=1" into decimal values between 0-255. The hash function works by:
    1. Getting the ASCII code for each character using .ord
    2. Multiplying the current value by 17
    3. Taking modulo 256 to get the final hash value
    For part 2, the initialization sequences now can contain "=" to add lenses or "-" to remove them from "boxes", which are arrays. The boxes are stored in a hash keyed by the hash value of the label on the lense. We’ll parse the complex input into easier-to-work with structures. Then we’ll iterate through the instructions, either adding/updating lenses or removing them if the instruction contains "-". At the end, we’ll sum each lens's "focusing power" based on the box number and slot number.
    After getting the right answer, we’ll spend some time refactoring some of the code, showing useful Ruby idioms like inject, delete_if, and pattern matching.
    Advent of Code: adventofcode.com/
    My Solutions: gist.github.com/cjavdev/d15a2...
    Playlist • Advent of Code
    #adventofcode #ruby
  • Jak na to + styl

Komentáře • 2

  • @piotrtalarczyk8987
    @piotrtalarczyk8987 Před 5 měsíci +1

    Hi! I'm still here. In AoC2022 I solved half of days. This time I would like solve at least 1st puzzle of each day either it's no more time of advent. Thx.

    • @cjav_dev
      @cjav_dev  Před 5 měsíci

      Great job! Thank you!!