Skip to content

Commit

Permalink
Solve 2023/25
Browse files Browse the repository at this point in the history
  • Loading branch information
code-shoily committed Dec 29, 2023
1 parent 5072e19 commit e490cf2
Show file tree
Hide file tree
Showing 7 changed files with 1,317 additions and 4 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,11 @@ Note that you can call those commands independently and also set up pre-commit h

Note: All files mentioned above are autogenerated and are created by running `mix update_stats` task.

## :trophy: 283/450
## :trophy: 285/450

| Day | [2015](/lib/2015) | [2016](/lib/2016) | [2017](/lib/2017) | [2018](/lib/2018) | [2019](/lib/2019) | [2020](/lib/2020) | [2021](/lib/2021) | [2022](/lib/2022) | [2023](/lib/2023) |
|:---:|:-:|:-:|:-:|:-:|:-:|:-:|:-:|:-:|:-:|
| :star2: | 48 | 22 | 34 | 15 | 15 | 44 | 24 | 45 | 36 |
| :star2: | 48 | 22 | 34 | 15 | 15 | 44 | 24 | 45 | 38 |
| 1 | :1st_place_medal: | :1st_place_medal: | :1st_place_medal: | :1st_place_medal: | :1st_place_medal: | :1st_place_medal: | :1st_place_medal: | :1st_place_medal: | :1st_place_medal: |
| 2 | :1st_place_medal: | :1st_place_medal: | :1st_place_medal: | :1st_place_medal: | :1st_place_medal: | :1st_place_medal: | :1st_place_medal: | :1st_place_medal: | :1st_place_medal: |
| 3 | :1st_place_medal: | :1st_place_medal: | :1st_place_medal: | :1st_place_medal: | :1st_place_medal: | :1st_place_medal: | :1st_place_medal: | :1st_place_medal: | :1st_place_medal: |
Expand All @@ -90,6 +90,6 @@ Note: All files mentioned above are autogenerated and are created by running `mi
| 22 | | | | | | :1st_place_medal: | | :2nd_place_medal: | |
| 23 | :1st_place_medal: | | :2nd_place_medal: | | | :2nd_place_medal: | | :1st_place_medal: | |
| 24 | :1st_place_medal: | | | | | :2nd_place_medal: | | :1st_place_medal: | :1st_place_medal: |
| 25 | :1st_place_medal: | | | | | :1st_place_medal: | :1st_place_medal: | :1st_place_medal: | |
| 25 | :1st_place_medal: | | | | | :1st_place_medal: | :1st_place_medal: | :1st_place_medal: | :1st_place_medal: |


1 change: 1 addition & 0 deletions difficulties.md
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ optimize them and update this page with refined and more accurate data.
| 2023/5 | [If You Give A Seed A Fertilizer](https://adventofcode.com/2023/day/5) | :1st_place_medal: | :snowflake: :snowflake: :snowflake: :snowflake: :snowflake: | [day_05.ex](/lib/2023/day_05.ex) | [day_05_test.exs](/test/2023/day_05_test.exs) | [range](/tags.md#range), [gb-tree](/tags.md#gb-tree) |
| 2023/10 | [Pipe Maze](https://adventofcode.com/2023/day/10) | :1st_place_medal: | :snowflake: :snowflake: :snowflake: :snowflake: :snowflake: | [day_10.ex](/lib/2023/day_10.ex) | [day_10_test.exs](/test/2023/day_10_test.exs) | [graph](/tags.md#graph), [graph-traversal](/tags.md#graph-traversal), [needs-improvement](/tags.md#needs-improvement), [not-fast-enough](/tags.md#not-fast-enough) |
| 2023/12 | [Hot Springs](https://adventofcode.com/2023/day/12) | :1st_place_medal: | :snowflake: :snowflake: :snowflake: :snowflake: :snowflake: | [day_12.ex](/lib/2023/day_12.ex) | [day_12_test.exs](/test/2023/day_12_test.exs) | [memoization](/tags.md#memoization), [vector](/tags.md#vector) |
| 2023/25 | [Snowverload](https://adventofcode.com/2023/day/25) | :1st_place_medal: | :snowflake: :snowflake: :snowflake: :snowflake: :snowflake: | [day_25.ex](/lib/2023/day_25.ex) | [day_25_test.exs](/test/2023/day_25_test.exs) | [graph](/tags.md#graph), [min-cut](/tags.md#min-cut), [probabilistic](/tags.md#probabilistic), [refactor](/tags.md#refactor), [not-fast-enough](/tags.md#not-fast-enough) |

## xxl

Expand Down
3 changes: 2 additions & 1 deletion lib/2023/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
[2015](/lib/2015) | [2016](/lib/2016) | [2017](/lib/2017) | [2018](/lib/2018) | [2019](/lib/2019) | [2020](/lib/2020) | [2021](/lib/2021) | [2022](/lib/2022) | 2023


## :trophy: 36/50
## :trophy: 38/50

| Day | Problem Page | Status | Difficulty | Solution Page | Test Page | Tags |
| :---: | :------: | :---: | :---: | :---: | :---: | :---: |
Expand All @@ -28,4 +28,5 @@
| 16 | [The Floor Will Be Lava](https://adventofcode.com/2023/day/16) | :1st_place_medal: | :snowflake: :snowflake: :snowflake: :snowflake: | [day_16.ex](/lib/2023/day_16.ex) | [day_16_test.exs](/test/2023/day_16_test.exs) | [grid](/tags.md#grid), [grid-walk](/tags.md#grid-walk), [memoization](/tags.md#memoization) |
| 18 | [Lavaduct Lagoon](https://adventofcode.com/2023/day/18) | :1st_place_medal: | :snowflake: :snowflake: :snowflake: :snowflake: | [day_18.ex](/lib/2023/day_18.ex) | [day_18_test.exs](/test/2023/day_18_test.exs) | [geometry](/tags.md#geometry) |
| 24 | [Never Tell Me The Odds](https://adventofcode.com/2023/day/24) | :1st_place_medal: | :snowflake: :snowflake: :snowflake: :snowflake: :snowflake: :snowflake: | [day_24.ex](/lib/2023/day_24.ex) | [day_24_test.exs](/test/2023/day_24_test.exs) | [geometry2d](/tags.md#geometry2d), [refactor](/tags.md#refactor) |
| 25 | [Snowverload](https://adventofcode.com/2023/day/25) | :1st_place_medal: | :snowflake: :snowflake: :snowflake: :snowflake: :snowflake: | [day_25.ex](/lib/2023/day_25.ex) | [day_25_test.exs](/test/2023/day_25_test.exs) | [graph](/tags.md#graph), [min-cut](/tags.md#min-cut), [probabilistic](/tags.md#probabilistic), [refactor](/tags.md#refactor), [not-fast-enough](/tags.md#not-fast-enough) |

80 changes: 80 additions & 0 deletions lib/2023/day_25.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
defmodule AdventOfCode.Y2023.Day25 do
@moduledoc """
--- Day 25: Snowverload ---
Problem Link: https://adventofcode.com/2023/day/25
Difficulty: xl
Tags: graph min-cut probabilistic refactor not-fast-enough
"""
alias AdventOfCode.Helpers.{InputReader, Transformers}

def input, do: InputReader.read_from_file(2023, 25)

def run(input \\ input()) do
input = parse(input)

{run_1(input), run_2(input)}
end

defp run_1(input) do
Stream.repeatedly(fn -> contract(input) end)
|> Enum.find(fn g1 -> cut_size(g1, input) == 3 end)
|> Map.keys()
|> Enum.map(&String.length/1)
|> Enum.map(&div(&1, 3))
|> Enum.product()
end

defp run_2(_), do: "🎉"

def parse(input) do
for line <- Transformers.lines(input), reduce: %{} do
graph ->
[u | connected] = String.split(line, [":", " "], trim: true)

for v <- connected, reduce: graph do
edges ->
edges
|> Map.update(u, MapSet.new([v]), &MapSet.put(&1, v))
|> Map.update(v, MapSet.new([u]), &MapSet.put(&1, u))
end
end
end

def cut_size(g, h) do
for {key, _} <- g do
key
|> String.to_charlist()
|> Enum.chunk_every(3)
|> Enum.map(&to_string/1)
end
|> then(fn [us, vs] ->
for u <- us, _ <- MapSet.intersection(h[u], MapSet.new(vs)), reduce: 0 do
acc -> acc + 1
end
end)
end

def contract(graph) when map_size(graph) == 2, do: graph

def contract(graph) do
{u, u_edges} = Enum.random(graph)
v = Enum.random(u_edges)

u_edges = u_edges |> MapSet.delete(v)
v_edges = graph[v] |> MapSet.delete(u)
u_v_edges = MapSet.union(u_edges, v_edges)
uv = u <> v

Enum.reduce(u_edges, graph, fn c, acc ->
Map.update!(acc, c, &MapSet.put(MapSet.delete(&1, u), uv))
end)
|> then(fn graph ->
Enum.reduce(v_edges, graph, fn c, acc ->
Map.update!(acc, c, &MapSet.put(MapSet.delete(&1, v), uv))
end)
|> Map.drop([u, v])
|> Map.put(uv, u_v_edges)
end)
|> contract()
end
end
Loading

0 comments on commit e490cf2

Please sign in to comment.