• 1 Post
  • 234 Comments
Joined 9 months ago
cake
Cake day: October 5th, 2023

help-circle










  • Im theory you could try counting CPU cycles or something analogous, or algorithmic complexity as the other commenter mentioned. You could also try to measure it in comparison to different code.

    Outside of that, and in any practical sense, no you can’t. Performance measurements are dependent on the machine as much as the software.

    If a car is hardware, and the driver is software, how would you measure the performance of the driver in the different cars? The only way I can think of is if you had 2 different drivers and could compare their times in both cars. If driver 1 is 2x as fast in car 1, and 2x as fast in car 2, you could say driver 1 has a score 2x higher than driver 2.


  • I’m not really sure how to interpret your comment but I’ll try my best. The edge case that causes some solutions to fail does not have any definition on how to handle it on the problem page. In other words, it does not state anywhere whether the correct interpretation of 1threeight is meant to be 18 or 13. If your solution replaces the words to numbers from left to right you end up with 13 as the value but it’s meant to be 18.

    The example answers don’t cover this but you will realise something is wrong if you run it against your full problem. Community has been very helpful on providing pointers.


  • I arrived at the following solution for Day #1:

    https://pastebin.com/u1SYJ4tY
    defmodule AdventOfCode.Day01 do
      def part1(args) do
        number_regex = ~r/([0-9])/
    
        args
        |> String.split(~r/\n/, trim: true)
        |> Enum.map(&first_and_last_number(&1, number_regex))
        |> Enum.map(&number_list_to_integer/1)
        |> Enum.sum()
      end
    
      def part2(args) do
        number_regex = ~r/(?=(one|two|three|four|five|six|seven|eight|nine|[0-9]))/
    
        args
        |> String.split(~r/\n/, trim: true)
        |> Enum.map(&first_and_last_number(&1, number_regex))
        |> Enum.map(fn number -> Enum.map(number, &replace_word_with_number/1) end)
        |> Enum.map(&number_list_to_integer/1)
        |> Enum.sum()
      end
    
      defp first_and_last_number(string, regex) do
        matches = Regex.scan(regex, string)
        [_, first] = List.first(matches)
        [_, last] = List.last(matches)
    
        [first, last]
      end
    
      defp number_list_to_integer(list) do
        list
        |> List.to_string()
        |> String.to_integer()
      end
    
      defp replace_word_with_number(string) do
        numbers = ["one", "two", "three", "four", "five", "six", "seven", "eight", "nine"]
    
        String.replace(string, numbers, fn x ->
          (Enum.find_index(numbers, &(&1 == x)) + 1)
          |> Integer.to_string()
        end)
      end
    end
    

  • My solutin in Elixir for both part 1 and part 2 is below. It does use regex and with that there are many different ways to accomplish the goal. I’m no regex master so I made it as simple as possible and relied on the language a bit more. I’m sure there are cooler solutions with no regex too, this is just what I settled on:

    https://pastebin.com/u1SYJ4tY
    defmodule AdventOfCode.Day01 do
      def part1(args) do
        number_regex = ~r/([0-9])/
    
        args
        |> String.split(~r/\n/, trim: true)
        |> Enum.map(&first_and_last_number(&1, number_regex))
        |> Enum.map(&number_list_to_integer/1)
        |> Enum.sum()
      end
    
      def part2(args) do
        number_regex = ~r/(?=(one|two|three|four|five|six|seven|eight|nine|[0-9]))/
    
        args
        |> String.split(~r/\n/, trim: true)
        |> Enum.map(&first_and_last_number(&1, number_regex))
        |> Enum.map(fn number -> Enum.map(number, &replace_word_with_number/1) end)
        |> Enum.map(&number_list_to_integer/1)
        |> Enum.sum()
      end
    
      defp first_and_last_number(string, regex) do
        matches = Regex.scan(regex, string)
        [_, first] = List.first(matches)
        [_, last] = List.last(matches)
    
        [first, last]
      end
    
      defp number_list_to_integer(list) do
        list
        |> List.to_string()
        |> String.to_integer()
      end
    
      defp replace_word_with_number(string) do
        numbers = ["one", "two", "three", "four", "five", "six", "seven", "eight", "nine"]
    
        String.replace(string, numbers, fn x ->
          (Enum.find_index(numbers, &(&1 == x)) + 1)
          |> Integer.to_string()
        end)
      end
    end