stifle867

joined 1 year ago
[–] [email protected] 1 points 1 year ago

I did this exact thing and hit the point where it didn't work. I appreciated that the problem broke my code because it made me arrive at a better solution.

[–] [email protected] 4 points 1 year ago (5 children)

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.

[–] [email protected] 5 points 1 year ago

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.

[–] [email protected] 1 points 1 year ago

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

[–] [email protected] 3 points 1 year ago

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

[–] [email protected] 2 points 1 year ago (1 children)

I just added this to my test case and when it passed the solution also passed:

assert part2("12oneightfve") === 18

[–] [email protected] 7 points 1 year ago (9 children)

The problem is the example is actually eightwothree which comes out as 83 so if you replace from start to finish the example passes but the solution is incorrect.

[–] [email protected] 12 points 1 year ago (12 children)

It's great! I've had community members point me in the right direction after already "solving" it incorrectly. It really makes you think about it. You have to expand your test cases and really come up with a better solution.

Not to say it isn't difficult especially if you expect the problem to be described perfectly accurately.

[–] [email protected] 3 points 1 year ago

Thank you for the test case! I went out for dinner but I'll work on this tomorrow. I strongly suspect this is the exact issue but I'm going to have to rework some things so I want to tackle it with a fresh mind.

[–] [email protected] 2 points 1 year ago

I think you may be right but the problem is at the end of the string. I'll add some test cases and rewrite the code. I think I'll have to ditch the regex replacements and scan through instead so I don't clobber the string in the wrong place.

[–] [email protected] 6 points 1 year ago

I found a number of articles specifically stating that video games and software remain illegal. Unfortunately I couldn't pinpoint the specific part of the law as they appear to br written in French and was running into hurdles with Google Translate character limits that I couldn't be bothered to work around.

I'm not sure if it's explicitly illegal or if music, videos, etc are explicitly exempted, or if software etc is different due to terms of service for example.

Furthermore, it's illegal for anyone to record your IP address torrenting a work and track you down that way as it violates Swiss data protection laws.

[–] [email protected] 35 points 1 year ago (5 children)

In Switzerland, individuals are still free to download whatever files they like for their own private use (except for software and video games).

Note that uploading or seeding a copyrighted work, which was a misdemeanor under the previous law, remains illegal.

source: With P2P law, Switzerland reaffirms its commitment to privacy

view more: ‹ prev next ›