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
2c300cea
Commit
2c300cea
authored
Dec 11, 2022
by
Falk Rehse
Browse files
Options
Downloads
Patches
Plain Diff
Day 10-1
parent
1e11f5a3
Branches
Branches containing commit
No related tags found
No related merge requests found
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
src/day_10_1/command.rs
+22
-0
22 additions, 0 deletions
src/day_10_1/command.rs
src/day_10_1/mod.rs
+44
-0
44 additions, 0 deletions
src/day_10_1/mod.rs
src/main.rs
+2
-1
2 additions, 1 deletion
src/main.rs
with
68 additions
and
1 deletion
src/day_10_1/command.rs
0 → 100644
+
22
−
0
View file @
2c300cea
pub
enum
Command
{
NoOp
{
cycles
:
u32
},
AddX
{
cycles
:
u32
,
value
:
i32
},
}
impl
Command
{
pub
fn
from_str
(
input
:
&
str
)
->
Command
{
match
input
.split
(
' '
)
.next
()
.expect
(
"Expected command!"
)
{
"noop"
=>
Self
::
NoOp
{
cycles
:
1
},
"addx"
=>
Self
::
AddX
{
cycles
:
2
,
value
:
input
.split
(
' '
)
.nth
(
1
)
.expect
(
"Expected value!"
)
.parse
()
.expect
(
"Expected value to be a number!"
),
},
_
=>
panic!
(
"Unexpected command!"
),
}
}
}
This diff is collapsed.
Click to expand it.
src/day_10_1/mod.rs
0 → 100644
+
44
−
0
View file @
2c300cea
mod
command
;
use
command
::
Command
;
pub
fn
solve_from_str
(
input
:
&
str
)
->
i32
{
let
commands
=
input
.split
(
'\n'
)
.map
(|
command
|
Command
::
from_str
(
command
))
.collect
();
solve
(
&
commands
)
}
pub
fn
solve
(
commands
:
&
Vec
<
Command
>
)
->
i32
{
let
mut
cycle
=
1
;
let
mut
x
=
1
;
let
mut
signal_strengths
=
Vec
::
new
();
for
command
in
commands
{
match
command
{
Command
::
NoOp
{
cycles
}
=>
{
increment_cycle
(
&
mut
cycle
,
&
mut
signal_strengths
,
cycles
,
&
x
);
},
Command
::
AddX
{
cycles
,
value
}
=>
{
increment_cycle
(
&
mut
cycle
,
&
mut
signal_strengths
,
cycles
,
&
x
);
x
+=
value
;
},
}
}
signal_strengths
.iter
()
.sum
()
}
fn
increment_cycle
(
cycle
:
&
mut
u32
,
signal_strengths
:
&
mut
Vec
<
i32
>
,
cycles
:
&
u32
,
x
:
&
i32
)
{
for
_
in
0
..*
cycles
{
if
[
20
,
60
,
100
,
140
,
180
,
220
]
.contains
(
cycle
)
{
signal_strengths
.push
(
*
x
*
*
cycle
as
i32
);
}
*
cycle
+=
1
;
}
}
This diff is collapsed.
Click to expand it.
src/main.rs
+
2
−
1
View file @
2c300cea
...
...
@@ -14,13 +14,14 @@ mod day_8_1;
mod
day_8_2
;
mod
day_9_1
;
mod
day_9_2
;
mod
day_10_1
;
use
std
::
fs
;
fn
main
()
{
let
input
=
fs
::
read_to_string
(
"input"
)
.expect
(
"Could not read input!"
);
let
solution
=
day_
9_2
::
solve_from_str
(
input
.as_str
());
let
solution
=
day_
10_1
::
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