Skip to content

Commit d360a5a

Browse files
committed
Day 17s
1 parent 9405073 commit d360a5a

21 files changed

Lines changed: 378 additions & 16 deletions

README.md

Lines changed: 3 additions & 3 deletions
Large diffs are not rendered by default.

lib/2020/README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
[Home](../../README.md) | [2015](../2015/README.md) | [2016](../2016/README.md) | [2017](../2017/README.md) | [2018](../2018/README.md) | [2019](../2019/README.md) | 2020 | [2021](../2021/README.md) | [2022](../2022/README.md) | [2023](../2023/README.md) | [2024](../2024/README.md) | [2025](../2025/README.md)
44

5-
## 48/50
5+
## 50/50
66

7-
[recursion](../../wiki/tags/recursion.md) `6` [set](../../wiki/tags/set.md) `4` [walk](../../wiki/tags/walk.md) `4` [regex](../../wiki/tags/regex.md) `3` [geometry2d](../../wiki/tags/geometry2d.md) `2` [grid](../../wiki/tags/grid.md) `2` [memoization](../../wiki/tags/memoization.md) `2` [modular-arithmetic](../../wiki/tags/modular-arithmetic.md) `2` [number-theory](../../wiki/tags/number-theory.md) `2` [rotation](../../wiki/tags/rotation.md) `2` [validation](../../wiki/tags/validation.md) `2` [arithmetic](../../wiki/tags/arithmetic.md) `1` [atomics](../../wiki/tags/atomics.md) `1` [binary-space](../../wiki/tags/binary-space.md) `1` [bitwise](../../wiki/tags/bitwise.md) `1` [chinese-remainder](../../wiki/tags/chinese-remainder.md) `1` [circular-linked-list](../../wiki/tags/circular-linked-list.md) `1` [concurrency](../../wiki/tags/concurrency.md) `1` [count](../../wiki/tags/count.md) `1` [digraph](../../wiki/tags/digraph.md) `1` [dynamic-programming](../../wiki/tags/dynamic-programming.md) `1` [emulation](../../wiki/tags/emulation.md) `1` [erlang](../../wiki/tags/erlang.md) `1` [flood-fill](../../wiki/tags/flood-fill.md) `1` [game](../../wiki/tags/game.md) `1` [graph-traversal](../../wiki/tags/graph-traversal.md) `1` [hexagon](../../wiki/tags/hexagon.md) `1` [mask](../../wiki/tags/mask.md) `1` [operator-precedence](../../wiki/tags/operator-precedence.md) `1` [parse-heavy](../../wiki/tags/parse-heavy.md) `1` [parser](../../wiki/tags/parser.md) `1` [partitioning](../../wiki/tags/partitioning.md) `1` [pattern-matching](../../wiki/tags/pattern-matching.md) `1` [range](../../wiki/tags/range.md) `1` [reduction](../../wiki/tags/reduction.md) `1` [sequence](../../wiki/tags/sequence.md) `1` [simulation](../../wiki/tags/simulation.md) `1` [sliding-window](../../wiki/tags/sliding-window.md) `1` [sort](../../wiki/tags/sort.md) `1` [string](../../wiki/tags/string.md) `1`
7+
[recursion](../../wiki/tags/recursion.md) `6` [set](../../wiki/tags/set.md) `4` [walk](../../wiki/tags/walk.md) `4` [regex](../../wiki/tags/regex.md) `3` [geometry2d](../../wiki/tags/geometry2d.md) `2` [grid](../../wiki/tags/grid.md) `2` [memoization](../../wiki/tags/memoization.md) `2` [modular-arithmetic](../../wiki/tags/modular-arithmetic.md) `2` [number-theory](../../wiki/tags/number-theory.md) `2` [rotation](../../wiki/tags/rotation.md) `2` [simulation](../../wiki/tags/simulation.md) `2` [validation](../../wiki/tags/validation.md) `2` [arithmetic](../../wiki/tags/arithmetic.md) `1` [atomics](../../wiki/tags/atomics.md) `1` [binary-space](../../wiki/tags/binary-space.md) `1` [bitwise](../../wiki/tags/bitwise.md) `1` [chinese-remainder](../../wiki/tags/chinese-remainder.md) `1` [circular-linked-list](../../wiki/tags/circular-linked-list.md) `1` [concurrency](../../wiki/tags/concurrency.md) `1` [count](../../wiki/tags/count.md) `1` [digraph](../../wiki/tags/digraph.md) `1` [dynamic-programming](../../wiki/tags/dynamic-programming.md) `1` [emulation](../../wiki/tags/emulation.md) `1` [erlang](../../wiki/tags/erlang.md) `1` [flood-fill](../../wiki/tags/flood-fill.md) `1` [game](../../wiki/tags/game.md) `1` [game-of-life](../../wiki/tags/game-of-life.md) `1` [graph-traversal](../../wiki/tags/graph-traversal.md) `1` [hexagon](../../wiki/tags/hexagon.md) `1` [infinite-grid](../../wiki/tags/infinite-grid.md) `1` [mask](../../wiki/tags/mask.md) `1` [operator-precedence](../../wiki/tags/operator-precedence.md) `1` [parse-heavy](../../wiki/tags/parse-heavy.md) `1` [parser](../../wiki/tags/parser.md) `1` [partitioning](../../wiki/tags/partitioning.md) `1` [pattern-matching](../../wiki/tags/pattern-matching.md) `1` [range](../../wiki/tags/range.md) `1` [reduction](../../wiki/tags/reduction.md) `1` [sequence](../../wiki/tags/sequence.md) `1` [sliding-window](../../wiki/tags/sliding-window.md) `1` [sort](../../wiki/tags/sort.md) `1` [string](../../wiki/tags/string.md) `1`
88

