Skip to content

Commit 164417e

Browse files
committed
Minor refactors
1 parent f7cea45 commit 164417e

12 files changed

Lines changed: 860 additions & 35 deletions

File tree

.credo.exs

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
# This file contains the configuration for Credo.
2+
#
3+
# For more information, see: https://hexdocs.pm/credo/config_file.html
4+
%{
5+
configs: [
6+
%{
7+
name: "default",
8+
files: %{
9+
included: ["lib/", "src/", "web/", "apps/"],
10+
excluded: [~r"/_build/", ~r"/deps/", ~r"/node_modules/"]
11+
},
12+
plugins: [],
13+
requires: [],
14+
strict: false,
15+
parse_timeout: 5000,
16+
color: true,
17+
checks: [
18+
# Disable cyclomatic complexity check - graph algorithms are naturally complex
19+
{Credo.Check.Refactor.CyclomaticComplexity, false},
20+
21+
# Other checks we want to keep
22+
{Credo.Check.Consistency.ExceptionNames, []},
23+
{Credo.Check.Consistency.LineEndings, []},
24+
{Credo.Check.Consistency.ParameterPatternMatching, []},
25+
{Credo.Check.Consistency.SpaceAroundOperators, []},
26+
{Credo.Check.Consistency.SpaceInParentheses, []},
27+
{Credo.Check.Consistency.TabsOrSpaces, []},
28+
{Credo.Check.Design.AliasUsage, [priority: :low, if_nested_deeper_than: 2, if_called_more_often_than: 0]},
29+
{Credo.Check.Design.DuplicatedCode, [nodes_threshold: 3]},
30+
{Credo.Check.Design.SkipTestWithoutComment, []},
31+
{Credo.Check.Readability.AliasOrder, []},
32+
{Credo.Check.Readability.FunctionNames, []},
33+
{Credo.Check.Readability.LargeNumbers, []},
34+
{Credo.Check.Readability.MaxLineLength, [priority: :low, max_length: 120]},
35+
{Credo.Check.Readability.ModuleAttributeNames, []},
36+
{Credo.Check.Readability.ModuleDoc, []},
37+
{Credo.Check.Readability.ModuleNames, []},
38+
{Credo.Check.Readability.ParenthesesInCondition, []},
39+
{Credo.Check.Readability.ParenthesesOnZeroArityDefs, []},
40+
{Credo.Check.Readability.PredicateFunctionNames, []},
41+
{Credo.Check.Readability.PreferImplicitTry, []},
42+
{Credo.Check.Readability.RedundantBlankLines, []},
43+
{Credo.Check.Readability.Semicolons, []},
44+
{Credo.Check.Readability.SpaceAfterCommas, []},
45+
{Credo.Check.Readability.StringSigils, []},
46+
{Credo.Check.Readability.TrailingBlankLine, []},
47+
{Credo.Check.Readability.TrailingWhiteSpace, []},
48+
{Credo.Check.Readability.UnnecessaryAliasExpansion, []},
49+
{Credo.Check.Readability.VariableNames, []},
50+
{Credo.Check.Readability.WithSingleClause, []},
51+
{Credo.Check.Refactor.ABCSize, false},
52+
{Credo.Check.Refactor.AppendSingleItem, []},
53+
{Credo.Check.Refactor.DoubleBooleanNegation, []},
54+
{Credo.Check.Refactor.FilterReject, []},
55+
{Credo.Check.Refactor.IoPuts, []},
56+
{Credo.Check.Refactor.MapJoin, []},
57+
{Credo.Check.Refactor.NegatedConditionsInUnless, []},
58+
{Credo.Check.Refactor.NegatedConditionsWithElse, []},
59+
{Credo.Check.Refactor.Nesting, [max_nesting: 10]},
60+
{Credo.Check.Refactor.RedundantWithClauseResult, []},
61+
{Credo.Check.Refactor.RejectFilter, []},
62+
{Credo.Check.Refactor.UnlessWithElse, []},
63+
{Credo.Check.Refactor.VariableRebinding, []},
64+
{Credo.Check.Refactor.WithClauses, []},
65+
{Credo.Check.Warning.ApplicationConfigInModuleAttribute, []},
66+
{Credo.Check.Warning.BoolOperationOnSameValues, []},
67+
{Credo.Check.Warning.ExpensiveEmptyEnumCheck, []},
68+
{Credo.Check.Warning.IExPry, []},
69+
{Credo.Check.Warning.IoInspect, []},
70+
{Credo.Check.Warning.MixEnv, []},
71+
{Credo.Check.Warning.OperationOnSameValues, []},
72+
{Credo.Check.Warning.OperationWithConstantResult, []},
73+
{Credo.Check.Warning.RaiseInsideRescue, []},
74+
{Credo.Check.Warning.SpecWithStruct, []},
75+
{Credo.Check.Warning.UnsafeExec, []},
76+
{Credo.Check.Warning.UnusedEnumOperation, []},
77+
{Credo.Check.Warning.UnusedFileOperation, []},
78+
{Credo.Check.Warning.UnusedKeywordOperation, []},
79+
{Credo.Check.Warning.UnusedListOperation, []},
80+
{Credo.Check.Warning.UnusedPathOperation, []},
81+
{Credo.Check.Warning.UnusedRegexOperation, []},
82+
{Credo.Check.Warning.UnusedStringOperation, []},
83+
{Credo.Check.Warning.UnusedTupleOperation, []},
84+
{Credo.Check.Warning.WrongTestFileExtension, []}
85+
]
86+
}
87+
]
88+
}

