Day 2: Cube Conundrum


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/ or pastebin (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


🔒This post will be unlocked when there is a decent amount of submissions on the leaderboard to avoid cheating for top spots

🔓 Edit: Post has been unlocked after 6 minutes

  • morrowind
    link
    fedilink
    arrow-up
    2
    ·
    1 year ago

    crystal

    (commented parts are for part 1)

    input = File.read("input.txt").lines
    
    limits = {"red"=> 12, "green"=> 13, "blue"=> 14}
    # possibles = [] of Int32
    powers = [] of Int32
    
    input.each do |line|
    	game = line.split(":")
    	id = game[0].split()[1].to_i
    	possible = true
    	min = {"red"=> 0, "green"=> 0, "blue"=> 0}
    	
    	draws = game[1].split(";", &.split(",") do |cube|
    		cubeinfo = cube.split
    		num = cubeinfo[0].to_i
    		
    		min[cubeinfo[1]] = num if num > min[cubeinfo[1]] 
    		
    		# unless num <= limits[cubeinfo[1]]
    		# 	possible = false
    		# 	break
    		# end 
    	end )
    	
    	powers.push(min.values.product)
    	# possibles.push(id) if possible
    end
    # puts possibles.sum
    puts powers.sum
    
    • snowe@programming.dev
      link
      fedilink
      arrow-up
      1
      ·
      1 year ago

      Man I really need to learn Crystal. I love the ruby syntax and it’s supposed to be really fast right?

      • morrowind
        link
        fedilink
        arrow-up
        1
        ·
        1 year ago

        Yeah, it’s still a very small language, not much community or tooling, but a pleasure to use. In practice it’ll hit half the speeds of low level languages like rust and C, but with way less effort.