-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsolution_day_3_2024.erl
More file actions
55 lines (50 loc) · 1.19 KB
/
solution_day_3_2024.erl
File metadata and controls
55 lines (50 loc) · 1.19 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
-module(solution_day_3_2024).
-export([
lex/1,
parse/1,
part_one/1,
part_two/1
]).
lex(Input) ->
{ok, Tokens, _} = lexer_day_3_2024:string(Input),
{ok, Tokens}.
parse(Input) ->
parser_day_3_2024:parse(Input).
-spec part_one(list(integer())) -> integer().
part_one(Input) ->
lists:foldl(
fun(Item, Acc) ->
case Item of
{operands, {X, Y}} ->
Acc + X * Y;
enable ->
Acc;
disable ->
Acc
end
end,
0,
Input
).
-spec part_two(list(integer())) -> integer().
part_two(Input) ->
{_Mode, Result} = lists:foldl(
fun(Item, {Mode, Sum}) ->
case Item of
{operands, {X, Y}} ->
case Mode of
enable ->
{Mode, Sum + X * Y};
disable ->
{Mode, Sum}
end;
enable ->
{Item, Sum};
disable ->
{Item, Sum}
end
end,
{enable, 0},
Input
),
Result.