ASCII values with ord and delete_if - Day 15 - Advent of Code 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: https://adventofcode.com/
My Solutions: https://gist.github.com/cjavdev/d15a2a4ffed6c840c2fb28a093e9f927/
Playlist https://www.youtube.com/playlist?list=PLS6F722u-R6KYlGyUv65EFpGKl2Esmurr
#adventofcode #ruby