-
-
Save tfwright/660de2ab982fc4f741ce8566e6ffadc2 to your computer and use it in GitHub Desktop.
AOC 2022 11
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
System.argv() | |
|> List.first() | |
|> File.stream!() | |
|> Enum.reduce({1, 1, 0}, fn instruction, {x, cycle, total_strength} -> | |
{new_x, new_cycle} = | |
case instruction do | |
"noop" <> _ -> | |
{x, cycle + 1} | |
"addx " <> val -> | |
[neg, amt] = | |
~r/(\-)?(\d{1,2}).*/ | |
|> Regex.run(val, capture: :all_but_first) | |
amt = String.to_integer(amt) | |
{x + (if neg == "-", do: amt |> Kernel.*(-1), else: amt), cycle + 2} | |
end | |
new_total_strength = | |
[20, 60, 100, 140, 180, 220] | |
|> Enum.find(fn counted_cycle -> cycle <= counted_cycle && new_cycle > counted_cycle end) | |
|> case do | |
nil -> total_strength | |
counted_cycle -> total_strength + counted_cycle * x | |
end | |
{new_x, new_cycle, new_total_strength} | |
end) | |
|> elem(2) | |
|> IO.inspect() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
crt_grid = | |
"." | |
|> List.duplicate(40) | |
|> List.duplicate(6) | |
divmod = | |
fn e, d -> | |
~w|div rem|a | |
|> Enum.map(&apply(Kernel, &1, [e, d])) | |
|> List.to_tuple() | |
end | |
System.argv() | |
|> List.first() | |
|> File.stream!() | |
|> Enum.reduce({crt_grid, 1, 1}, fn instruction, {grid, x, cycle} -> | |
{new_x, new_cycle} = | |
case instruction do | |
"noop" <> _ -> | |
{x, cycle + 1} | |
"addx " <> val -> | |
[neg, amt] = | |
~r/(\-)?(\d{1,2}).*/ | |
|> Regex.run(val, capture: :all_but_first) | |
amt = String.to_integer(amt) | |
{x + (if neg == "-", do: amt |> Kernel.*(-1), else: amt), cycle + 2} | |
end | |
new_grid = | |
(cycle - 1)..(new_cycle - 2) | |
|> Enum.reduce(grid, fn position, grid -> | |
{pixel_x, pixel_y} = divmod.(position, 40) | |
if pixel_y in (x - 1)..(x + 1) do | |
List.update_at(grid, pixel_x, & List.replace_at(&1, pixel_y, "#")) | |
else | |
grid | |
end | |
end) | |
{new_grid, new_x, new_cycle} | |
end) | |
|> elem(0) | |
|> Enum.map(&Enum.join/1) | |
|> IO.inspect |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment