mix format

This commit is contained in:
Caleb Webber 2023-12-02 02:11:54 -05:00
parent 7c0ebe4c50
commit d6fce584fd
5 changed files with 175 additions and 131 deletions

View file

@ -1,11 +1,14 @@
defmodule Day1 do defmodule Day1 do
def get_answer(state) do def get_answer(state) do
{_, total, part} = state {_, total, part} = state
total |>
then(fn total -> case part do total
|> then(fn total ->
case part do
:part1 -> total :part1 -> total
:part2 -> total |> Enum.reduce(0, fn i, acc -> i + acc end) :part2 -> total |> Enum.reduce(0, fn i, acc -> i + acc end)
end end) end
end)
end end
def init_state(part) do def init_state(part) do
@ -18,7 +21,8 @@ defmodule Day1 do
def execute(line, state) do def execute(line, state) do
apply_command( apply_command(
line |> to_command(), line |> to_command(),
state) state
)
end end
defp apply_command(command, state) do defp apply_command(command, state) do
@ -38,19 +42,27 @@ defmodule Day1 do
defp apply_add(n, state) do defp apply_add(n, state) do
{current, total, part} = state {current, total, part} = state
new_current = n + if is_nil(current) do 0 else current end
new_current =
n +
if is_nil(current) do
0
else
current
end
{new_current, total, part} {new_current, total, part}
end end
defp take_top_three(n, list) do defp take_top_three(n, list) do
[ n | list ] |> [n | list]
Enum.sort(:desc) |> |> Enum.sort(:desc)
Enum.take(3) |> Enum.take(3)
end end
defp apply_reset(state) do defp apply_reset(state) do
{current, total, part} = state {current, total, part} = state
if !is_nil(current) do if !is_nil(current) do
case part do case part do
:part1 -> {nil, max(total, current), part} :part1 -> {nil, max(total, current), part}
@ -61,4 +73,3 @@ defmodule Day1 do
end end
end end
end end

View file

@ -4,11 +4,11 @@ defmodule Day10 do
@behaviour Solution @behaviour Solution
def init_state(p) when p == :part1 do def init_state(p) when p == :part1 do
%{ vm: VirtualMachine.new(), acc: 0, p: p } %{vm: VirtualMachine.new(), acc: 0, p: p}
end end
def init_state(p) when p == :part2 do def init_state(p) when p == :part2 do
%{ vm: VirtualMachine.new(), crt: "", p: p} %{vm: VirtualMachine.new(), crt: "", p: p}
end end
def execute(line, state) when line == "" do def execute(line, state) when line == "" do
@ -16,42 +16,48 @@ defmodule Day10 do
end end
def execute(line, state) when state.p == :part1 do def execute(line, state) when state.p == :part1 do
VirtualMachine.plan_execute(line) |> VirtualMachine.plan_execute(line)
Enum.reduce( |> Enum.reduce(
state, state,
fn (tock, %{vm: vm, acc: acc}) -> fn tock, %{vm: vm, acc: acc} ->
next_tick = vm.tick + 1 next_tick = vm.tick + 1
acc = if (next_tick - 20 |> mod(40)) === 0, do: acc + (next_tick) * vm.x, else: acc acc = if (next_tick - 20) |> mod(40) === 0, do: acc + next_tick * vm.x, else: acc
vm = tock.(vm) vm = tock.(vm)
%{ vm: vm, acc: acc} %{vm: vm, acc: acc}
end end
) |> Map.put(:p, state.p) )
|> Map.put(:p, state.p)
end end
def execute(line, state) when state.p == :part2 do def execute(line, state) when state.p == :part2 do
VirtualMachine.plan_execute(line) |> VirtualMachine.plan_execute(line)
Enum.reduce( |> Enum.reduce(
state, state,
fn (tock, %{vm: vm, crt: crt}) -> fn tock, %{vm: vm, crt: crt} ->
next_tick = vm.tick + 1 next_tick = vm.tick + 1
char = case mod(vm.tick, 40) do char =
x when x >= (vm.x - 1) and x <= (vm.x + 1) -> "#" case mod(vm.tick, 40) do
x when x >= vm.x - 1 and x <= vm.x + 1 -> "#"
_ -> "." _ -> "."
end end
crt = crt <> char crt = crt <> char
crt = crt <> if mod(next_tick, 40) == 0 do crt =
crt <>
if mod(next_tick, 40) == 0 do
"\n" "\n"
else "" else
""
end end
vm = tock.(vm) vm = tock.(vm)
%{ vm: vm, crt: crt} %{vm: vm, crt: crt}
end end
) |> Map.put(:p, state.p) )
|> Map.put(:p, state.p)
end end
def get_answer(state) when state.p == :part1 do def get_answer(state) when state.p == :part1 do
@ -65,11 +71,12 @@ end
defmodule VirtualMachine do defmodule VirtualMachine do
def new() do def new() do
%{ x: 1, tick: 0 } %{x: 1, tick: 0}
end end
def plan_execute(instruction) do def plan_execute(instruction) do
[instruction, args] = if instruction == "noop" do [instruction, args] =
if instruction == "noop" do
[instruction, nil] [instruction, nil]
else else
String.split(instruction, " ") String.split(instruction, " ")
@ -79,25 +86,27 @@ defmodule VirtualMachine do
end end
def plan_execute(instruction, args) when instruction == "addx" do def plan_execute(instruction, args) when instruction == "addx" do
args = case Integer.parse(args) do args =
case Integer.parse(args) do
{args, _} -> args {args, _} -> args
:error -> 0 :error -> 0
end end
if is_integer(args) do if is_integer(args) do
plan_execute("noop", nil) ++ plan_execute("noop", nil) ++
[ [
fn (vm) -> fn vm ->
vm |> vm
Map.update(:tick, 2, &(&1 + 1)) |> |> Map.update(:tick, 2, &(&1 + 1))
Map.update(:x, args, &(&1 + args)) |> Map.update(:x, args, &(&1 + args))
end end
] ]
else [] else
[]
end end
end end
def plan_execute(instruction, _) when instruction == "noop" do def plan_execute(instruction, _) when instruction == "noop" do
[fn (vm) -> Map.update(vm, :tick, 1, &(&1 + 1)) end] [fn vm -> Map.update(vm, :tick, 1, &(&1 + 1)) end]
end end
end end

