DEV Community

Caleb Weeks
Caleb Weeks

Posted on

2

Advent of Code #2 (in Crystal)

Nothing extraordinary about today's problem. Here's a few things I learned about Crystal while solving this one:

  • You can define type aliases using alias (shocker)
  • Somewhat unintuitively (to me, at least), you cannot call the map method with parentheses. Instead, you wrap the anonymous function in braces ({}) and then you can chain methods after that. I think it's idiomatic to leave a space between map and the braces, but that means you end up with code that looks like map {|x| x + 1}.sum.
  • Destructuring arrays works as expected (without the surrounding square brackets), but destructuing hashes isn't really a thing. You can destructure the key/value pair (which is basically an array), or you can destructure the keys or values separately after turning them into arrays.
input = File.read("input").strip

alias Games = Hash(Int32, Subsets)
alias Subsets = Array(Subset)
alias Subset = Hash(String, Int32)

games = input.split("\n").reduce(Games.new) do |games, line|
  game, subsets = line.split(":")
  game_number = game[5..].to_i
  subsets = subsets.split(';').map do |subset|
    subset.split(',').reduce(Subset.new) do |set, cubes|
      number, color = cubes.strip.split(' ')
      set.merge({color => number.to_i})
    end
  end
  games.merge({game_number => subsets})
end

bag = {
  "red" => 12,
  "green" => 13,
  "blue" => 14
}

part1 = games.select do |game, subsets|
  subsets.map do |subset|
    subset.map { |color, number| number <= bag[color] }.all?
  end.all?
end.keys.sum

puts part1

part2 = games.values.map do |subsets|
  subsets.reduce do |set, cubes|
    set.merge(cubes) { |_, old, new| Math.max(old, new) }
  end.values.product
end.sum

puts part2
Enter fullscreen mode Exit fullscreen mode

Image of Docusign

🛠️ Bring your solution into Docusign. Reach over 1.6M customers.

Docusign is now extensible. Overcome challenges with disconnected products and inaccessible data by bringing your solutions into Docusign and publishing to 1.6M customers in the App Center.

Learn more

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay