- 2 Posts
- 32 Comments
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
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
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
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
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
aureleOPto What is this thing?@lemmy.world•A piece of metal in the center of France used as a garden table.English1·9 months agoIt is flat (and round obviously).
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.
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.
aureleto Ask Lemmy@lemmy.world•What item do you own that has an unexpectedly high value?33·1 year agoWhat is the point of cross posting from another Lemmy site when they are interoperable? A “Ask Lemmy” war or competition?
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.
You can run it on Linux through Firejail.
For IPAs: Brooklyn Special Effects and BrewDog Punk AF
Still the case in France, although you can upgrade your “automatic-only” driving license to a regular one by following an additional 7 hour course.
I always go back to https://www.zombo.com/
aureletoHacker News@lemmy.smeargle.fans•Precompiled binaries removed from serde v1.0.1841·2 years agoGlad to see this resolved in the best way possible. This may also lead to better package scrutiny in the future.
If you sell for $10 something that costs $1 to make, is that a 90% profit or a 900% one?
aureleto Open Source@lemmy.ml•GitHub is slowly rolling in 2FA. Any good open source apps that will enable me to activate 2FA token on android?21·2 years agoWell, if you use a password manager such as bitwarden you can store your 2FA one ctrl-v away. Even if this is a less secure setup, that still prevents someone eavesdropping on your password from reusing it.
aureleto Europe@feddit.de•Sweden raises its terror threat to four out of five after Quran burningsEnglish1·2 years agoHow about we stop at obviously malicious attempts of incitement to intercultural hate and violence?
Is such an incitement not an offense in Sweden already? I know it is in France for example.
aureleto Open Source@lemmy.ml•GitHub is slowly rolling in 2FA. Any good open source apps that will enable me to activate 2FA token on android?141·2 years agoWhy would you not want to use 2FA?
Look no further than AtomicUsize in the standard library.