Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
A
AoC 2022
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Iterations
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
Advent of Code
AoC 2022
Commits
ee39b08e
Commit
ee39b08e
authored
2 years ago
by
Evy Storozhenko
Browse files
Options
Downloads
Patches
Plain Diff
added mutable collumns
parent
065e1d7b
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
day08/src/main.rs
+15
-7
15 additions, 7 deletions
day08/src/main.rs
day08/src/matrix.rs
+70
-1
70 additions, 1 deletion
day08/src/matrix.rs
with
85 additions
and
8 deletions
day08/src/main.rs
+
15
−
7
View file @
ee39b08e
#![feature(type_alias_impl_trait)]
mod
matrix
;
use
matrix
::
Matrix
;
...
...
@@ -107,13 +109,19 @@ fn main() {
});
});
datamat
.collumns
()
.for_each
(|
col
|
{
col
.for_each
(|
v
|
print!
(
"{}"
,
v
.0
));
datamat
.collumns_mut
()
.for_each
(|
col
|
{
col
.fold
(
-
1
,
|
acc
,
v
|
{
if
v
.0
as
i32
>
acc
{
v
.1
=
true
;
v
.0
as
i32
}
else
{
acc
}
});
});
datamat
.lines
()
.for_each
(|
v
|
{
v
.iter
()
.for_each
(|
x
|
print!
(
"{}"
,
if
x
.1
{
'A'
}
else
{
'_'
}));
println!
();
})
// datamat.lines().for_each(|v| {
// v.iter().for_each(|x| print!("{}", if x.1 { 'A' } else { '_' }));
// println!();
// })
}
This diff is collapsed.
Click to expand it.
day08/src/matrix.rs
+
70
−
1
View file @
ee39b08e
use
std
::{
iter
::
repeat_with
,
iter
::{
repeat_with
,
Take
},
marker
::
PhantomData
,
slice
::{
Chunks
,
ChunksMut
},
};
...
...
@@ -43,6 +44,25 @@ impl<T: Clone> Matrix<T> {
MatrixLinesMut
(
self
.elements
.chunks_mut
(
self
.width
))
}
pub
fn
collumns_mut
(
&
mut
self
)
->
impl
Iterator
<
Item
=
MatrixCollumnMut
<
'_
,
T
>>
{
let
mut
collumn
=
0
;
let
width
=
self
.width
;
MatrixCollumnsMut
{
iter
:
repeat_with
(
move
||
{
let
i
=
collumn
;
collumn
+=
1
;
MatrixCollumnMut
{
collumn
:
i
,
width
,
index
:
0
,
obj
:
self
,
phantom
:
PhantomData
,
}
})
.take
(
width
),
}
}
pub
fn
collumns
(
&
self
)
->
MatrixCollumns
<
T
>
{
MatrixCollumns
::
new
(
self
)
}
...
...
@@ -57,6 +77,53 @@ impl<T: Clone> Matrix<T> {
}
}
pub
struct
MatrixCollumnsMut
<
'a
,
T
,
I
>
where
T
:
Clone
+
'a
,
I
:
Iterator
<
Item
=
MatrixCollumnMut
<
'a
,
T
>>
,
{
iter
:
Take
<
I
>
,
}
impl
<
'a
,
T
,
I
>
Iterator
for
MatrixCollumnsMut
<
'a
,
T
,
I
>
where
T
:
Clone
+
'a
,
I
:
Iterator
<
Item
=
MatrixCollumnMut
<
'a
,
T
>>
,
{
type
Item
=
MatrixCollumnMut
<
'a
,
T
>
;
fn
next
(
&
mut
self
)
->
Option
<
Self
::
Item
>
{
self
.iter
.next
()
}
}
pub
struct
MatrixCollumnMut
<
'a
,
T
:
Clone
>
{
width
:
usize
,
collumn
:
usize
,
index
:
usize
,
obj
:
*
mut
Matrix
<
T
>
,
phantom
:
PhantomData
<&
'a
T
>
,
}
impl
<
'a
,
T
:
Clone
>
Iterator
for
MatrixCollumnMut
<
'a
,
T
>
{
type
Item
=
&
'a
mut
T
;
fn
next
(
&
mut
self
)
->
Option
<
Self
::
Item
>
{
let
i
=
self
.index
;
self
.index
+=
1
;
if
i
<
self
.width
{
Some
(
unsafe
{
&
mut
*
(
*
self
.obj
)
.elements
.as_mut_ptr
()
.add
(
self
.collumn
+
i
*
self
.width
)
})
}
else
{
None
}
}
}
#[derive(Clone,
Copy)]
pub
struct
MatrixCollumns
<
'a
,
T
:
Clone
>
{
parent
:
&
'a
Matrix
<
T
>
,
...
...
@@ -114,6 +181,8 @@ impl<'a, T: Clone> Iterator for MatrixCollumn<'a, T> {
}
}
impl
<
'a
,
T
:
Clone
>
ExactSizeIterator
for
MatrixCollumn
<
'a
,
T
>
{}
pub
struct
MatrixLines
<
'a
,
T
>
(
Chunks
<
'a
,
T
>
);
impl
<
'a
,
T
>
Iterator
for
MatrixLines
<
'a
,
T
>
{
...
...
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