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
09b99777
Commit
09b99777
authored
Dec 10, 2022
by
Falk Rehse
Browse files
Options
Downloads
Patches
Plain Diff
Day 9-1
parent
53613b34
No related branches found
No related tags found
No related merge requests found
Changes
4
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
src/day_9_1/direction.rs
+18
-0
18 additions, 0 deletions
src/day_9_1/direction.rs
src/day_9_1/mod.rs
+116
-0
116 additions, 0 deletions
src/day_9_1/mod.rs
src/day_9_1/movement.rs
+18
-0
18 additions, 0 deletions
src/day_9_1/movement.rs
src/main.rs
+2
-1
2 additions, 1 deletion
src/main.rs
with
154 additions
and
1 deletion
src/day_9_1/direction.rs
0 → 100644
+
18
−
0
View file @
09b99777
pub
enum
Direction
{
Up
,
Down
,
Left
,
Right
,
}
impl
Direction
{
pub
fn
from_str
(
input
:
&
str
)
->
Direction
{
match
input
{
"U"
=>
Self
::
Up
,
"D"
=>
Self
::
Down
,
"L"
=>
Self
::
Left
,
"R"
=>
Self
::
Right
,
_
=>
panic!
(
"Unexpected direction!"
),
}
}
}
This diff is collapsed.
Click to expand it.
src/day_9_1/mod.rs
0 → 100644
+
116
−
0
View file @
09b99777
mod
direction
;
mod
movement
;
use
direction
::
Direction
;
use
movement
::
Movement
;
pub
fn
solve_from_str
(
input
:
&
str
)
->
u32
{
let
moves
=
input
.split
(
'\n'
)
.map
(|
movement
|
Movement
::
from_str
(
movement
))
.collect
();
solve
(
&
moves
)
}
pub
fn
solve
(
moves
:
&
Vec
<
Movement
>
)
->
u32
{
let
mut
visited
=
Vec
::
new
();
visited
.push
((
0
,
0
));
let
mut
head
=
(
0
,
0
);
let
mut
tail
=
(
0
,
0
);
for
movement
in
moves
{
for
_
in
0
..
movement
.amount
{
move_head
(
&
mut
head
,
&
movement
.direction
);
move_tail
(
&
mut
tail
,
&
head
);
if
!
visited
.contains
(
&
tail
)
{
visited
.push
(
tail
);
}
// visited.push(tail);
}
}
// visited.dedup();
visited
.len
()
as
u32
}
fn
move_head
(
head
:
&
mut
(
i32
,
i32
),
direction
:
&
Direction
)
{
match
direction
{
Direction
::
Up
=>
head
.1
+=
1
,
Direction
::
Down
=>
head
.1
-=
1
,
Direction
::
Left
=>
head
.0
-=
1
,
Direction
::
Right
=>
head
.0
+=
1
,
}
}
fn
move_tail
(
tail
:
&
mut
(
i32
,
i32
),
head
:
&
(
i32
,
i32
))
{
if
tail
.0
==
head
.0
-
2
{
if
tail
.1
==
head
.1
-
1
{
tail
.0
+=
1
;
tail
.1
+=
1
;
}
else
if
tail
.1
==
head
.1
{
tail
.0
+=
1
;
}
else
if
tail
.1
==
head
.1
+
1
{
tail
.0
+=
1
;
tail
.1
-=
1
;
}
else
{
panic!
(
"Head lost!"
);
}
}
else
if
tail
.0
==
head
.0
-
1
{
if
tail
.1
==
head
.1
-
2
{
tail
.0
+=
1
;
tail
.1
+=
1
;
}
else
if
tail
.1
==
head
.1
-
1
{
}
else
if
tail
.1
==
head
.1
{
}
else
if
tail
.1
==
head
.1
+
1
{
}
else
if
tail
.1
==
head
.1
+
2
{
tail
.0
+=
1
;
tail
.1
-=
1
;
}
else
{
panic!
(
"Head lost!"
);
}
}
else
if
tail
.0
==
head
.0
{
if
tail
.1
==
head
.1
-
2
{
tail
.1
+=
1
;
}
else
if
tail
.1
==
head
.1
-
1
{
}
else
if
tail
.1
==
head
.1
{
}
else
if
tail
.1
==
head
.1
+
1
{
}
else
if
tail
.1
==
head
.1
+
2
{
tail
.1
-=
1
;
}
else
{
panic!
(
"Head lost!"
);
}
}
else
if
tail
.0
==
head
.0
+
1
{
if
tail
.1
==
head
.1
-
2
{
tail
.0
-=
1
;
tail
.1
+=
1
;
}
else
if
tail
.1
==
head
.1
-
1
{
}
else
if
tail
.1
==
head
.1
{
}
else
if
tail
.1
==
head
.1
+
1
{
}
else
if
tail
.1
==
head
.1
+
2
{
tail
.0
-=
1
;
tail
.1
-=
1
;
}
else
{
panic!
(
"Head lost!"
);
}
}
else
if
tail
.0
==
head
.0
+
2
{
if
tail
.1
==
head
.1
-
1
{
tail
.0
-=
1
;
tail
.1
+=
1
;
}
else
if
tail
.1
==
head
.1
{
tail
.0
-=
1
;
}
else
if
tail
.1
==
head
.1
+
1
{
tail
.0
-=
1
;
tail
.1
-=
1
;
}
else
{
panic!
(
"Head lost!"
);
}
}
else
{
panic!
(
"Head lost!"
);
}
}
This diff is collapsed.
Click to expand it.
src/day_9_1/movement.rs
0 → 100644
+
18
−
0
View file @
09b99777
use
crate
::
day_9_1
::
direction
::
Direction
;
pub
struct
Movement
{
pub
direction
:
Direction
,
pub
amount
:
u32
,
}
impl
Movement
{
pub
fn
from_str
(
input
:
&
str
)
->
Movement
{
let
mut
data
=
input
.split
(
' '
);
let
direction
=
Direction
::
from_str
(
data
.next
()
.expect
(
"Expected direction!"
));
let
amount
=
data
.next
()
.expect
(
"Expected amount!"
)
.parse
()
.expect
(
"Expected amount to be a number!"
);
Movement
{
direction
,
amount
}
}
}
This diff is collapsed.
Click to expand it.
src/main.rs
+
2
−
1
View file @
09b99777
...
@@ -12,13 +12,14 @@ mod day_6_1;
...
@@ -12,13 +12,14 @@ mod day_6_1;
mod
day_6_2
;
mod
day_6_2
;
mod
day_8_1
;
mod
day_8_1
;
mod
day_8_2
;
mod
day_8_2
;
mod
day_9_1
;
use
std
::
fs
;
use
std
::
fs
;
fn
main
()
{
fn
main
()
{
let
input
=
fs
::
read_to_string
(
"input"
)
.expect
(
"Could not read input!"
);
let
input
=
fs
::
read_to_string
(
"input"
)
.expect
(
"Could not read input!"
);
let
solution
=
day_
8_2
::
solve_from_str
(
input
.as_str
());
let
solution
=
day_
9_1
::
solve_from_str
(
input
.as_str
());
println!
(
"Solution: {}"
,
solution
);
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