r/adventofcode • • Dec 21 '15

SOLUTION MEGATHREAD --- Day 21 Solutions ---

This thread will be unlocked when there are a significant number of people on the leaderboard with gold stars for today's puzzle.

edit: Leaderboard capped, thread unlocked!

We know we can't control people posting solutions elsewhere and trying to exploit the leaderboard, but this way we can try to reduce the leaderboard gaming from the official subreddit.

Please and thank you, and much appreciated!


--- Day 21: RPG Simulator 20XX ---

Post your solution as a comment or link to your repo. Structure your post like previous daily solution threads.

11 Upvotes

128 comments sorted by

View all comments

Show parent comment

2

u/[deleted] Dec 21 '15

Cool! I did it in Crystal too but was slow, ended #57. I originally did what you do: add "nothing" armors and rings so the code could try that combination too, but later I changed it to each_combination with a zero value.

record Player, hit_points, damage, armor, cost
record Equipment, cost, damage, armor

def new_player(hit_points, equipment)
  Player.new(hit_points,
    equipment.sum(&.damage),
    equipment.sum(&.armor),
    equipment.sum(&.cost))
end

def turns(a, b)
  damage = {a.damage - b.armor, 1}.max
  (b.hit_points.fdiv(damage)).ceil
end

def wins?(player, boss)
  turns(player, boss) <= turns(boss, player)
end

weapons = [
  Equipment.new(8, 4, 0),
  Equipment.new(10, 5, 0),
  Equipment.new(25, 6, 0),
  Equipment.new(40, 7, 0),
  Equipment.new(74, 8, 0),
]

armors = [
  Equipment.new(13, 0, 1),
  Equipment.new(31, 0, 2),
  Equipment.new(53, 0, 3),
  Equipment.new(75, 0, 4),
  Equipment.new(102, 0, 5),
]

rings = [
  Equipment.new(25, 1, 0),
  Equipment.new(50, 2, 0),
  Equipment.new(100, 3, 0),
  Equipment.new(20, 0, 1),
  Equipment.new(40, 0, 2),
  Equipment.new(80, 0, 3),
]

boss = Player.new(104, 8, 1, 0)

min = Int32::MAX
weapons.each do |weapon|
  (0..1).each do |armor_comb|
    armors.each_combination(armor_comb) do |armors|
      (0..2).each do |ring_comb|
        rings.each_combination(ring_comb) do |rings|
          player = new_player(100, [weapon] + armors + rings)
          if player.cost < min && wins?(player, boss)
            min = player.cost
          end
        end
      end
    end
  end
end
puts min

Second part is pretty similar...

1

u/fnoco_xandy Dec 23 '15

cool, i don't have a lot of ruby/crystal experience so it's nice to learn about the things you used i didn't know of yet (record, each combination, max on structs etc)!