99
| Day | Title | Difficulty | Tags | Source |
1010
|:---:|-------|:----------:|------|--------|
@@ -24,6 +24,7 @@
2424
| [14](https://adventofcode.com/2020/day/14) | [Docking Data](https://adventofcode.com/2020/day/14) | 🟠 | [bitwise](../../wiki/tags/bitwise.md), [mask](../../wiki/tags/mask.md), [recursion](../../wiki/tags/recursion.md) | [day_14.ex](day_14.ex) |
2525
| [15](https://adventofcode.com/2020/day/15) | [Rambunctious Recitation](https://adventofcode.com/2020/day/15) | 🟠 | [erlang](../../wiki/tags/erlang.md), [memoization](../../wiki/tags/memoization.md), [sequence](../../wiki/tags/sequence.md) | [day_15.ex](day_15.ex) |
2626
| [16](https://adventofcode.com/2020/day/16) | [Ticket Translation](https://adventofcode.com/2020/day/16) | 🟠 | [range](../../wiki/tags/range.md), [validation](../../wiki/tags/validation.md) | [day_16.ex](day_16.ex) |
27+
| [17](https://adventofcode.com/2020/day/17) | [Conway Cubes](https://adventofcode.com/2020/day/17) | 🟠 | [simulation](../../wiki/tags/simulation.md), [game-of-life](../../wiki/tags/game-of-life.md), [infinite-grid](../../wiki/tags/infinite-grid.md) | [day_17.ex](day_17.ex) |
2728
| [18](https://adventofcode.com/2020/day/18) | [Operation Order](https://adventofcode.com/2020/day/18) | 🟠 | [parser](../../wiki/tags/parser.md), [recursion](../../wiki/tags/recursion.md), [operator-precedence](../../wiki/tags/operator-precedence.md) | [day_18.ex](day_18.ex) |
2829
| [19](https://adventofcode.com/2020/day/19) | [Monster Messages](https://adventofcode.com/2020/day/19) | 🔴 | [regex](../../wiki/tags/regex.md), [recursion](../../wiki/tags/recursion.md), [pattern-matching](../../wiki/tags/pattern-matching.md) | [day_19.ex](day_19.ex) |
2930
| [20](https://adventofcode.com/2020/day/20) | [Jurassic Jigsaw](https://adventofcode.com/2020/day/20) | 💀 | [geometry2d](../../wiki/tags/geometry2d.md), [rotation](../../wiki/tags/rotation.md) | [day_20.ex](day_20.ex) |

lib/2020/day_17.ex

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
defmodule AdventOfCode.Y2020.Day17 do
2+
@moduledoc """
3+
--- Day 17: Conway Cubes ---
4+
Problem Link: https://adventofcode.com/2020/day/17
5+
Difficulty: m
6+
Tags: simulation game-of-life infinite-grid
7+
"""
8+
alias AdventOfCode.Helpers.{InputReader, Transformers}
9+
10+
def input, do: InputReader.read_from_file(2020, 17)
11+
12+
def run(input \\ input()) do
13+
initial = parse(input)
14+
{solve(initial, 3), solve(initial, 4)}
15+
end
16+
17+
defp solve(initial, dims) do
18+
state =
19+
initial
20+
|> Enum.map(fn {x, y} ->
21+
if dims == 3, do: {x, y, 0}, else: {x, y, 0, 0}
22+
end)
23+
|> MapSet.new()
24+
25+
Enum.reduce(1..6, state, fn _, acc -> step(acc, dims) end)
26+
|> MapSet.size()
27+
end
28+
29+
defp step(active, dims) do
30+
neighbor_counts =
31+
active
32+
|> Enum.reduce(%{}, fn pos, counts ->
33+
get_neighbors(pos, dims)
34+
|> Enum.reduce(counts, fn n, acc -> Map.update(acc, n, 1, &(&1 + 1)) end)
35+
end)
36+
37+
for {pos, count} <- neighbor_counts,
38+
count == 3 or (count == 2 and MapSet.member?(active, pos)),
39+
into: MapSet.new() do
40+
pos
41+
end
42+
end
43+
44+
defp get_neighbors({x, y, z}, 3) do
45+
for dx <- -1..1, dy <- -1..1, dz <- -1..1, {dx, dy, dz} != {0, 0, 0} do
46+
{x + dx, y + dy, z + dz}
47+
end
48+
end
49+
50+
defp get_neighbors({x, y, z, w}, 4) do
51+
for dx <- -1..1, dy <- -1..1, dz <- -1..1, dw <- -1..1, {dx, dy, dz, dw} != {0, 0, 0, 0} do
52+
{x + dx, y + dy, z + dz, w + dw}
53+
end
54+
end
55+
56+
def parse(data \\ input()) do
57+
data
58+
|> Transformers.lines()
59+
|> Enum.with_index()
60+
|> Enum.flat_map(fn {line, y} ->
61+
line
62+
|> String.graphemes()
63+
|> Enum.with_index()
64+
|> Enum.filter(fn {char, _} -> char == "#" end)
65+
|> Enum.map(fn {_, x} -> {x, y} end)
66+
end)
67+
end
68+
end

lib/2021/README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
[Home](../../README.md) | [2015](../2015/README.md) | [2016](../2016/README.md) | [2017](../2017/README.md) | [2018](../2018/README.md) | [2019](../2019/README.md) | [2020](../2020/README.md) | 2021 | [2022](../2022/README.md) | [2023](../2023/README.md) | [2024](../2024/README.md) | [2025](../2025/README.md)
44

5-
## 26/50
5+
## 28/50
66

7-
[grid](../../wiki/tags/grid.md)&nbsp;`4` [graph](../../wiki/tags/graph.md)&nbsp;`3` [calculation](../../wiki/tags/calculation.md)&nbsp;`2` [rust](../../wiki/tags/rust.md)&nbsp;`2` [set](../../wiki/tags/set.md)&nbsp;`2` [bitwise](../../wiki/tags/bitwise.md)&nbsp;`1` [connectivity](../../wiki/tags/connectivity.md)&nbsp;`1` [dynamic-programming](../../wiki/tags/dynamic-programming.md)&nbsp;`1` [graph-route](../../wiki/tags/graph-route.md)&nbsp;`1` [map](../../wiki/tags/map.md)&nbsp;`1` [memoization](../../wiki/tags/memoization.md)&nbsp;`1` [optimization](../../wiki/tags/optimization.md)&nbsp;`1` [path-finding](../../wiki/tags/path-finding.md)&nbsp;`1` [random-access](../../wiki/tags/random-access.md)&nbsp;`1` [recursion](../../wiki/tags/recursion.md)&nbsp;`1` [sequence](../../wiki/tags/sequence.md)&nbsp;`1` [sliding-window](../../wiki/tags/sliding-window.md)&nbsp;`1` [traversal](../../wiki/tags/traversal.md)&nbsp;`1`
7+
[grid](../../wiki/tags/grid.md)&nbsp;`4` [graph](../../wiki/tags/graph.md)&nbsp;`3` [calculation](../../wiki/tags/calculation.md)&nbsp;`2` [rust](../../wiki/tags/rust.md)&nbsp;`2` [set](../../wiki/tags/set.md)&nbsp;`2` [bitwise](../../wiki/tags/bitwise.md)&nbsp;`1` [connectivity](../../wiki/tags/connectivity.md)&nbsp;`1` [dynamic-programming](../../wiki/tags/dynamic-programming.md)&nbsp;`1` [graph-route](../../wiki/tags/graph-route.md)&nbsp;`1` [map](../../wiki/tags/map.md)&nbsp;`1` [memoization](../../wiki/tags/memoization.md)&nbsp;`1` [optimization](../../wiki/tags/optimization.md)&nbsp;`1` [path-finding](../../wiki/tags/path-finding.md)&nbsp;`1` [physics](../../wiki/tags/physics.md)&nbsp;`1` [random-access](../../wiki/tags/random-access.md)&nbsp;`1` [recursion](../../wiki/tags/recursion.md)&nbsp;`1` [sequence](../../wiki/tags/sequence.md)&nbsp;`1` [simulation](../../wiki/tags/simulation.md)&nbsp;`1` [sliding-window](../../wiki/tags/sliding-window.md)&nbsp;`1` [traversal](../../wiki/tags/traversal.md)&nbsp;`1`
88

99
| Day | Title | Difficulty | Tags | Source |
1010
|:---:|-------|:----------:|------|--------|
@@ -19,5 +19,6 @@
1919
| [12](https://adventofcode.com/2021/day/12) | [Passage Pathing](https://adventofcode.com/2021/day/12) | 🟠 | [graph](../../wiki/tags/graph.md), [traversal](../../wiki/tags/traversal.md), [recursion](../../wiki/tags/recursion.md), [path-finding](../../wiki/tags/path-finding.md) | [day_12.ex](day_12.ex) |
2020
| [15](https://adventofcode.com/2021/day/15) | [Chiton](https://adventofcode.com/2021/day/15) | 🔴 | [graph](../../wiki/tags/graph.md), [graph-route](../../wiki/tags/graph-route.md) | [day_15.ex](day_15.ex) |
2121
| [16](https://adventofcode.com/2021/day/16) | [Packet Decoder](https://adventofcode.com/2021/day/16) | 🟠 | [bitwise](../../wiki/tags/bitwise.md) | [day_16.ex](day_16.ex) |
22+
| [17](https://adventofcode.com/2021/day/17) | [Trick Shot](https://adventofcode.com/2021/day/17) | 🟠 | [simulation](../../wiki/tags/simulation.md), [physics](../../wiki/tags/physics.md) | [day_17.ex](day_17.ex) |
2223
| [21](https://adventofcode.com/2021/day/21) | [Dirac Dice](https://adventofcode.com/2021/day/21) | 💀 | [memoization](../../wiki/tags/memoization.md), [dynamic-programming](../../wiki/tags/dynamic-programming.md) | [day_21.ex](day_21.ex) |
2324
| [25](https://adventofcode.com/2021/day/25) | [Sea Cucumber](https://adventofcode.com/2021/day/25) | 🟠 | [grid](../../wiki/tags/grid.md), [set](../../wiki/tags/set.md) | [day_25.ex](day_25.ex) |

lib/2021/day_17.ex

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
defmodule AdventOfCode.Y2021.Day17 do
2+
@moduledoc """
3+
--- Day 17: Trick Shot ---
4+
Problem Link: https://adventofcode.com/2021/day/17
5+
Difficulty: m
6+
Tags: simulation physics
7+
"""
8+
9+
alias AdventOfCode.Helpers.InputReader
10+
11+
def input, do: InputReader.read_from_file(2021, 17)
12+
13+
def run(input \\ input()) do
14+
{xr, yr} = parse(input)
15+
16+
# Physics: For any positive vy, the probe will return to y=0 with velocity -vy.
17+
# The next step will bring it to y = -(vy + 1).
18+
# To hit the trench, -(vy + 1) must be >= the bottom of the trench (yr.first).
19+
# So max(vy) = abs(yr.first) - 1.
20+
max_vy = abs(yr.first) - 1
21+
max_height = div(max_vy * (max_vy + 1), 2)
22+
23+
hits =
24+
for vx <- 1..xr.last,
25+
vy <- yr.first..abs(yr.first),
26+
hits?({vx, vy}, {xr, yr}),
27+
do: {vx, vy}
28+
29+
{max_height, Enum.count(hits)}
30+
end
31+
32+
defp hits?({vx, vy}, {xr, yr}) do
33+
Stream.iterate({0, 0, vx, vy}, fn {x, y, vx, vy} ->
34+
{x + vx, y + vy, max(0, vx - 1), vy - 1}
35+
end)
36+
|> Enum.reduce_while(nil, fn {x, y, _vx, vy}, _ ->
37+
cond do
38+
x in xr and y in yr -> {:halt, true}
39+
x > xr.last or (y < yr.first and vy < 0) -> {:halt, false}
40+
true -> {:cont, nil}
41+
end
42+
end)
43+
end
44+
45+
def parse(data \\ input()) do
46+
[x1, x2, y1, y2] =
47+
Regex.scan(~r/-?\d+/, data)
48+
|> List.flatten()
49+
|> Enum.map(&String.to_integer/1)
50+
51+
{x1..x2, min(y1, y2)..max(y1, y2)}
52+
end
53+
end

0 commit comments

Comments
 (0)