aurele

joined 2 years ago
[โ€“] aurele 1 points 3 weeks ago (1 children)

Elixir

defmodule AdventOfCode.Solution.Year2024.Day01 do
  use AdventOfCode.Solution.SharedParse

  @impl true
  def parse(input) do
    numbers =
      input
      |> String.split("\n", trim: true)
      |> Enum.map(fn l -> String.split(l, ~r/\s+/) |> Enum.map(&String.to_integer/1) end)

    {Stream.map(numbers, &Enum.at(&1, 0)), Stream.map(numbers, &Enum.at(&1, 1))}
  end

  def part1({left, right}) do
    Enum.zip_reduce(Enum.sort(left), Enum.sort(right), 0, &(&3 + abs(&1 - &2)))
  end

  def part2({left, right}) do
    freq = Enum.frequencies(right)
    left |> Stream.map(&(&1 * Map.get(freq, &1, 0))) |> Enum.sum()
  end
end
[โ€“] aurele 1 points 3 weeks ago

Elixir

defmodule AdventOfCode.Solution.Year2024.Day02 do
  use AdventOfCode.Solution.SharedParse

  @impl true
  def parse(input) do
    for line <- String.split(input, "\n", trim: true),
        do: String.split(line) |> Enum.map(&String.to_integer/1)
  end

  def part1(input) do
    Enum.count(input, &is_safe(&1, false))
  end

  def part2(input) do
    Enum.count(input, &(is_safe(&1, true) or is_safe(tl(&1), false)))
  end

  def is_safe([a, b, c | rest], can_fix) do
    cond do
      (b - a) * (c - b) > 0 and abs(b - a) in 1..3 and abs(c - b) in 1..3 ->
        is_safe([b, c | rest], can_fix)

      can_fix ->
        is_safe([a, c | rest], false) or is_safe([a, b | rest], false)

      true ->
        false
    end
  end

  def is_safe(_, _), do: true
end
[โ€“] aurele 1 points 3 weeks ago

Elixir

defmodule AdventOfCode.Solution.Year2024.Day03 do
  def part1(input) do
    Regex.scan(~r/mul\((?<l>\d+),(?<r>\d+)\)/, input, capture: ["l", "r"])
    |> Stream.map(fn l -> Enum.reduce(l, 1, &(&2 * String.to_integer(&1))) end)
    |> Enum.sum()
  end

  def part2(input) do
    input |> String.replace(~r/don't\(\).*(do\(\)|$)/Us, "") |> part1
  end
end
[โ€“] aurele 1 points 3 weeks ago

Elixir

defmodule AdventOfCode.Solution.Year2024.Day04 do
  use AdventOfCode.Solution.SharedParse

  defmodule Map do
    defstruct [:chars, :width, :height]
  end

  @impl true
  def parse(input) do
    chars = String.split(input, "\n", trim: true) |> Enum.map(&String.codepoints/1)
    %Map{chars: chars, width: length(Enum.at(chars, 0)), height: length(chars)}
  end

  def at(%Map{} = map, x, y) do
    cond do
      x < 0 or x >= map.width or y < 0 or y >= map.height -> ""
      true -> map.chars |> Enum.at(y, []) |> Enum.at(x, "")
    end
  end

  def part1(map) do
    dirs = for dx <- -1..1, dy <- -1..1, {dx, dy} != {0, 0}, do: {dx, dy}
    xmas = String.codepoints("XMAS") |> Enum.with_index() |> Enum.drop(1)

    for x <- 0..(map.width - 1),
        y <- 0..(map.height - 1),
        "X" == at(map, x, y),
        {dx, dy} <- dirs,
        xmas
        |> Enum.all?(fn {c, n} -> at(map, x + dx * n, y + dy * n) == c end),
        reduce: 0 do
      t -> t + 1
    end
  end

  def part2(map) do
    for x <- 0..(map.width - 1),
        y <- 0..(map.height - 1),
        "A" == at(map, x, y),
        (at(map, x - 1, y - 1) <> at(map, x + 1, y + 1)) in ["MS", "SM"],
        (at(map, x - 1, y + 1) <> at(map, x + 1, y - 1)) in ["MS", "SM"],
        reduce: 0 do
      t -> t + 1
    end
  end
end
[โ€“] aurele 1 points 3 weeks ago

Elixir

defmodule AdventOfCode.Solution.Year2024.Day05 do
  use AdventOfCode.Solution.SharedParse

  @impl true
  def parse(input) do
    [rules, pages_list] =
      String.split(input, "\n\n", limit: 2) |> Enum.map(&String.split(&1, "\n", trim: true))

    {for(rule <- rules, do: String.split(rule, "|") |> Enum.map(&String.to_integer/1))
     |> MapSet.new(),
     for(pages <- pages_list, do: String.split(pages, ",") |> Enum.map(&String.to_integer/1))}
  end

  def part1({rules, pages_list}), do: solve(rules, pages_list, false)

  def part2({rules, pages_list}), do: solve(rules, pages_list, true)

  def solve(rules, pages_list, negate) do
    for pages <- pages_list, reduce: 0 do
      total ->
        ordered = Enum.sort(pages, &([&1, &2] in rules))

        if negate != (ordered == pages),
          do: total + Enum.at(ordered, div(length(ordered), 2)),
          else: total
    end
  end
end
[โ€“] aurele 1 points 4 months ago

It is flat (and round obviously).

 
[โ€“] aurele 2 points 7 months ago

I've always had the impression that images are underutilized in Factor, or not needed, compared to Smalltalk for example. At least their size will now be more manageable.

[โ€“] aurele 10 points 9 months ago

Same default settings in France, although your organs can only be used for transplant. Using them for teaching and practicing in medical school still needs your explicit (prior) consent.

[โ€“] aurele 0 points 9 months ago (3 children)

What is the point of cross posting from another Lemmy site when they are interoperable? A "Ask Lemmy" war or competition?

[โ€“] aurele 12 points 1 year ago* (last edited 1 year ago) (2 children)

Except that if people are chosen randomly there is 2/3 chance that you are on the main track according to Bayes. Let's assume there are 10 people.

The probability to be chosen is 1/6 (all are chosen if 6 is rolled) + (5/6) ร— (1/10) (only one is chosen to go to the side track if 1-5 is rolled) = 15/60 = 1/4.

The probability that you are on the side track knowing that you have been chosen is the probability that you have been chosen knowing that the side track is selected (1/10) ร— the probability that the side track is selected (5/6) divided by the probability for you to be selected at all (1/4), so (1/10)ร—(5/6)/(1/4) = 20/60 = 1/3. So there is a 2/3 chance that you are on the main track.

If you do not flip the switch, (2/3)ร—10 = 20/3 people die.

If you flip the switch, 1/3 (you if on side track) + 10 ร— 2/3 ร— 9 / 10 (switch misfires 9 out of 10 times if on the main track) = 190/30 = 19/3 die. This is slightly better than not flipping the switch, you save 1/3 people more. That's an arm and a leg.

[โ€“] aurele 18 points 1 year ago (2 children)

You can run it on Linux through Firejail.

[โ€“] aurele 5 points 1 year ago (1 children)

For IPAs: Brooklyn Special Effects and BrewDog Punk AF

view more: next โ€บ