• 4 Posts
  • 174 Comments
Joined 2 years ago
cake
Cake day: July 2nd, 2024

help-circle
  • 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.


  • 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)}"
    

  • Elixir

    For part 1 I iterated over the lists and took some time figuring out what the end condition for this should actually be. Not really proud of this part but it works.

    So for part 2 I started over and did it with an approach kind of using a starting point, vector to a second point and then calculating point 3 and 4 from there. Then each point is checked wether it’s out of bounds and if it has a tree planted there. It’s taking a few seconds to complete so it’s not running really fast but I like the approach more than my part 1 and it could probably be optimised by rearranging some checks so it stop checking earlier.

    Edit: Rearranging checks doesn’t do much, this approach is just slow. It’s probably better to get the coordinates for all trees and work with that as some other solutions did.

    solution
    #!/usr/bin/env elixir
    
    defmodule AOC do
    
    def getData(inputStr) do
    	inputStr
    	|> String.split("\n", trim: true)
    	|> Enum.map(fn s -> String.graphemes(s) end)
    end
    
    def part1(data) do
    	0..length(data)-2
    	|> Enum.map(
    		fn r ->
    			0..length(Enum.at(data,0))-2
    			|> Enum.map(
    				fn c ->
    					case Enum.at(Enum.at(data,r),c) do
    						"#" -> checkPos(data, r, c)
    						_ -> 0
    					end
    				end
    			)
    			|> Enum.sum()
    		end
    	)
    	|> Enum.sum()
    end
    
    def checkPos(data, r, c) do
    	ub = min(length(data)-1-r, length(Enum.at(data,0))-1-c)
    	c+1..c+ub
    	|> Enum.map(
    		fn c2 ->
    			dist = c2 - c
    			if Enum.at(Enum.at(data,r),c2) == "#" and
    			Enum.at(Enum.at(data,r+dist),c) == "#" and
    			Enum.at(Enum.at(data,r+dist),c2) == "#" do
    				1
    			else
    				0
    			end
    		end
    	)
    	|> Enum.sum()
    end
    
    def part2(data) do
    	for r <- 0..length(data)-2, c <- 0..length(data)-2 do
    		case Enum.at(Enum.at(data,r),c) do
    			"#" -> checkPos2(data, r, c)
    			_ -> 0
    		end
    	end
    	|> Enum.sum()
    end
    
    def checkPos2(data, r, c) do
    	for dr <- 1..length(data)-1, dc <- -c..0 do
    		{r2, c2, r3, c3} = {r+dr, c+dc, r-dc, c+dr}
    		{r4, c4} = {r3+dr, c3+dc}
    		if r3 < length(data) and c3 < length(data) and
    		r4 < length(data) and c4 < length(data) do
    			if Enum.at(Enum.at(data,r2),c2) == "#" and
    			Enum.at(Enum.at(data,r3),c3) == "#" and
    			Enum.at(Enum.at(data,r4),c4) == "#" do
    				1
    			else
    				0
    			end
    		else
    			0
    		end
    	end
    	|> Enum.sum()
    end
    
    end # module
    
    input = AOC.getData(File.read!("input/day03.txt"))
    IO.puts "part 1 #{AOC.part1(input)}"
    IO.puts "part 2 #{AOC.part2(input)}"
    


  • Elixir

    I thought about this a bit and realised that modulo math could probably help with this repeating pattern. So I transformed the letters to numbers, like on a clock. For part 2 I just sorted the program in descending order, since you start at 0 (or rather 6) and want to go as far as possible for all the remaining instructions.

    solution
    #!/usr/bin/env elixir
    
    defmodule AOC do
    
    def getProgNums(prog) do
    	String.graphemes(prog)
    	|> Enum.map(
    		&Map.get(%{"r"=>6,"o"=>1,"y"=>2,"g"=>3,"b"=>4,"v"=>5}, &1, &1)
    	)
    end
    
    def part1(prog) do
    	move(AOC.getProgNums(prog), 0, 0)
    end
    
    def move([], acc, _pos) do
    	acc
    end
    
    def move([n | rest], acc, pos) do
    	case rem(n-pos, 6) do
    		r when r <= 0 -> move(rest, acc + r + 6, n)
    		r -> move(rest, acc + r, n)
    	end
    end
    
    def part2(prog) do
    	AOC.getProgNums(prog)
    	|> Enum.sort(:desc)
    	|> move(0,0)
    end
    
    end # module
    
    input =
    	File.read!("input/day02.txt")
    	|> String.trim()
    
    IO.puts "Part 1 answer: #{AOC.part1(input)}"
    IO.puts "Part 2 answer: #{AOC.part2(input)}"
    



  • Elixir

    First I tried to manually assemble a list with the IDs and their amounts but then found out there is a built in function for that.

    solution
    #!/usr/bin/env elixir
    
    require Logger
    
    defmodule AOC do
    
    def part1(filename) do
    	ids =
    		File.read!(filename)
    		|> String.trim()
    		|> String.split("\n")
    		|> Enum.map(fn x -> String.to_integer(x) end)
    		|> Enum.frequencies()
    	[p1, p2, p3 | _rest] = Enum.sort_by(ids, fn {_k,v} -> -v end)
    	{p1, p2, p3}
    end
    
    def part2(filename) do
    	ids =
    		File.read!(filename)
    		|> String.trim()
    		|> String.split("\n")
    		|> Enum.map(fn x -> String.to_integer(x) end)
    		|> Enum.frequencies()
    	[p1, p2, p3 | _rest] = Enum.sort_by(ids, fn {_k,v} -> v end)
    	{p1, p2, p3}
    end
    
    end # module
    
    {p1, p2, p3} = AOC.part1("input/day01Test.txt")
    res = elem(p1,0)*elem(p2,0)*elem(p3,0)
    if res != 186276, do: raise "result should be 186276 but is #{res}"
    {p1, p2, p3} = AOC.part1("input/day01Input.txt")
    res = elem(p1,0)*elem(p2,0)*elem(p3,0)
    Logger.info("Part 1: #{res}")
    
    {p1, p2, p3} = AOC.part2("input/day01Test.txt")
    res = elem(p1,0)*elem(p2,0)*elem(p3,0)
    if res != 193050, do: raise "result should be 193050 but is #{res}"
    {p1, p2, p3} = AOC.part2("input/day01Input.txt")
    res = elem(p1,0)*elem(p2,0)*elem(p3,0)
    Logger.info("Part 2: #{res}")
    



  • For a project using Elixir on a Raspberry Pi I recently used Alpine instead of the default Raspberry OS because the package there was so old that it wasn’t compatible with a second PC I was developing the project on. It could have worked if I had set up a special distrobox with Debian or something that has the older version but honestly why do all that just for using software that’s years old. However now I have to run the project as root because apparently you can’t access the GPIO pins as a user on anything other than Raspberry OS so it’s not perfect but at least it works.








  • eta@feddit.orgtoLinux@lemmy.ml•*Permanently Deleted*
    link
    fedilink
    English
    arrow-up
    14
    arrow-down
    5
    ·
    27 days ago

    Asking AI in my experience is not always bad but you have to know at least a little about the subject for it to be helpful. If you just tell it “this doesn’t work, fix it” and leave out important information then it comes up with strange solutions.