Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
A
Advent of Code 2022
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Falk Rehse
Advent of Code 2022
Commits
a91bfb2c
Commit
a91bfb2c
authored
Dec 12, 2022
by
Falk Rehse
Browse files
Options
Downloads
Patches
Plain Diff
Simplify
parent
19a9ea6a
Branches
Branches containing commit
No related tags found
1 merge request
!1
Day 7
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
src/day_7_1/file_tree.rs
+45
-53
45 additions, 53 deletions
src/day_7_1/file_tree.rs
src/day_7_1/mod.rs
+1
-3
1 addition, 3 deletions
src/day_7_1/mod.rs
with
46 additions
and
56 deletions
src/day_7_1/file_tree.rs
+
45
−
53
View file @
a91bfb2c
use
std
::
rc
::
Rc
;
use
crate
::
day_7_1
::
command
::
Command
;
use
crate
::
day_7_1
::
node
::
Node
::
File
;
#[derive(Debug,
Clone)]
pub
enum
FileTree
{
File
{
name
:
String
,
size
:
u32
},
Directory
{
name
:
String
,
files
:
Vec
<
FileTree
>
},
File
{
name
:
String
,
parent
:
Option
<
Rc
<
FileTree
>>
,
size
:
u32
,
},
Directory
{
name
:
String
,
parent
:
Option
<
Rc
<
FileTree
>>
,
files
:
Vec
<
FileTree
>
,
},
}
impl
FileTree
{
pub
fn
new
()
->
FileTree
{
Self
::
Directory
{
name
:
""
.to_string
(),
parent
:
None
,
files
:
Vec
::
new
(),
}
}
pub
fn
fill_
from_commands
(
&
mut
self
,
commands
:
&
Vec
<
Command
>
)
{
let
mut
current_path
=
"/"
.to_string
(
);
pub
fn
from_commands
(
commands
:
&
Vec
<
Command
>
)
->
FileTree
{
let
mut
tree
=
Rc
::
new
(
Self
::
new
()
);
for
command
in
commands
{
match
command
{
Command
::
ChangeDir
{
path
}
=>
{
current_path
=
s
elf
.
change_path
(
&
current_path
,
&
path
);
tree
=
S
elf
::
change_path
(
tree
,
&
path
);
}
Command
::
List
{
files
}
=>
{
let
tree
=
self
.create_path
(
&
current_path
);
for
file
in
files
{
if
let
File
{
name
,
size
}
=
file
{
tree
.add_file
(
name
,
size
);
}
// tree.add_file(Rc::clone(&mut tree), name, size);
}
}
}
}
}
fn
change_path
<
'a
>
(
&
self
,
current_path
:
&
'a
str
,
cd_path
:
&
'a
str
)
->
String
{
if
cd_path
.starts_with
(
"/"
)
{
cd_path
.to_string
()
}
else
if
cd_path
==
".."
{
let
mut
path
:
Vec
<&
str
>
=
current_path
.split
(
'/'
)
.collect
();
let
current_dir
=
path
.pop
()
.unwrap
();
(
*
tree
)
.clone
()
}
current_path
.strip_suffix
(
&
format!
(
"/{}"
,
current_dir
))
.unwrap
()
.to_string
()
fn
change_path
(
tree
:
Rc
<
FileTree
>
,
path
:
&
str
)
->
Rc
<
FileTree
>
{
if
path
==
"/"
{
Rc
::
clone
(
&
tree
)
}
else
if
path
==
".."
{
Rc
::
clone
(
&
tree
.get_parent
()
.as_ref
()
.unwrap
())
}
else
{
format!
(
"{}/{}"
,
current_path
,
cd_
path
)
Self
::
create_directory
(
tree
,
path
)
}
}
fn
create_path
(
&
mut
self
,
path
:
&
str
)
->
&
mut
FileTree
{
let
mut
tree
=
self
;
let
segments
=
path
.split
(
'/'
);
for
segment
in
segments
{
let
new_tree
=
if
let
Self
::
Directory
{
files
,
..
}
=
tree
{
let
new_tree
=
files
.iter_mut
()
.find
(|
tree
|
match
tree
{
Self
::
Directory
{
name
,
..
}
=>
name
==
segment
,
_
=>
false
,
});
new_tree
}
else
{
panic!
(
"Expected a directory!"
);
};
tree
=
if
let
Some
(
new_tree
)
=
new_tree
{
new_tree
}
else
{
panic!
();
fn
create_directory
(
mut
tree
:
Rc
<
FileTree
>
,
path
:
&
str
)
->
Rc
<
FileTree
>
{
let
file_tree
:
&
mut
FileTree
=
&
mut
tree
;
/* if let Self::Directory { files, .. } = tree {
let mut new_dir = Self::Directory {
name: segment.to_string(),
if
let
Self
::
Directory
{
files
,
..
}
=
file_tree
{
let
mut
directory
=
Self
::
Directory
{
name
:
path
.to_string
(),
parent
:
Some
(
Rc
::
clone
(
&
tree
)),
files
:
Vec
::
new
(),
};
files.push(new_dir);
&mut new_dir
files
.push
(
directory
);
Rc
::
new
(
directory
)
}
else
{
panic!
(
"Expected a directory!"
);
} */
}
}
tree
}
fn
add_file
(
&
mut
self
,
name
:
&
str
,
size
:
&
u32
)
{
fn
add_file
(
&
mut
self
,
tree
:
Rc
<
FileTree
>
,
name
:
&
str
,
size
:
&
u32
)
{
if
let
Self
::
Directory
{
files
,
..
}
=
self
{
files
.push
(
Self
::
File
{
name
:
name
.to_string
(),
parent
:
Some
(
tree
),
size
:
size
.clone
(),
})
}
}
fn
get_parent
(
&
mut
self
)
->
&
mut
Option
<
Rc
<
FileTree
>>
{
match
self
{
Self
::
Directory
{
parent
,
..
}
=>
parent
,
Self
::
File
{
parent
,
..
}
=>
parent
,
}
}
}
This diff is collapsed.
Click to expand it.
src/day_7_1/mod.rs
+
1
−
3
View file @
a91bfb2c
...
...
@@ -14,9 +14,7 @@ pub fn solve_from_str(input: &str) -> u32 {
let
commands
=
commands
.map
(|
command
|
Command
::
from_str
(
command
))
.collect
();
let
mut
file_tree
=
FileTree
::
new
();
file_tree
.fill_from_commands
(
&
commands
);
let
file_tree
=
FileTree
::
from_commands
(
&
commands
);
solve
(
file_tree
)
}
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment