diff --git a/day7.erl b/day7.erl index cb8ce2c..c770299 100644 --- a/day7.erl +++ b/day7.erl @@ -5,18 +5,23 @@ solve(InputData) -> Input = binary:split(InputData, <<"\n">>, [trim_all, global]), #{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) -> 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 ..">>, #{cwd := [_Cwd | Parent]} = State) -> State#{cwd := Parent}; execute(<<"$ cd ", Dir/binary>>, #{cwd := Cwd} = State) -> State#{cwd := [Dir | Cwd]}; execute(<<"$ ls">>, State) -> State; execute(<<"dir ", Dir/binary>>, #{cwd := Cwd} = State) -> State#{[Dir | Cwd] => 0}; 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)}. parse_file_command(Command) -> parse_file_command(Command, 0).