diff --git a/lib/day1.ex b/lib/day1.ex index b87989a..1d8b719 100644 --- a/lib/day1.ex +++ b/lib/day1.ex @@ -1,11 +1,14 @@ defmodule Day1 do def get_answer(state) do {_, total, part} = state - total |> - then(fn total -> case part do + + total + |> then(fn total -> + case part do :part1 -> total :part2 -> total |> Enum.reduce(0, fn i, acc -> i + acc end) - end end) + end + end) end def init_state(part) do @@ -15,10 +18,11 @@ defmodule Day1 do end end - def execute(line, state) do + def execute(line, state) do apply_command( line |> to_command(), - state) + state + ) end defp apply_command(command, state) do @@ -31,26 +35,34 @@ defmodule Day1 do defp to_command(s) do try do {:add, s |> String.trim() |> String.to_integer()} - rescue + rescue ArgumentError -> {:reset} end end defp apply_add(n, state) do {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} end defp take_top_three(n, list) do - [ n | list ] |> - Enum.sort(:desc) |> - Enum.take(3) + [n | list] + |> Enum.sort(:desc) + |> Enum.take(3) end defp apply_reset(state) do {current, total, part} = state + if !is_nil(current) do case part do :part1 -> {nil, max(total, current), part} @@ -61,4 +73,3 @@ defmodule Day1 do end end end - diff --git a/lib/day10.ex b/lib/day10.ex index 8ff711b..e95fcab 100644 --- a/lib/day10.ex +++ b/lib/day10.ex @@ -4,11 +4,11 @@ defmodule Day10 do @behaviour Solution def init_state(p) when p == :part1 do - %{ vm: VirtualMachine.new(), acc: 0, p: p } + %{vm: VirtualMachine.new(), acc: 0, p: p} end def init_state(p) when p == :part2 do - %{ vm: VirtualMachine.new(), crt: "", p: p} + %{vm: VirtualMachine.new(), crt: "", p: p} end def execute(line, state) when line == "" do @@ -16,42 +16,48 @@ defmodule Day10 do end def execute(line, state) when state.p == :part1 do - VirtualMachine.plan_execute(line) |> - Enum.reduce( + VirtualMachine.plan_execute(line) + |> Enum.reduce( state, - fn (tock, %{vm: vm, acc: acc}) -> + fn tock, %{vm: vm, acc: acc} -> 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: vm, acc: acc} + %{vm: vm, acc: acc} end - ) |> Map.put(:p, state.p) + ) + |> Map.put(:p, state.p) end def execute(line, state) when state.p == :part2 do - VirtualMachine.plan_execute(line) |> - Enum.reduce( + VirtualMachine.plan_execute(line) + |> Enum.reduce( state, - fn (tock, %{vm: vm, crt: crt}) -> + fn tock, %{vm: vm, crt: crt} -> next_tick = vm.tick + 1 - char = case mod(vm.tick, 40) do - x when x >= (vm.x - 1) and x <= (vm.x + 1) -> "#" - _ -> "." - end + char = + case mod(vm.tick, 40) do + x when x >= vm.x - 1 and x <= vm.x + 1 -> "#" + _ -> "." + end crt = crt <> char - crt = crt <> if mod(next_tick, 40) == 0 do - "\n" - else "" - end + crt = + crt <> + if mod(next_tick, 40) == 0 do + "\n" + else + "" + end vm = tock.(vm) - %{ vm: vm, crt: crt} + %{vm: vm, crt: crt} end - ) |> Map.put(:p, state.p) + ) + |> Map.put(:p, state.p) end def get_answer(state) when state.p == :part1 do @@ -65,39 +71,42 @@ end defmodule VirtualMachine do def new() do - %{ x: 1, tick: 0 } + %{x: 1, tick: 0} end def plan_execute(instruction) do - [instruction, args] = if instruction == "noop" do - [instruction, nil] - else - String.split(instruction, " ") - end + [instruction, args] = + if instruction == "noop" do + [instruction, nil] + else + String.split(instruction, " ") + end plan_execute(instruction, args) end def plan_execute(instruction, args) when instruction == "addx" do - args = case Integer.parse(args) do - {args, _} -> args - :error -> 0 - end + args = + case Integer.parse(args) do + {args, _} -> args + :error -> 0 + end + if is_integer(args) do - plan_execute("noop", nil) ++ - [ - fn (vm) -> - vm |> - Map.update(:tick, 2, &(&1 + 1)) |> - Map.update(:x, args, &(&1 + args)) - end - ] - else [] + plan_execute("noop", nil) ++ + [ + fn vm -> + vm + |> Map.update(:tick, 2, &(&1 + 1)) + |> Map.update(:x, args, &(&1 + args)) + end + ] + else + [] end end 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 - diff --git a/lib/day2.ex b/lib/day2.ex index bb49139..e340913 100644 --- a/lib/day2.ex +++ b/lib/day2.ex @@ -8,16 +8,17 @@ defmodule Day2 do end def execute(line, state) do - if line |> String.trim() |> String.length == 0 do + if line |> String.trim() |> String.length() == 0 do state else {_, player, outcome} = parse_moves(line, state.part) - + init_state( - state.score + - points_for_move(player) + - points_from_outcome(outcome), - state.part) + state.score + + points_for_move(player) + + points_from_outcome(outcome), + state.part + ) end end @@ -48,35 +49,42 @@ defmodule Day2 do opposing_move else case opposing_move do - :rock -> case outcome do - :win -> :paper - :loss -> :scissors - end - :paper -> case outcome do - :win -> :scissors - :loss -> :rock - end - :scissors -> case outcome do - :win -> :rock - :loss -> :paper - end + :rock -> + case outcome do + :win -> :paper + :loss -> :scissors + end + + :paper -> + case outcome do + :win -> :scissors + :loss -> :rock + end + + :scissors -> + case outcome do + :win -> :rock + :loss -> :paper + end end end end - defp parse_move(move, side) do case side do - :left -> case move do - "A" -> :rock - "B" -> :paper - "C" -> :scissors - end - :right -> case move do - "X" -> :rock - "Y" -> :paper - "Z" -> :scissors - end + :left -> + case move do + "A" -> :rock + "B" -> :paper + "C" -> :scissors + end + + :right -> + case move do + "X" -> :rock + "Y" -> :paper + "Z" -> :scissors + end end end @@ -93,24 +101,30 @@ defmodule Day2 do 0 else case a do - :rock -> case b do - :paper -> -1 - :scissors -> 1 - end - :paper -> case b do - :rock -> 1 - :scissors -> -1 - end - :scissors -> case b do - :rock -> -1 - :paper -> 1 - end + :rock -> + case b do + :paper -> -1 + :scissors -> 1 + end + + :paper -> + case b do + :rock -> 1 + :scissors -> -1 + end + + :scissors -> + case b do + :rock -> -1 + :paper -> 1 + end end end end defp outcome?(match) when is_tuple(match) and tuple_size(match) == 2 do {opponent, player} = match + case sort_(player, opponent) do 1 -> :win 0 -> :draw diff --git a/lib/day3.ex b/lib/day3.ex index 315c3ae..d67df05 100644 --- a/lib/day3.ex +++ b/lib/day3.ex @@ -4,29 +4,37 @@ defmodule Day3 do end defp find_duplicates(line) do - line |> - String.to_charlist() |> - Enum.chunk_every(Integer.floor_div(String.length(line), 2)) |> - Enum.map(&MapSet.new/1) |> - then(fn [a,b] -> MapSet.intersection(a,b) end) |> - MapSet.to_list() + line + |> String.to_charlist() + |> Enum.chunk_every(Integer.floor_div(String.length(line), 2)) + |> Enum.map(&MapSet.new/1) + |> then(fn [a, b] -> MapSet.intersection(a, b) end) + |> MapSet.to_list() end 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 def execute(line, state) do - increment_sum = fn s -> Map.update(state, :sum, 0, &(&1 + s)) end + increment_sum = fn s -> Map.update(state, :sum, 0, &(&1 + s)) end trimmed = line |> String.trim() unless String.length(trimmed) == 0 do - trimmed |> - find_duplicates() |> - Enum.map(&to_priority(&1)) |> - Enum.reduce(&(&1 + &2)) |> - increment_sum.() + trimmed + |> find_duplicates() + |> Enum.map(&to_priority(&1)) + |> Enum.reduce(&(&1 + &2)) + |> increment_sum.() else state end @@ -36,4 +44,3 @@ defmodule Day3 do state.sum end end - diff --git a/lib/day9.ex b/lib/day9.ex index b5d9fed..194ad89 100644 --- a/lib/day9.ex +++ b/lib/day9.ex @@ -1,10 +1,11 @@ defmodule Day9 do def init_state(p) when p == :part1 do - { [ {0,0}, {0,0} ], MapSet.new() } + {[{0, 0}, {0, 0}], MapSet.new()} end 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 def execute(line, state) when line == "" do @@ -12,29 +13,32 @@ defmodule Day9 do end def execute(line, state) do - [ movement, count] = line |> String.split(" "); - { count, _ } = Integer.parse(count); + [movement, count] = line |> String.split(" ") + {count, _} = Integer.parse(count) Enum.reduce( 1..count, state, - fn (_, state) -> - { rope, tail_positions } = state - rope = movement + fn _, state -> + {rope, tail_positions} = state + + rope = + movement |> string_to_movement_direction() |> move_rope_by_head(rope) - { rope, MapSet.put(tail_positions, List.last(rope)) } + + {rope, MapSet.put(tail_positions, List.last(rope))} end - ); + ) end def get_answer(state) do - {_, tail_positions } = state + {_, tail_positions} = state MapSet.size(tail_positions) end defp move_rope_by_head(direction, rope) do - [head | tail ] = rope + [head | tail] = rope reconcile_tail([add_2_tuple(head, direction)], tail) end @@ -44,7 +48,7 @@ defmodule Day9 do defp reconcile_tail(head, tail) do leader = head |> List.last() - [ follower | tail ] = tail + [follower | tail] = tail reconcile_tail(head ++ [keep_min_distance(follower, leader, 1)], tail) end @@ -52,11 +56,11 @@ defmodule Day9 do defp keep_min_distance(tail, head, min_distance) do difference = distance_vector(tail, head) - if max_coord_difference(tail, head) <= min_distance do - tail + if max_coord_difference(tail, head) <= min_distance do + tail else {a, b} = difference - + add_2_tuple(tail, {megan(a, 0, -1, 1), megan(b, 0, -1, 1)}) end end @@ -72,7 +76,7 @@ defmodule Day9 do defp megan(0, zero, _, _) do zero end - + defp string_to_movement_direction(direction) do case direction do "R" -> {1, 0} @@ -83,16 +87,15 @@ defmodule Day9 do end 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 - defp distance_vector(x, y) do - { elem(y,0) - elem(x,0), elem(y,1) - elem(x,1) } + defp distance_vector(x, y) do + {elem(y, 0) - elem(x, 0), elem(y, 1) - elem(x, 1)} end - defp max_coord_difference(x, y) do - {a, b} = { abs(elem(x,0)-elem(y,0)), abs(elem(x,1)-elem(y,1)) } + defp max_coord_difference(x, y) do + {a, b} = {abs(elem(x, 0) - elem(y, 0)), abs(elem(x, 1) - elem(y, 1))} max(a, b) end end -