-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsolution_day_4_2024.erl
More file actions
117 lines (101 loc) · 2.95 KB
/
Copy pathsolution_day_4_2024.erl
File metadata and controls
117 lines (101 loc) · 2.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
-module(solution_day_4_2024).
% NOTE: this makes the LSP happy, but shouldn't be necessary
-feature(maybe_expr, enable).
-export([
lex/1,
parse/1,
part_one/1,
part_two/1
]).
lex(Input) ->
{ok, Tokens, _} = lexer_day_4_2024:string(Input),
{ok, Tokens}.
parse(Input) ->
parser_day_4_2024:parse(Input).
%% TODO: would be nice to describe these as a list of kernels
%% Something like matrix:gets([{0,1}, {0,2}, {0,3}], Key, Xmas)
get_right({Row, Col}, Xmas) ->
X = maps:get({Row, Col + 1}, Xmas, none),
Y = maps:get({Row, Col + 2}, Xmas, none),
Z = maps:get({Row, Col + 3}, Xmas, none),
[X, Y, Z].
get_down({Row, Col}, Xmas) ->
X = maps:get({Row + 1, Col}, Xmas, none),
Y = maps:get({Row + 2, Col}, Xmas, none),
Z = maps:get({Row + 3, Col}, Xmas, none),
[X, Y, Z].
get_positive_diagonal({Row, Col}, Xmas) ->
X = maps:get({Row + 1, Col + 1}, Xmas, none),
Y = maps:get({Row + 2, Col + 2}, Xmas, none),
Z = maps:get({Row + 3, Col + 3}, Xmas, none),
[X, Y, Z].
get_negative_diagonal({Row, Col}, Xmas) ->
X = maps:get({Row + 1, Col - 1}, Xmas, none),
Y = maps:get({Row + 2, Col - 2}, Xmas, none),
Z = maps:get({Row + 3, Col - 3}, Xmas, none),
[X, Y, Z].
get_x({Row, Col}, Xmas) ->
A = maps:get({Row - 1, Col - 1}, Xmas, none),
B = maps:get({Row + 1, Col + 1}, Xmas, none),
C = maps:get({Row - 1, Col + 1}, Xmas, none),
D = maps:get({Row + 1, Col - 1}, Xmas, none),
{{A, B}, {C, D}}.
search(Key, Xmas, Compare) ->
Right = get_right(Key, Xmas),
Down = get_down(Key, Xmas),
PosDiagonal = get_positive_diagonal(Key, Xmas),
NegDiagonal = get_negative_diagonal(Key, Xmas),
lists:foldl(
fun(X, Acc) ->
case X =:= Compare of
true -> Acc + 1;
false -> Acc
end
end,
0,
[Right, Down, PosDiagonal, NegDiagonal]
).
search_xmas(Key, Xmas) ->
search(Key, Xmas, [m, a, s]).
search_samx(Key, Xmas) ->
search(Key, Xmas, [a, m, x]).
check(X) ->
(X =:= {m, s}) or (X =:= {s, m}).
search_cross(Key, Xmas) ->
{X, Y} = get_x(Key, Xmas),
case check(X) and check(Y) of
true -> 1;
false -> 0
end.
-spec part_one(list(list(string()))) -> integer().
part_one(Input) ->
Xmas = maps:from_list(Input),
maps:fold(
fun(Key, Value, Acc) ->
case Value of
x ->
Acc + search_xmas(Key, Xmas);
s ->
Acc + search_samx(Key, Xmas);
_ ->
Acc
end
end,
0,
Xmas
).
-spec part_two(list(list(string()))) -> integer().
part_two(Input) ->
Xmas = maps:from_list(Input),
maps:fold(
fun(Key, Value, Acc) ->
case Value of
a ->
Acc + search_cross(Key, Xmas);
_ ->
Acc
end
end,
0,
Xmas
).