Something went wrong. Try again.
Code for the Advent of Code event
aoc advent-of-code
Something went wrong. Try again.
544 B · 28 lines
Ruby
at rust
1234567891011121314151617181920212223242526272829#!/usr/bin/env ruby# frozen_string_literal: true
class Node attr_accessor :children, :metadata
def initialize @children = [] @metadata = [] end
def leaf?; @children.empty?; end
def value leaf? ? @metadata.sum : @metadata.map { |i| @children[i - 1]&.value }.compact.sum endend
data = File.read('input.txt').split.map(&:to_i)
def build(d) a = d.shift 2 node = Node.new node.children.concat a[0].times.map { build d } node.tap { |n| n.metadata.concat d.shift a[1] }end
puts "Part 2: #{build(data).value}"