View file

@ -8,7 +8,7 @@ defmodule Day2 do
end end
def execute(line, state) do def execute(line, state) do
if line |> String.trim() |> String.length == 0 do if line |> String.trim() |> String.length() == 0 do
state state
else else
{_, player, outcome} = parse_moves(line, state.part) {_, player, outcome} = parse_moves(line, state.part)
@ -17,7 +17,8 @@ defmodule Day2 do
state.score + state.score +
points_for_move(player) + points_for_move(player) +
points_from_outcome(outcome), points_from_outcome(outcome),
state.part) state.part
)
end end
end end
@ -48,15 +49,20 @@ defmodule Day2 do
opposing_move opposing_move
else else
case opposing_move do case opposing_move do
:rock -> case outcome do :rock ->
case outcome do
:win -> :paper :win -> :paper
:loss -> :scissors :loss -> :scissors
end end
:paper -> case outcome do
:paper ->
case outcome do
:win -> :scissors :win -> :scissors
:loss -> :rock :loss -> :rock
end end
:scissors -> case outcome do
:scissors ->
case outcome do
:win -> :rock :win -> :rock
:loss -> :paper :loss -> :paper
end end
@ -64,15 +70,17 @@ defmodule Day2 do
end end
end end
defp parse_move(move, side) do defp parse_move(move, side) do
case side do case side do
:left -> case move do :left ->
case move do
"A" -> :rock "A" -> :rock
"B" -> :paper "B" -> :paper
"C" -> :scissors "C" -> :scissors
end end
:right -> case move do
:right ->
case move do
"X" -> :rock "X" -> :rock
"Y" -> :paper "Y" -> :paper
"Z" -> :scissors "Z" -> :scissors
@ -93,15 +101,20 @@ defmodule Day2 do
0 0
else else
case a do case a do
:rock -> case b do :rock ->
case b do
:paper -> -1 :paper -> -1
:scissors -> 1 :scissors -> 1
end end
:paper -> case b do
:paper ->
case b do
:rock -> 1 :rock -> 1
:scissors -> -1 :scissors -> -1
end end
:scissors -> case b do
:scissors ->
case b do
:rock -> -1 :rock -> -1
:paper -> 1 :paper -> 1
end end
@ -111,6 +124,7 @@ defmodule Day2 do
defp outcome?(match) when is_tuple(match) and tuple_size(match) == 2 do defp outcome?(match) when is_tuple(match) and tuple_size(match) == 2 do
{opponent, player} = match {opponent, player} = match
case sort_(player, opponent) do case sort_(player, opponent) do
1 -> :win 1 -> :win
0 -> :draw 0 -> :draw

View file

