Percent delimiters %w, zip - Day 06 - Advent of Code 2023

Sdílet
Vložit
  • čas přidán 5. 12. 2023
  • See how to solve the Day 6 "Race" puzzle for the advent of code with Ruby! We’ll determine how long to press a boat's speed button to beat distance records. We’ll talk about Ruby's %w delimiter to extract race times and distances. Then use `zip` to combine the time and distance arrays together.
    We’ll increment a counter when the distance beats the record. By reducing these counters per race, we can find the final product.
    While this approach works, faster alternative solutions are using the quadratic formula for a mathematical model or counting below threshold distances. This allows skipping unnecessary iterations.
    Advent of Code: adventofcode.com/
    My Solutions: gist.github.com/cjavdev/d15a2...
    Playlist • Advent of Code
    #adventofcode #ruby
  • Jak na to + styl

Komentáře • 6

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

    Finally i manged to complete day 5 - it was hard. Today's puzzles are tricky. I had to back to my school time when I was 15-19 yo - more then 20 years ago to re-learn how to do quadratic equations.
    class ToyBoat
    def initialize time_given, record
    @time_given = time_given
    @record = record
    end
    def get_winning_range
    delta = @time_given ** 2 - 4 * (@record+1)
    time1 = (@time_given - Math.sqrt(delta))/2
    time1 = time1.ceil
    time2 = (@time_given + Math.sqrt(delta))/2
    time2 = time2.floor
    (time1..time2)
    end
    end
    time, record = $stdin.read.split("
    ").map(&:split)
    p time = time[1..-1].join.to_i
    p record = record[1..-1].join.to_i
    race = ToyBoat.new(time,record)
    p race
    p gwr = race.get_winning_range
    p nums_of_ways = gwr.size

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

      Nice job! Way to stay with it.

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

    the => result is new to me, so we can assign to variable at the end of block ? cool

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

      Oh yes! Checkout the earlier episode called rightward assignment where we talk a lot about that new Ruby feature

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

    BTW: What is that vim/neovim plugin that shows warnings while typing?

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

      I'm using ale - github.com/dense-analysis/ale