|
|
@ -5,18 +5,23 @@ |
|
|
solve(InputData) -> |
|
|
solve(InputData) -> |
|
|
Input = binary:split(InputData, <<"\n">>, [trim_all, global]), |
|
|
Input = binary:split(InputData, <<"\n">>, [trim_all, global]), |
|
|
#{sizes := Sizes} = lists:foldl(fun execute/2, #{cwd => [], sizes => #{}}, Input), |
|
|
#{sizes := Sizes} = lists:foldl(fun execute/2, #{cwd => [], sizes => #{}}, Input), |
|
|
{maps:fold(fun adder/3, 0, Sizes), none}. |
|
|
|
|
|
|
|
|
{maps:fold(fun adder/3, 0, Sizes), part2(Sizes)}. |
|
|
|
|
|
|
|
|
adder(_Path, Size, Total) when Size > 100000 -> Total; |
|
|
adder(_Path, Size, Total) when Size > 100000 -> Total; |
|
|
adder(_Path, Size, Total) -> Size + Total. |
|
|
adder(_Path, Size, Total) -> Size + Total. |
|
|
|
|
|
|
|
|
|
|
|
part2(#{[] := FullSize} = SizesIn) -> |
|
|
|
|
|
Sizes0 = maps:values(SizesIn), |
|
|
|
|
|
Sizes1 = lists:filter(fun(Size) -> Size > FullSize - 40000000 end, Sizes0), |
|
|
|
|
|
lists:min(Sizes1). |
|
|
|
|
|
|
|
|
execute(<<"$ cd /">>, State) -> State#{cwd := []}; |
|
|
execute(<<"$ cd /">>, State) -> State#{cwd := []}; |
|
|
execute(<<"$ cd ..">>, #{cwd := [_Cwd | Parent]} = State) -> State#{cwd := Parent}; |
|
|
execute(<<"$ cd ..">>, #{cwd := [_Cwd | Parent]} = State) -> State#{cwd := Parent}; |
|
|
execute(<<"$ cd ", Dir/binary>>, #{cwd := Cwd} = State) -> State#{cwd := [Dir | Cwd]}; |
|
|
execute(<<"$ cd ", Dir/binary>>, #{cwd := Cwd} = State) -> State#{cwd := [Dir | Cwd]}; |
|
|
execute(<<"$ ls">>, State) -> State; |
|
|
execute(<<"$ ls">>, State) -> State; |
|
|
execute(<<"dir ", Dir/binary>>, #{cwd := Cwd} = State) -> State#{[Dir | Cwd] => 0}; |
|
|
execute(<<"dir ", Dir/binary>>, #{cwd := Cwd} = State) -> State#{[Dir | Cwd] => 0}; |
|
|
execute(FileCommand ,#{cwd := Cwd, sizes := Sizes} = State) -> |
|
|
execute(FileCommand ,#{cwd := Cwd, sizes := Sizes} = State) -> |
|
|
{Filename, Size} = parse_file_command(FileCommand), |
|
|
|
|
|
|
|
|
{_Filename, Size} = parse_file_command(FileCommand), |
|
|
State#{sizes := add_sizes(Cwd, Size, Sizes)}. |
|
|
State#{sizes := add_sizes(Cwd, Size, Sizes)}. |
|
|
|
|
|
|
|
|
parse_file_command(Command) -> parse_file_command(Command, 0). |
|
|
parse_file_command(Command) -> parse_file_command(Command, 0). |
|
|
|