-
Notifications
You must be signed in to change notification settings - Fork 0
/
sol14a.erl
executable file
·45 lines (37 loc) · 1.17 KB
/
sol14a.erl
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
#!/usr/bin/escript
main([]) ->
main(["sol14.data"]);
main([Filename]) ->
{ok, File} = file:read_file(Filename),
Lines = string:lexemes(File, "\r\n"),
Instr = [parse_line(L) || L <- Lines],
Values = lists:foldl(
fun (X, Acc) -> process_instruction(X, Acc) end,
{16#fffffffff, 16#000000000, #{}},
Instr),
{_, _, Mem} = Values,
Total = lists:foldl(fun erlang:'+'/2, 0, maps:values(Mem)),
io:format("~B~n", [Total]).
parse_line(<<"mask = ", Text/binary>>) ->
{mask,
build_binary(Text, fun (X) -> map_mask(X) end),
build_binary(Text, fun (X) -> map_bitset(X) end)
};
parse_line(<<"mem", _/binary>> = Mem) ->
[AddrT, ValueT] = string:lexemes(Mem, "me[] ="),
Addr = binary_to_integer(AddrT),
Value = binary_to_integer(ValueT),
{mem, Addr, Value}.
build_binary(Text, Mapper) ->
Bits = << <<(Mapper(Bit)):1>> || <<Bit>> <= Text>>,
<<N:36/big-unsigned>> = Bits,
N.
map_mask($X) -> 1;
map_mask(_) -> 0.
map_bitset($1) -> 1;
map_bitset(_) -> 0.
process_instruction({mask, Mask, Bits}, {_, _, Mem}) ->
{Mask, Bits, Mem};
process_instruction({mem, Addr, Value}, {Mask, Bits, Mem}) ->
NewMem = Mem#{Addr => (Value band Mask) bor Bits},
{Mask, Bits, NewMem}.