Day 9: Mirage Maintenance

Megathread guidelines

  • Keep top level comments as only solutions, if you want to say something other than a solution put it in a new post. (replies to comments can be whatever)
  • Code block support is not fully rolled out yet but likely will be in the middle of the event. Try to share solutions as both code blocks and using something such as https://topaz.github.io/paste/ , pastebin, or github (code blocks to future proof it for when 0.19 comes out and since code blocks currently function in some apps and some instances as well if they are running a 0.19 beta)

FAQ


🔒 Thread is locked until there’s at least 100 2 star entries on the global leaderboard

🔓 Unlocked after 5 mins

  • snoweA
    link
    fedilink
    arrow-up
    4
    ·
    edit-2
    1 year ago

    Ruby

    [email protected] [LANGUAGE: Ruby]

    I found today really easy thankfully. Hardest part was remembering the language features haha

    https://github.com/snowe2010/advent-of-code/blob/master/ruby_aoc/2023/day09/day09.rb

    edit: code golfing this one was easy too! man this day really worked out huh

        def get_subsequent_reading(reading)
          puts "passed in readings #{reading}"
          if reading.all?(0)
            reading << 0
          else
            readings = reading.each_cons(2).map do |a, b|
              b - a
            end
            sub_reading = get_subsequent_reading(readings)
            reading << (reading[-1] + sub_reading[-1])
            puts "current reading #{reading}"
            reading
          end
        end
        
        execute(1) do |lines|
          lines.map do |reading|
            get_subsequent_reading(reading.split.map(&:to_i))
          end.map {|arr| arr[-1]}.sum
        end
        
        
        def get_preceeding_readings(reading)
          puts "passed in readings #{reading}"
          if reading.all?(0)
            reading.unshift(0)
          else
            readings = reading.each_cons(2).map do |a, b|
              b - a
            end
            sub_reading = get_preceeding_readings(readings)
            reading.unshift(reading[0] - sub_reading[0])
            puts "current reading #{readings} #{sub_reading}"
            reading
          end
        end
        
        
        execute(2, test_only: false, test_file_suffix: '') do |lines|
          lines.map do |reading|
            get_preceeding_readings(reading.split.map(&:to_i))
          end.map {|arr| arr[0]}.sum
        end
    

    code golf

      a=->r{r.unshift(r.all?(0)?0:(r[0]-a[r.each_cons(2).map{_2-_1}][0]))}
      l.map{a[_1.split.map(&:to_i)]}.map{_1[0]}.sum