lib/2016/day_08.ex

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -75,12 +75,8 @@ defmodule AdventOfCode.Y2016.Day08 do
7575
for w <- 0..(@width - 1) do
7676
IO.write(grid[{w, h}])
7777
end
78-
79-
IO.puts("")
8078
end
8179

82-
IO.puts("")
83-
8480
:ok
8581
end
8682

lib/2016/day_13.ex

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ defmodule AdventOfCode.Y2016.Day13 do
6060
|> MapSet.size()
6161
end
6262

63-
defp is_wall?(x, y, fav) do
63+
defp wall?(x, y, fav) do
6464
if x < 0 or y < 0 do
6565
true
6666
else
@@ -71,7 +71,7 @@ defmodule AdventOfCode.Y2016.Day13 do
7171

7272
defp open_neighbors({x, y}, fav) do
7373
[{x + 1, y}, {x - 1, y}, {x, y + 1}, {x, y - 1}]
74-
|> Enum.filter(fn {nx, ny} -> not is_wall?(nx, ny, fav) end)
74+
|> Enum.filter(fn {nx, ny} -> not wall?(nx, ny, fav) end)
7575
end
7676

7777
defp count_ones(0), do: 0

lib/2019/day_08.ex

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,6 @@ defmodule AdventOfCode.Y2019.Day08 do
5959
|> chunkify()
6060
|> List.first()
6161
|> Enum.map_join("\n", &Enum.join/1)
62-
|> tap(&IO.puts/1)
6362
end
6463

6564
defp chunkify(data) do

lib/2019/day_18.ex

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -109,21 +109,21 @@ defmodule AdventOfCode.Y2019.Day18 do
109109

110110
defp find_pois(grid, start_chars) do
111111
grid
112-
|> Enum.filter(fn {_, char} -> char in start_chars or is_key?(char) end)
112+
|> Enum.filter(fn {_, char} -> char in start_chars or key?(char) end)
113113
|> Map.new(fn {pos, char} -> {char, pos} end)
114114
end
115115

116116
defp find_pois_part_2(grid, start_chars) do
117117
grid
118118
|> Enum.filter(fn {_, char} ->
119-
char in start_chars or is_key?(char)
119+
char in start_chars or key?(char)
120120
end)
121121
|> Map.new(fn {pos, char} -> {char, pos} end)
122122
end
123123

124124
defp calculate_keys_mask(pois) do
125125
Enum.reduce(pois, 0, fn {label, _}, acc ->
126-
if is_key?(label), do: bor(acc, key_bit(label)), else: acc
126+
if key?(label), do: bor(acc, key_bit(label)), else: acc
127127
end)
128128
end
129129

@@ -150,7 +150,7 @@ defmodule AdventOfCode.Y2019.Day18 do
150150

151151
# Record this key if we found one (but don't stop exploring!)
152152
new_acc =
153-
if dist > 0 and is_key?(char) do
153+
if dist > 0 and key?(char) do
154154
[%{to: char, dist: dist, required: mask} | acc]
155155
else
156156
acc
@@ -159,8 +159,8 @@ defmodule AdventOfCode.Y2019.Day18 do
159159
# Continue exploring - update mask with doors AND keys we pass through
160160
new_mask =
161161
cond do
162-
is_door?(char) -> bor(mask, door_bit(char))
163-
is_key?(char) -> bor(mask, key_bit(char))
162+
door?(char) -> bor(mask, door_bit(char))
163+
key?(char) -> bor(mask, key_bit(char))
164164
true -> mask
165165
end
166166

@@ -181,8 +181,8 @@ defmodule AdventOfCode.Y2019.Day18 do
181181

182182
defp neighbors({x, y}), do: [{x + 1, y}, {x - 1, y}, {x, y + 1}, {x, y - 1}]
183183

184-
defp is_key?(char), do: char != nil and char >= "a" and char <= "z"
185-
defp is_door?(char), do: char != nil and char >= "A" and char <= "Z"
184+
defp key?(char), do: char != nil and char >= "a" and char <= "z"
185+
defp door?(char), do: char != nil and char >= "A" and char <= "Z"
186186

187187
defp key_bit(char), do: bsl(1, hd(String.to_charlist(char)) - ?a)
188188
defp door_bit(char), do: bsl(1, hd(String.to_charlist(char)) - ?A)

