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
934632cc
Commit
934632cc
authored
Dec 24, 2022
by
Falk Rehse
Browse files
Options
Downloads
Patches
Plain Diff
Try Day-15-2
parent
a063a157
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_15_2/mod.rs
+84
-0
84 additions, 0 deletions
src/day_15_2/mod.rs
src/main.rs
+2
-1
2 additions, 1 deletion
src/main.rs
with
86 additions
and
1 deletion
src/day_15_2/mod.rs
0 → 100644
+
84
−
0
View file @
934632cc
const
MIN_X
:
usize
=
0
;
const
MAX_X
:
usize
=
4_000_000
;
const
MIN_Y
:
usize
=
0
;
const
MAX_Y
:
usize
=
4_000_000
;
pub
fn
solve_from_str
(
input
:
&
str
)
->
usize
{
let
pairs
=
input
.split
(
"
\n
"
)
.map
(|
pair
|
{
let
mut
data
=
pair
.split
(
' '
);
let
sensor_x
=
data
.nth
(
2
)
.unwrap
()
.strip_prefix
(
"x="
)
.unwrap
()
.strip_suffix
(
","
)
.unwrap
()
.parse
()
.expect
(
"Expected sensor x-coordinate to be a number!"
);
let
sensor_y
=
data
.nth
(
0
)
.unwrap
()
.strip_prefix
(
"y="
)
.unwrap
()
.strip_suffix
(
":"
)
.unwrap
()
.parse
()
.expect
(
"Expected sensor y-coordinate to be a number!"
);
let
beacon_x
=
data
.nth
(
4
)
.unwrap
()
.strip_prefix
(
"x="
)
.unwrap
()
.strip_suffix
(
","
)
.unwrap
()
.parse
()
.expect
(
"Expected beacon x-coordinate to be a number!"
);
let
beacon_y
=
data
.nth
(
0
)
.unwrap
()
.strip_prefix
(
"y="
)
.unwrap
()
.parse
()
.expect
(
"Expected beacon y-coordinate to be a number!"
);
((
sensor_x
,
sensor_y
),
(
beacon_x
,
beacon_y
))
})
.collect
();
solve
(
&
pairs
)
}
pub
fn
solve
(
pairs
:
&
Vec
<
((
i32
,
i32
),
(
i32
,
i32
))
>
)
->
usize
{
let
areas
:
Vec
<
((
i32
,
i32
),
i32
)
>
=
pairs
.iter
()
.map
(|
pair
|
(
pair
.0
,
get_manhatten_distance
(
&
pair
.0
,
&
pair
.1
)))
.collect
();
let
mut
beacon
=
None
;
for
x
in
MIN_X
..=
MAX_X
{
dbg!
(
x
);
for
y
in
MIN_Y
..=
MAX_Y
{
for
area
in
&
areas
{
if
get_manhatten_distance
(
&
(
x
as
i32
,
y
as
i32
),
&
area
.0
)
<=
area
.1
{
continue
;
}
}
beacon
=
Some
((
x
,
y
));
}
}
let
beacon
=
beacon
.unwrap
();
dbg!
(
&
beacon
);
beacon
.0
*
4_000_000
+
beacon
.1
}
fn
get_manhatten_distance
(
from
:
&
(
i32
,
i32
),
to
:
&
(
i32
,
i32
))
->
i32
{
i32
::
abs
(
from
.0
-
to
.0
)
+
i32
::
abs
(
from
.1
-
to
.1
)
}
This diff is collapsed.
Click to expand it.
src/main.rs
+
2
−
1
View file @
934632cc
...
...
@@ -27,13 +27,14 @@ mod day_13_2;
mod
day_14_1
;
mod
day_14_2
;
mod
day_15_1
;
mod
day_15_2
;
use
std
::
fs
;
fn
main
()
{
let
input
=
fs
::
read_to_string
(
"input"
)
.expect
(
"Could not read input!"
);
let
solution
=
day_15_
1
::
solve_from_str
(
input
.as_str
());
let
solution
=
day_15_
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