ASCII values with ord and delete_if - Day 15 - Advent of Code 2023
Вставка
- Опубліковано 26 сер 2024
- 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.co...
Playlist • Advent of Code
#adventofcode #ruby
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.
Great job! Thank you!!