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
065e1d7b
Commit
065e1d7b
authored
Dec 14, 2022
by
Evy Storozhenko
Browse files
Options
Downloads
Patches
Plain Diff
added some stuff for day08
parent
6b030f74
No related branches found
No related tags found
No related merge requests found
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
README.md
+2
-0
2 additions, 0 deletions
README.md
day08/src/main.rs
+46
-6
46 additions, 6 deletions
day08/src/main.rs
day08/src/matrix.rs
+139
-0
139 additions, 0 deletions
day08/src/matrix.rs
with
187 additions
and
6 deletions
README.md
0 → 100644
+
2
−
0
View file @
065e1d7b
# Advent of Code 2022
*Quality may vary*
\ No newline at end of file
This diff is collapsed.
Click to expand it.
day08/src/main.rs
+
46
−
6
View file @
065e1d7b
mod
matrix
;
use
matrix
::
Matrix
;
use
std
::
fs
::
read_to_string
;
fn
main
()
{
fn
solve_without_types
()
{
let
data
=
read_to_string
(
"data.txt"
)
.unwrap
();
let
mut
vismap
=
Vec
::
<
Vec
<
bool
>>
::
new
();
let
width
=
data
.lines
()
.next
()
.unwrap
()
.len
();
let
height
=
data
.lines
()
.count
();
let
mut
vismap
=
Vec
::
<
Vec
<
bool
>>
::
new
();
vismap
.resize
(
height
,
{
let
mut
vec
=
Vec
::
new
();
vec
.resize
(
width
,
false
);
...
...
@@ -72,8 +75,45 @@ fn main() {
.for_each
(|
c
|
print!
(
"{}"
,
if
*
c
{
'A'
}
else
{
'_'
}));
println!
();
});
let
trees
=
vismap
.iter
()
.map
(|
v
|
{
v
.iter
()
.filter
(|
&&
v
|
v
)
.count
()
})
.sum
::
<
usize
>
();
let
trees
=
vismap
.iter
()
.map
(|
v
|
v
.iter
()
.filter
(|
&&
v
|
v
)
.count
())
.sum
::
<
usize
>
();
println!
(
"Trees: {trees}"
);
}
fn
main
()
{
let
data
=
read_to_string
(
"data.txt"
)
.unwrap
();
let
mut
datamat
=
Matrix
::
new_from_str
(
&
data
,
|
c
|
(
c
.to_digit
(
10
)
.unwrap
(),
false
));
datamat
.lines_mut
()
.for_each
(|
line
|
{
line
.iter_mut
()
.fold
(
-
1
,
|
acc
,
v
|
{
if
v
.0
as
i32
>
acc
{
v
.1
=
true
;
v
.0
as
i32
}
else
{
acc
}
});
});
datamat
.lines_mut
()
.for_each
(|
line
|
{
line
.iter_mut
()
.rev
()
.fold
(
-
1
,
|
acc
,
v
|
{
if
v
.0
as
i32
>
acc
{
v
.1
=
true
;
v
.0
as
i32
}
else
{
acc
}
});
});
datamat
.collumns
()
.for_each
(|
col
|
{
col
.for_each
(|
v
|
print!
(
"{}"
,
v
.0
));
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
0 → 100644
+
139
−
0
View file @
065e1d7b
use
std
::{
iter
::
repeat_with
,
slice
::{
Chunks
,
ChunksMut
},
};
pub
struct
Matrix
<
T
:
Clone
>
{
pub
width
:
usize
,
pub
height
:
usize
,
pub
elements
:
Vec
<
T
>
,
}
impl
<
T
:
Clone
>
Matrix
<
T
>
{
pub
fn
new
()
->
Self
{
Self
{
width
:
0
,
height
:
0
,
elements
:
Vec
::
new
(),
}
}
pub
fn
new_from_str
<
F
>
(
source
:
&
str
,
f
:
F
)
->
Self
where
F
:
FnMut
(
char
)
->
T
,
{
let
mut
f_mut
=
f
;
let
mut
iter
=
source
.chars
()
.filter
(|
c
|
c
!=
&
'\n'
);
let
width
=
source
.lines
()
.next
()
.unwrap_or
(
""
)
.len
();
let
height
=
source
.lines
()
.count
();
Self
{
width
,
height
,
elements
:
repeat_with
(||
f_mut
(
iter
.next
()
.unwrap
()))
.take
(
width
*
height
)
.collect
::
<
Vec
<
_
>>
(),
}
}
pub
fn
lines
(
&
self
)
->
MatrixLines
<
T
>
{
MatrixLines
(
self
.elements
.chunks
(
self
.width
))
}
pub
fn
lines_mut
(
&
mut
self
)
->
MatrixLinesMut
<
T
>
{
MatrixLinesMut
(
self
.elements
.chunks_mut
(
self
.width
))
}
pub
fn
collumns
(
&
self
)
->
MatrixCollumns
<
T
>
{
MatrixCollumns
::
new
(
self
)
}
pub
fn
resize
(
&
mut
self
,
width
:
usize
,
height
:
usize
,
value
:
T
)
where
T
:
Clone
,
{
self
.width
=
width
;
self
.height
=
height
;
self
.elements
.resize
(
width
*
height
,
value
);
}
}
#[derive(Clone,
Copy)]
pub
struct
MatrixCollumns
<
'a
,
T
:
Clone
>
{
parent
:
&
'a
Matrix
<
T
>
,
index
:
usize
,
}
impl
<
'a
,
T
:
Clone
>
MatrixCollumns
<
'a
,
T
>
{
pub
fn
new
(
parent
:
&
'a
Matrix
<
T
>
)
->
Self
{
Self
{
parent
,
index
:
0
}
}
}
impl
<
'a
,
T
:
Clone
>
Iterator
for
MatrixCollumns
<
'a
,
T
>
{
type
Item
=
MatrixCollumn
<
'a
,
T
>
;
fn
next
(
&
mut
self
)
->
Option
<
Self
::
Item
>
{
if
self
.index
<
self
.parent.width
{
self
.index
+=
1
;
Some
(
MatrixCollumn
::
new
(
self
.clone
(),
self
.index
-
1
))
}
else
{
None
}
}
}
pub
struct
MatrixCollumn
<
'a
,
T
:
Clone
>
{
parent
:
MatrixCollumns
<
'a
,
T
>
,
offset
:
usize
,
index
:
usize
,
}
impl
<
'a
,
T
:
Clone
>
MatrixCollumn
<
'a
,
T
>
{
pub
fn
new
(
parent
:
MatrixCollumns
<
'a
,
T
>
,
offset
:
usize
)
->
Self
{
Self
{
parent
,
offset
,
index
:
0
,
}
}
}
impl
<
'a
,
T
:
Clone
>
Iterator
for
MatrixCollumn
<
'a
,
T
>
{
type
Item
=
&
'a
T
;
fn
next
(
&
mut
self
)
->
Option
<
Self
::
Item
>
{
if
self
.index
<
self
.parent.parent.height
{
self
.index
+=
1
;
Some
(
&
self
.parent.parent.elements
[
self
.offset
+
(
self
.index
-
1
)
*
self
.parent.parent.width
],
)
}
else
{
None
}
}
}
pub
struct
MatrixLines
<
'a
,
T
>
(
Chunks
<
'a
,
T
>
);
impl
<
'a
,
T
>
Iterator
for
MatrixLines
<
'a
,
T
>
{
type
Item
=
&
'a
[
T
];
fn
next
(
&
mut
self
)
->
Option
<
Self
::
Item
>
{
self
.0
.next
()
}
}
impl
<
'a
,
T
>
ExactSizeIterator
for
MatrixLines
<
'a
,
T
>
{}
pub
struct
MatrixLinesMut
<
'a
,
T
>
(
ChunksMut
<
'a
,
T
>
);
impl
<
'a
,
T
>
Iterator
for
MatrixLinesMut
<
'a
,
T
>
{
type
Item
=
&
'a
mut
[
T
];
fn
next
(
&
mut
self
)
->
Option
<
Self
::
Item
>
{
self
.0
.next
()
}
}
impl
<
'a
,
T
>
ExactSizeIterator
for
MatrixLinesMut
<
'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