@@ -13,57 +13,41 @@ defmodule Day13 do
13
13
end
14
14
15
15
def partA ( file_path ) do
16
- file_path
17
- |> read_input ( )
18
- |> Enum . map ( & find_mirror ( & 1 , 0 ) )
19
- |> Enum . map ( & mirror_point / 1 )
20
- |> Enum . sum ( )
16
+ solve ( file_path , 0 )
21
17
end
22
18
23
19
def partB ( file_path ) do
20
+ solve ( file_path , 1 )
21
+ end
22
+
23
+ def solve ( file_path , target ) do
24
24
file_path
25
25
|> read_input ( )
26
- |> Enum . map ( & find_mirror ( & 1 , 1 ) )
26
+ |> Enum . map ( & find_mirror ( & 1 , target ) )
27
27
|> Enum . map ( & mirror_point / 1 )
28
28
|> Enum . sum ( )
29
29
end
30
30
31
- def pairs ( mirror_loc , offset ) , do: { mirror_loc - 1 - offset , mirror_loc + offset }
32
- def get_row ( arr , row_id ) , do: arr |> Enum . at ( row_id )
33
- def get_col ( arr , col_id ) , do: arr |> Enum . map ( & Enum . at ( & 1 , col_id ) ) |> List . flatten ( )
34
-
35
- def number_of_mismatches_at_offset ( pattern , mirror_loc , offset , get_fun ) do
36
- { p1 , p2 } = pairs ( mirror_loc , offset )
37
- slice1 = get_fun . ( pattern , p1 )
38
- slice2 = get_fun . ( pattern , p2 )
31
+ def number_of_mismatches_at_offset ( pattern , mirror_loc , offset , type ) do
32
+ { p1 , p2 } = pairs_at_offset ( mirror_loc , offset )
33
+ slice1 = get_f ( type ) . ( pattern , p1 )
34
+ slice2 = get_f ( type ) . ( pattern , p2 )
39
35
40
36
Enum . zip ( slice1 , slice2 )
41
37
|> Enum . filter ( fn { a , b } -> a != b end )
42
38
|> length ( )
43
39
end
44
40
45
41
def valid_mirror? ( pattern , mirror_loc , type , target ) do
46
- { get_fun , lim_fun } =
47
- case type do
48
- :row -> { & get_row / 2 , & get_col / 2 }
49
- :col -> { & get_col / 2 , & get_row / 2 }
50
- end
51
-
52
- 0 .. ( min ( mirror_loc , length ( lim_fun . ( pattern , 0 ) ) - mirror_loc ) - 1 )
53
- |> Enum . map ( & number_of_mismatches_at_offset ( pattern , mirror_loc , & 1 , get_fun ) )
42
+ 0 .. ( min ( mirror_loc , length ( lim_f ( type ) . ( pattern , 0 ) ) - mirror_loc ) - 1 )
43
+ |> Enum . map ( & number_of_mismatches_at_offset ( pattern , mirror_loc , & 1 , type ) )
54
44
|> Enum . sum ( )
55
45
|> Kernel . == ( target )
56
46
end
57
47
58
48
def find_mirrorH ( pattern , type , target ) do
59
- lim_fun =
60
- case type do
61
- :row -> & get_col / 2
62
- :col -> & get_row / 2
63
- end
64
-
65
49
Enum . reduce (
66
- 1 .. ( length ( lim_fun . ( pattern , 0 ) ) - 1 ) ,
50
+ 1 .. ( length ( lim_f ( type ) . ( pattern , 0 ) ) - 1 ) ,
67
51
nil ,
68
52
fn mirror_loc , acc ->
69
53
case acc do
@@ -83,6 +67,13 @@ defmodule Day13 do
83
67
84
68
def mirror_point ( { :row , x } ) , do: 100 * x
85
69
def mirror_point ( { :col , x } ) , do: x
70
+ def get_f ( :row ) , do: & get_row / 2
71
+ def get_f ( :col ) , do: & get_col / 2
72
+ def lim_f ( :row ) , do: & get_col / 2
73
+ def lim_f ( :col ) , do: & get_row / 2
74
+ def pairs_at_offset ( mirror_loc , offset ) , do: { mirror_loc - 1 - offset , mirror_loc + offset }
75
+ def get_row ( arr , row_id ) , do: arr |> Enum . at ( row_id )
76
+ def get_col ( arr , col_id ) , do: arr |> Enum . map ( & Enum . at ( & 1 , col_id ) ) |> List . flatten ( )
86
77
end
87
78
88
79
IO . puts ( Day13 . partA ( "./input" ) )
0 commit comments