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

  • Jummit@lemmy.one
    link
    fedilink
    arrow-up
    2
    ·
    11 months ago

    Mostly an input parsing problem this time, but it was fun to use Hares tokenizer functions:

    lua
    -- SPDX-FileCopyrightText: 2023 Jummit
    --
    -- SPDX-License-Identifier: GPL-3.0-or-later
    
    local colors = {"blue", "red", "green"}
    local available = {red = 12, blue = 14, green = 13}
    local possible = 0
    local id = 0
    local min = 0
    
    for game in io.open("2.input"):lines() do
      id = id + 1
      game = game:gsub("Game %d+: ", "").."; "
      local max = {red = 0, blue = 0, green = 0}
      for show in game:gmatch(".-; ") do
        for _, color in ipairs(colors) do
          local num = tonumber(show:match("(%d+) "..color))
          if num then
            max[color] = math.max(max[color], num)
          end
        end
      end
      min = min + max.red * max.blue * max.green
      local thisPossible = true
      for _, color in ipairs(colors) do
        if max[color] > available[color] then
          thisPossible = false
          break
        end
      end
      if thisPossible then
        possible = possible + id
      end
    end
    
    print(possible)
    print(min)
    
    hare
    // SPDX-FileCopyrightText: 2023 Jummit
    //
    // SPDX-License-Identifier: GPL-3.0-or-later
    
    use strconv;
    use types;
    use strings;
    use io;
    use bufio;
    use os;
    use fmt;
    
    const available: []uint = [12, 13, 14];
    
    fn color_id(color: str) const uint = {
    	switch (color) {
    	case "red" => return 0;
    	case "green" => return 1;
    	case "blue" => return 2;
    	case => abort();
    	};
    };
    
    export fn main() void = {
    	const file = os::open("2.input")!;
    	defer io::close(file)!;
    	const scan = bufio::newscanner(file, types::SIZE_MAX);
    	let possible: uint = 0;
    	let min: uint = 0;
    
    	for (let id = 1u; true; id += 1) {
    		const line = match(bufio::scan_line(&scan)!) {
    		case io::EOF =>
    			break;
    		case let line: const str =>
    			yield strings::sub(
    					line,
    					strings::index(line, ": ") as size + 2,
    					strings::end);
    		};
    		let max: []uint = [0, 0, 0];
    		let tok = strings::rtokenize(line, "; ");
    		for (true) {
    			const show = match(strings::next_token(&tok)) {
    			case void =>
    				break;
    			case let show: str =>
    				yield show;
    			};
    			const pairs = strings::tokenize(show, ", ");
    			for (true) {
    				const pair: (str, str) = match(strings::next_token(&pairs)) {
    				case void =>
    					break;
    				case let pair: str =>
    					let tok = strings::tokenize(pair, " ");
    					yield (
    						strings::next_token(&tok) as str,
    						strings::next_token(&tok) as str
    					);
    				};
    				let color = color_id(pair.1);
    				let amount = strconv::stou(pair.0)!;
    				if (amount > max[color]) max[color] = amount;
    			};
    		};
    		if (max[0] <= available[0] && max[1] <= available[1] && max[2] <= available[2]) {
    			fmt::printfln("{}", id)!;
    			possible += id;
    		};
    		min += max[0] * max[1] * max[2];
    	};
    	
    	fmt::printfln("{}", possible)!;
    	fmt::printfln("{}", min)!;
    };