Day 4: Perilous Pier

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)
  • You can send code in code blocks by using three backticks, the code, and then three backticks or use something such as https://topaz.github.io/paste/ if you prefer sending it through a URL

FAQ

  • eta@feddit.org
    link
    fedilink
    English
    arrow-up
    1
    ·
    11 hours ago

    Elixir

    Hmm, my solution (although not really clean) works for the test input but not for the real input (answer is too low). Also looked around for some more codes to test but it works for those too. Anyone got additional tips what to try?

    My approach is to first disassemble each code and then with increasing instruction length check for each legible character if there are any interference with this length, both looking ahead and behind. After that I take the minimum length where that didn’t result in collisions.

    code
    #!/usr/bin/env elixir
    
    defmodule AOC do
    
    def part1(input) do
    	input |> Enum.map(&AOC.check/1) |> Enum.sum()
    end
    
    def check(code) do
    	chars =
    		String.graphemes(code)
    		|> Enum.with_index
    		|> Enum.group_by(fn {c,_} -> c end, fn {_,i} -> i end)
    	codeLen = String.length(code)
    	illegibPos = 
    		case Map.get(chars, "?") do
    			nil -> []
    			other -> other
    		end
    	legibChars =
    		case Map.drop(chars, ["?"]) do
    			nil -> %{}
    			other -> other
    		end
    	keys = Map.keys(legibChars)
    	case legibChars do
    		lC when (keys == []) ->
    			1
    		_ ->
    			length(Map.keys(legibChars))..codeLen
    			|> Enum.map(
    				fn instrLen ->
    					case checkChars(legibChars, illegibPos, codeLen, instrLen) do
    						s when s < 0 ->
    							99
    						_ -> instrLen
    					end
    				end
    			)
    			|> Enum.min()
    	end
    end
    
    def checkChars(legibChars, illegibPos, codeLen, instrLen) do
    	Enum.map(Map.keys(legibChars),
    		fn c ->
    			Enum.map(legibChars[c],
    				fn p ->
    					d = if p + instrLen >= codeLen do
    							-instrLen
    						else
    							instrLen
    						end
    					if p+d in legibChars[c] or p+d in illegibPos or p+d < 0 do
    						1
    					else
    						-99
    					end
    				end
    			)
    			|> Enum.sum()
    		end
    	)
    	|> Enum.sum()
    end
    
    end # module
    
    input =
    	File.read!("input/day04.txt")
    	|> String.split("\n", trim: true)
    testInput = """
    	rgbrgbrgbr
    	?rv
    	o??v???y
    	??bg?v??
    	??b?????
    	???
    	"""
    	|> String.split("\n", trim: true)
    
    res = AOC.part1(testInput)
    if res != 16, do: raise "test input is #{res} but should be 16"
    res = AOC.part1(["rrrbrr?b"])
    if res != 4, do: raise "test input is #{res} but should be 4"
    res = AOC.part1(["rg"])
    if res != 2, do: raise "test input is #{res} but should be 2"
    IO.puts "result: #{AOC.part1(input)}"
    
    • VegOwOtenks@lemmy.world
      link
      fedilink
      arrow-up
      2
      ·
      10 hours ago

      I had that too, luckily for me I didn’t need to drbug but only paste the correct number.

      What do you mean by “disassemble” the input?

      • eta@feddit.org
        link
        fedilink
        English
        arrow-up
        1
        ·
        4 hours ago

        By disassemble I mean that I’m not working with the input string itself but transforming it into one list of all the positions of the illegible characters and one map with all the legible ones and their position.

        I think I’ll have another look and compare the results I get with with what they should be for specific codes. I tried just putting in a number about 50 higher than my result and it was still too low so I’m not just off by one.