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
726c9763
Commit
726c9763
authored
Dec 16, 2023
by
Falk Rehse
Browse files
Options
Downloads
Patches
Plain Diff
Attemp at Day 14-2
Unfortunately way to slow
parent
8f67be2d
No related branches found
No related tags found
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/day_14_2/mod.rs
+162
-0
162 additions, 0 deletions
src/day_14_2/mod.rs
with
162 additions
and
0 deletions
src/day_14_2/mod.rs
0 → 100644
+
162
−
0
View file @
726c9763
use
std
::{
collections
::{
hash_map
::
DefaultHasher
,
HashMap
},
hash
::{
Hash
,
Hasher
},
};
use
crate
::
util
::
*
;
pub
fn
solve_from_str
(
input
:
&
str
)
->
usize
{
let
mut
grid
=
to_grid_flipped_axis
(
input
);
let
height
=
grid
.len
();
let
mut
hashes
=
HashMap
::
new
();
let
total_cycles
=
10
;
let
mut
skipped
=
false
;
let
mut
i
:
usize
=
0
;
while
i
<
total_cycles
{
tilt_up
(
&
mut
grid
);
tilt_left
(
&
mut
grid
);
tilt_down
(
&
mut
grid
);
tilt_right
(
&
mut
grid
);
if
!
skipped
{
let
mut
hasher
=
DefaultHasher
::
new
();
Hash
::
hash_slice
(
&
grid
,
&
mut
hasher
);
let
cycle_hash
=
hasher
.finish
();
if
hashes
.contains_key
(
&
cycle_hash
)
{
let
previous
=
hashes
.get
(
&
cycle_hash
)
.unwrap
();
dbg!
(
previous
);
dbg!
(
i
);
let
period_length
=
i
-
previous
;
while
i
+
period_length
<
total_cycles
{
i
+=
period_length
;
}
skipped
=
true
;
dbg!
(
i
);
}
else
{
hashes
.insert
(
cycle_hash
,
i
);
i
+=
1
;
}
}
else
{
i
+=
1
;
}
}
print_grid_flipped_axis
(
&
grid
);
grid
.iter
()
.enumerate
()
.map
(|(
i
,
line
)|
get_line_value
(
height
-
i
,
line
))
.sum
()
}
fn
tilt_up
(
grid
:
&
mut
Vec
<
Vec
<
char
>>
)
{
let
width
=
grid
[
0
]
.len
();
let
height
=
grid
.len
();
for
y
in
0
..
height
{
for
x
in
0
..
width
{
if
grid
[
y
][
x
]
==
'O'
{
let
mut
y_valid
=
y
;
for
y_new
in
(
0
..
y
)
.rev
()
{
if
grid
[
y_new
][
x
]
==
'.'
{
y_valid
=
y_new
;
}
else
{
break
;
}
}
grid
[
y
][
x
]
=
'.'
;
grid
[
y_valid
][
x
]
=
'O'
;
}
}
}
}
fn
tilt_down
(
grid
:
&
mut
Vec
<
Vec
<
char
>>
)
{
let
width
=
grid
[
0
]
.len
();
let
height
=
grid
.len
();
for
y
in
(
0
..
height
)
.rev
()
{
for
x
in
(
0
..
width
)
.rev
()
{
if
grid
[
y
][
x
]
==
'O'
{
let
mut
y_valid
=
y
;
for
y_new
in
(
y
+
1
)
..
height
{
if
grid
[
y_new
][
x
]
==
'.'
{
y_valid
=
y_new
;
}
else
{
break
;
}
}
grid
[
y
][
x
]
=
'.'
;
grid
[
y_valid
][
x
]
=
'O'
;
}
}
}
}
fn
tilt_left
(
grid
:
&
mut
Vec
<
Vec
<
char
>>
)
{
let
width
=
grid
[
0
]
.len
();
let
height
=
grid
.len
();
for
x
in
0
..
width
{
for
y
in
0
..
height
{
if
grid
[
y
][
x
]
==
'O'
{
let
mut
x_valid
=
x
;
for
x_new
in
(
0
..
x
)
.rev
()
{
if
grid
[
y
][
x_new
]
==
'.'
{
x_valid
=
x_new
;
}
else
{
break
;
}
}
grid
[
y
][
x
]
=
'.'
;
grid
[
y
][
x_valid
]
=
'O'
;
}
}
}
}
fn
tilt_right
(
grid
:
&
mut
Vec
<
Vec
<
char
>>
)
{
let
width
=
grid
[
0
]
.len
();
let
height
=
grid
.len
();
for
x
in
(
0
..
width
)
.rev
()
{
for
y
in
(
0
..
height
)
.rev
()
{
if
grid
[
y
][
x
]
==
'O'
{
let
mut
x_valid
=
x
;
for
x_new
in
(
x
+
1
)
..
width
{
if
grid
[
y
][
x_new
]
==
'.'
{
x_valid
=
x_new
;
}
else
{
break
;
}
}
grid
[
y
][
x
]
=
'.'
;
grid
[
y
][
x_valid
]
=
'O'
;
}
}
}
}
fn
get_line_value
(
multiplier
:
usize
,
line
:
&
Vec
<
char
>
)
->
usize
{
line
.iter
()
.map
(|
char
|
if
*
char
==
'O'
{
multiplier
}
else
{
0
})
.sum
()
}
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