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
ea95a0cd
Commit
ea95a0cd
authored
Dec 12, 2022
by
Falk Rehse
Browse files
Options
Downloads
Patches
Plain Diff
Day 12-2
parent
45572c62
No related branches found
No related tags found
No related merge requests found
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
src/day_12_2/mod.rs
+115
-0
115 additions, 0 deletions
src/day_12_2/mod.rs
src/main.rs
+2
-1
2 additions, 1 deletion
src/main.rs
with
117 additions
and
1 deletion
src/day_12_2/mod.rs
0 → 100644
+
115
−
0
View file @
ea95a0cd
use
petgraph
::
algo
;
use
petgraph
::
graph
::
Graph
;
use
petgraph
::
graph
::
NodeIndex
;
const
START_CHAR
:
u32
=
'S'
as
u32
;
const
END_CHAR
:
u32
=
'E'
as
u32
;
pub
fn
solve_from_str
(
input
:
&
str
)
->
u32
{
let
heights
=
input
.split
(
"
\n
"
)
.map
(|
row
|
row
.chars
()
.collect
())
.collect
();
solve
(
&
heights
)
}
pub
fn
solve
(
heights
:
&
Vec
<
Vec
<
char
>>
)
->
u32
{
let
mut
graph
=
Graph
::
<
_
,
()
>
::
new
();
let
mut
nodes
=
Vec
::
with_capacity
(
heights
.len
());
let
mut
start
=
Vec
::
new
();
let
mut
end
=
None
;
for
y
in
0
..
heights
.len
()
{
let
mut
row
=
Vec
::
with_capacity
(
heights
[
y
]
.len
());
for
x
in
0
..
heights
[
y
]
.len
()
{
let
node
=
graph
.add_node
(
heights
[
y
][
x
]);
if
heights
[
y
][
x
]
==
'a'
||
heights
[
y
][
x
]
==
'S'
{
start
.push
(
node
)
}
if
heights
[
y
][
x
]
==
'E'
{
end
=
Some
(
node
);
}
row
.push
(
node
);
}
nodes
.push
(
row
);
}
for
y
in
0
..
heights
.len
()
{
for
x
in
0
..
heights
[
y
]
.len
()
{
add_edges
(
&
mut
graph
,
&
nodes
,
y
,
x
);
}
}
let
end
=
end
.expect
(
"Expected an end node!"
);
let
mut
distances
=
Vec
::
new
();
for
start
in
start
{
let
dijkstra
=
algo
::
dijkstra
(
&
graph
,
start
,
Some
(
end
),
|
_
|
1
);
if
let
Some
(
dijkstra
)
=
dijkstra
.get
(
&
end
)
{
distances
.push
(
*
dijkstra
);
}
}
distances
.sort_unstable
();
*
distances
.first
()
.expect
(
"Expected at least one distance!"
)
}
fn
add_edges
(
graph
:
&
mut
Graph
<
char
,
()
>
,
nodes
:
&
Vec
<
Vec
<
NodeIndex
>>
,
y
:
usize
,
x
:
usize
)
{
let
current_node
=
nodes
[
y
][
x
];
let
current_height
=
get_height
(
graph
[
current_node
]);
if
x
>
0
{
if
let
Some
(
nodes
)
=
nodes
.get
(
y
)
{
if
let
Some
(
node
)
=
nodes
.get
(
x
-
1
)
{
if
get_height
(
graph
[
*
node
])
<=
current_height
+
1
{
graph
.add_edge
(
current_node
,
*
node
,
());
}
}
}
}
if
let
Some
(
nodes
)
=
nodes
.get
(
y
)
{
if
let
Some
(
node
)
=
nodes
.get
(
x
+
1
)
{
if
get_height
(
graph
[
*
node
])
<=
current_height
+
1
{
graph
.add_edge
(
current_node
,
*
node
,
());
}
}
}
if
y
>
0
{
if
let
Some
(
nodes
)
=
nodes
.get
(
y
-
1
)
{
if
let
Some
(
node
)
=
nodes
.get
(
x
)
{
if
get_height
(
graph
[
*
node
])
<=
current_height
+
1
{
graph
.add_edge
(
current_node
,
*
node
,
());
}
}
}
}
if
let
Some
(
nodes
)
=
nodes
.get
(
y
+
1
)
{
if
let
Some
(
node
)
=
nodes
.get
(
x
)
{
if
get_height
(
graph
[
*
node
])
<=
current_height
+
1
{
graph
.add_edge
(
current_node
,
*
node
,
());
}
}
}
}
fn
get_height
(
char
:
char
)
->
u32
{
let
charcode
=
char
as
u32
;
match
charcode
{
97
..=
122
=>
charcode
-
96
,
START_CHAR
=>
get_height
(
'a'
),
END_CHAR
=>
get_height
(
'z'
),
_
=>
panic!
(
"Illegal charcode: {}"
,
charcode
),
}
}
This diff is collapsed.
Click to expand it.
src/main.rs
+
2
−
1
View file @
ea95a0cd
...
...
@@ -19,13 +19,14 @@ mod day_10_2;
mod
day_11_1
;
mod
day_11_2
;
mod
day_12_1
;
mod
day_12_2
;
use
std
::
fs
;
fn
main
()
{
let
input
=
fs
::
read_to_string
(
"input"
)
.expect
(
"Could not read input!"
);
let
solution
=
day_12_
1
::
solve_from_str
(
input
.as_str
());
let
solution
=
day_12_
2
::
solve_from_str
(
input
.as_str
());
println!
(
"Solution: {}"
,
solution
);
}
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