lib/2022/day_12.ex

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ defmodule AdventOfCode.Y2022.Day12 do
66
Tags: graph graph-traversal
77
"""
88
alias AdventOfCode.Helpers.{InputReader, Transformers}
9-
alias Yog.Builder.Grid
9+
alias Yog.Builder.{Grid, GridGraph}
1010
alias Yog.Pathfinding.Dijkstra
1111
alias Yog.Transform
1212

@@ -22,8 +22,8 @@ defmodule AdventOfCode.Y2022.Day12 do
2222

2323
{sr, sc} = start_pos
2424
{er, ec} = end_pos
25-
start_id = Yog.Builder.GridGraph.coord_to_id(builder, sr, sc)
26-
end_id = Yog.Builder.GridGraph.coord_to_id(builder, er, ec)
25+
start_id = GridGraph.coord_to_id(builder, sr, sc)
26+
end_id = GridGraph.coord_to_id(builder, er, ec)
2727

2828
# Part 1 and Part 2 are computed in parallel
2929
task_1 =

lib/2023/day_10.ex

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ defmodule AdventOfCode.Y2023.Day10 do
66
Tags: graph bfs grid
77
"""
88
alias AdventOfCode.Helpers.{InputReader, Transformers}
9-
alias Yog.Builder.Grid
9+
alias Yog.Builder.{Grid, GridGraph}
10+
alias Yog.Model
1011
alias Yog.Pathfinding.Dijkstra
1112

1213
def input, do: InputReader.read_from_file(2023, 10)
@@ -96,11 +97,11 @@ defmodule AdventOfCode.Y2023.Day10 do
9697
|> Enum.filter(&MapSet.member?(loop_nodes, &1))
9798

9899
# Get coordinates of S and its neighbors to determine the pipe type
99-
{sr, sc} = Yog.Builder.GridGraph.id_to_coord(grid_graph, start_id)
100+
{sr, sc} = GridGraph.id_to_coord(grid_graph, start_id)
100101

101102
s_offsets =
102103
Enum.map(s_neighbors, fn nid ->
103-
{nr, nc} = Yog.Builder.GridGraph.id_to_coord(grid_graph, nid)
104+
{nr, nc} = GridGraph.id_to_coord(grid_graph, nid)
104105
{nr - sr, nc - sc}
105106
end)
106107
|> MapSet.new()
@@ -123,11 +124,11 @@ defmodule AdventOfCode.Y2023.Day10 do
123124
{row_enclosed, _inside} =
124125
for c <- 0..(cols - 1), reduce: {0, false} do
125126
{count, inside} ->
126-
id = Yog.Builder.GridGraph.coord_to_id(grid_graph, r, c)
127+
id = GridGraph.coord_to_id(grid_graph, r, c)
127128

128129
if MapSet.member?(loop_nodes, id) do
129130
# Part of the loop - check if it's a boundary crossing
130-
cell = Yog.Model.node(graph, id)
131+
cell = Model.node(graph, id)
131132
char = if cell.char == "S", do: s_char, else: cell.char
132133

133134
# Standard scan-line crossing logic: | L J flip whereas F 7 - do not
@@ -156,7 +157,7 @@ defmodule AdventOfCode.Y2023.Day10 do
156157
to_remove =
157158
Enum.filter(node_set, fn id ->
158159
degree =
159-
Yog.Model.successors(graph, id)
160+
Model.successors(graph, id)
160161
|> Enum.count(fn {neighbor, _} -> MapSet.member?(node_set, neighbor) end)
161162

162163
degree < 2

lib/helpers/generator.ex

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,15 +42,15 @@ defmodule AdventOfCode.Helpers.Generator do
4242
end
4343

4444
defp create_file(path, content) do
45-
if !File.exists?(path) do
45+
if File.exists?(path) do
46+
{:exists, path}
47+
else
4648
path |> Path.dirname() |> File.mkdir_p!()
4749

4850
case File.write(path, content) do
4951
:ok -> {:ok, path}
5052
{:error, reason} -> {:error, path, reason}
5153
end
52-
else
53-
{:exists, path}
5454
end
5555
end
5656

@@ -76,7 +76,9 @@ defmodule AdventOfCode.Helpers.Generator do
7676
end
7777

7878
defp create_input_file(path, year, day) do
79-
if !File.exists?(path) do
79+
if File.exists?(path) do
80+
{:exists, path}
81+
else
8082
path |> Path.dirname() |> File.mkdir_p!()
8183

8284
case fetch_cookie(year, day) do
@@ -89,8 +91,6 @@ defmodule AdventOfCode.Helpers.Generator do
8991
{:error, reason} ->
9092
{:error, path, reason}
9193
end
92-
else
93-
{:exists, path}
9494
end
9595
end
9696

lib/helpers/helpers.ex

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,6 @@ defmodule AdventOfCode.Helpers do
3232
for j <- 0..y do
3333
IO.write(grid[{i, j}])
3434
end
35-
36-
IO.puts("")
3735
end
3836
end
3937
end

0 commit comments

Comments
 (0)