Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
A
Advent of Code 2023
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 2023
Commits
3f11be35
Verified
Commit
3f11be35
authored
Dec 10, 2023
by
Falk Rehse
Browse files
Options
Downloads
Patches
Plain Diff
Day 9
parent
66dd172e
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/mod.rs
+22
-0
22 additions, 0 deletions
src/day_9_1/mod.rs
src/day_9_2/mod.rs
+22
-0
22 additions, 0 deletions
src/day_9_2/mod.rs
src/main.rs
+3
-1
3 additions, 1 deletion
src/main.rs
src/util/mod.rs
+7
-0
7 additions, 0 deletions
src/util/mod.rs
with
54 additions
and
1 deletion
src/day_9_1/mod.rs
0 → 100644
+
22
−
0
View file @
3f11be35
use
crate
::
util
::
*
;
pub
fn
solve_from_str
(
input
:
&
str
)
->
i32
{
input
.lines
()
.map
(|
line
|
regex_signed_numbers
(
line
))
.map
(|
series
|
{
let
mut
derivatives
=
Vec
::
new
();
derivatives
.push
(
series
);
while
!
derivatives
.last
()
.unwrap
()
.iter
()
.all
(|
value
|
*
value
==
0
)
{
derivatives
.push
(
derivatives
.last
()
.unwrap
()
.windows
(
2
)
.map
(|
values
|
values
[
1
]
-
values
[
0
])
.collect
());
}
derivatives
.pop
();
let
mut
prediction
=
0
;
for
derivative
in
derivatives
.iter
()
.rev
()
{
prediction
+=
derivative
.last
()
.unwrap
();
}
prediction
})
.sum
()
}
This diff is collapsed.
Click to expand it.
src/day_9_2/mod.rs
0 → 100644
+
22
−
0
View file @
3f11be35
use
crate
::
util
::
*
;
pub
fn
solve_from_str
(
input
:
&
str
)
->
i32
{
input
.lines
()
.map
(|
line
|
regex_signed_numbers
(
line
))
.map
(|
series
|
{
let
mut
derivatives
=
Vec
::
new
();
derivatives
.push
(
series
);
while
!
derivatives
.last
()
.unwrap
()
.iter
()
.all
(|
value
|
*
value
==
0
)
{
derivatives
.push
(
derivatives
.last
()
.unwrap
()
.windows
(
2
)
.map
(|
values
|
values
[
1
]
-
values
[
0
])
.collect
());
}
derivatives
.pop
();
let
mut
prediction
=
0
;
for
derivative
in
derivatives
.iter
()
.rev
()
{
prediction
=
derivative
.first
()
.unwrap
()
-
prediction
;
}
prediction
})
.sum
()
}
This diff is collapsed.
Click to expand it.
src/main.rs
+
3
−
1
View file @
3f11be35
...
...
@@ -14,6 +14,8 @@ mod day_7_1;
mod
day_7_2
;
mod
day_8_1
;
mod
day_8_2
;
mod
day_9_1
;
mod
day_9_2
;
mod
util
;
...
...
@@ -22,7 +24,7 @@ use std::fs;
fn
main
()
{
let
input
=
fs
::
read_to_string
(
"input"
)
.expect
(
"Could not read input!"
);
let
solution
=
day_
8
_2
::
solve_from_str
(
input
.trim_end
());
let
solution
=
day_
9
_2
::
solve_from_str
(
input
.trim_end
());
println!
(
"Solution: {}"
,
solution
);
}
This diff is collapsed.
Click to expand it.
src/util/mod.rs
+
7
−
0
View file @
3f11be35
...
...
@@ -16,6 +16,13 @@ pub fn regex_positive_numbers(input: &str) -> Vec<u32> {
.collect
()
}
pub
fn
regex_signed_numbers
(
input
:
&
str
)
->
Vec
<
i32
>
{
let
re
=
Regex
::
new
(
r"-?\d+"
)
.unwrap
();
re
.find_iter
(
input
)
.map
(|
m
|
str
::
parse
(
m
.as_str
())
.expect
(
"Regex result did not parse as i32!"
))
.collect
()
}
pub
fn
regex_positive_numbers_u64
(
input
:
&
str
)
->
Vec
<
u64
>
{
let
re
=
Regex
::
new
(
r"\d+"
)
.unwrap
();
re
.find_iter
(
input
)
...
...
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