@ -4,16 +4,24 @@ defmodule Day3 do
end end
defp find_duplicates(line) do defp find_duplicates(line) do
line |> line
String.to_charlist() |> |> String.to_charlist()
Enum.chunk_every(Integer.floor_div(String.length(line), 2)) |> |> Enum.chunk_every(Integer.floor_div(String.length(line), 2))
Enum.map(&MapSet.new/1) |> |> Enum.map(&MapSet.new/1)
then(fn [a,b] -> MapSet.intersection(a,b) end) |> |> then(fn [a, b] -> MapSet.intersection(a, b) end)
MapSet.to_list() |> MapSet.to_list()
end end
defp to_priority(c) do defp to_priority(c) do
if is_nil(c) do 0 else if c < 97 do c - 38 else c - 96 end end if is_nil(c) do
0
else
if c < 97 do
c - 38
else
c - 96
end
end
end end
def execute(line, state) do def execute(line, state) do
@ -22,11 +30,11 @@ defmodule Day3 do
trimmed = line |> String.trim() trimmed = line |> String.trim()
unless String.length(trimmed) == 0 do unless String.length(trimmed) == 0 do
trimmed |> trimmed
find_duplicates() |> |> find_duplicates()
Enum.map(&to_priority(&1)) |> |> Enum.map(&to_priority(&1))
Enum.reduce(&(&1 + &2)) |> |> Enum.reduce(&(&1 + &2))
increment_sum.() |> increment_sum.()
else else
state state
end end
@ -36,4 +44,3 @@ defmodule Day3 do
state.sum state.sum
end end
end end

View file

@ -1,10 +1,11 @@
defmodule Day9 do defmodule Day9 do
def init_state(p) when p == :part1 do def init_state(p) when p == :part1 do
{ [ {0,0}, {0,0} ], MapSet.new() } {[{0, 0}, {0, 0}], MapSet.new()}
end end
def init_state(p) when p == :part2 do def init_state(p) when p == :part2 do
{ [ {0,0}, {0,0}, {0,0}, {0,0}, {0,0}, {0,0}, {0,0}, {0,0}, {0,0}, {0,0}], MapSet.new() } {[{0, 0}, {0, 0}, {0, 0}, {0, 0}, {0, 0}, {0, 0}, {0, 0}, {0, 0}, {0, 0}, {0, 0}],
MapSet.new()}
end end
def execute(line, state) when line == "" do def execute(line, state) when line == "" do
@ -12,29 +13,32 @@ defmodule Day9 do
end end
def execute(line, state) do def execute(line, state) do
[ movement, count] = line |> String.split(" "); [movement, count] = line |> String.split(" ")
{ count, _ } = Integer.parse(count); {count, _} = Integer.parse(count)
Enum.reduce( Enum.reduce(
1..count, 1..count,
state, state,
fn (_, state) -> fn _, state ->
{ rope, tail_positions } = state {rope, tail_positions} = state
rope = movement
rope =
movement
|> string_to_movement_direction() |> string_to_movement_direction()
|> move_rope_by_head(rope) |> move_rope_by_head(rope)
{ rope, MapSet.put(tail_positions, List.last(rope)) }
{rope, MapSet.put(tail_positions, List.last(rope))}
end end
); )
end end
def get_answer(state) do def get_answer(state) do
{_, tail_positions } = state {_, tail_positions} = state
MapSet.size(tail_positions) MapSet.size(tail_positions)
end end
defp move_rope_by_head(direction, rope) do defp move_rope_by_head(direction, rope) do
[head | tail ] = rope [head | tail] = rope
reconcile_tail([add_2_tuple(head, direction)], tail) reconcile_tail([add_2_tuple(head, direction)], tail)
end end
@ -44,7 +48,7 @@ defmodule Day9 do
defp reconcile_tail(head, tail) do defp reconcile_tail(head, tail) do
leader = head |> List.last() leader = head |> List.last()
[ follower | tail ] = tail [follower | tail] = tail
reconcile_tail(head ++ [keep_min_distance(follower, leader, 1)], tail) reconcile_tail(head ++ [keep_min_distance(follower, leader, 1)], tail)
end end
@ -83,16 +87,15 @@ defmodule Day9 do
end end
defp add_2_tuple(x, y) do defp add_2_tuple(x, y) do
{ elem(x,0) + elem(y,0), elem(x,1) + elem(y,1) } {elem(x, 0) + elem(y, 0), elem(x, 1) + elem(y, 1)}
end end
defp distance_vector(x, y) do defp distance_vector(x, y) do
{ elem(y,0) - elem(x,0), elem(y,1) - elem(x,1) } {elem(y, 0) - elem(x, 0), elem(y, 1) - elem(x, 1)}
end end
defp max_coord_difference(x, y) do defp max_coord_difference(x, y) do
{a, b} = { abs(elem(x,0)-elem(y,0)), abs(elem(x,1)-elem(y,1)) } {a, b} = {abs(elem(x, 0) - elem(y, 0)), abs(elem(x, 1) - elem(y, 1))}
max(a, b) max(a, b)
end end
end end