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

help-circle
  • 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
    
    require Logger
    
    
    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
    
    
    Logger.info "Advent of Code 2026 Day 2"
    
    input =
    	File.read!("input/day02.txt")
    	|> String.trim()
    
    Logger.info "Part 1"
    inputTest0 = "obbvb"
    res = AOC.part1(inputTest0)
    if res != 16, do: raise "test result 0 is #{res} but should be 16"
    inputTest1 = "roygbv"
    res = AOC.part1(inputTest1)
    if res != 11, do: raise "test result 1 is #{res} but should be 11"
    inputTest2 = "vbgyor"
    res = AOC.part1(inputTest2)
    if res != 30, do: raise "test result 2 is #{res} but should be 30"
    inputTest3 = "vvvvvv"
    res = AOC.part1(inputTest3)
    if res != 35, do: raise "test result 3 is #{res} but should be 35"
    res = AOC.part1(input)
    Logger.info "answer: #{res}"
    
    Logger.info "Part 2"
    inputTest0 = "rybryb"
    res = AOC.part2(inputTest0)
    if res != 32, do: raise "test result 0 is #{res} but should be 32"
    inputTest1 = "vrvbvg"
    res = AOC.part2(inputTest1)
    if res != 33, do: raise "test result 1 is #{res} but should be 33"
    inputTest2 = "groovyyoyo"
    res = AOC.part2(inputTest2)
    if res != 55, do: raise "test result 2 is #{res} but should be 55"
    inputTest3 = "roygbvroygbvroygbv"
    res = AOC.part2(inputTest3)
    if res != 103, do: raise "test result 3 is #{res} but should be 103"
    res = AOC.part2(input)
    Logger.info "answer: #{res}"
    



  • 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
    ·
    24 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.