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
abcfc3a1
Commit
abcfc3a1
authored
1 year ago
by
Falk Rehse
Browse files
Options
Downloads
Patches
Plain Diff
Day 16
parent
726c9763
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
src/day_16_1/mod.rs
+257
-0
257 additions, 0 deletions
src/day_16_1/mod.rs
src/day_16_2/mod.rs
+284
-0
284 additions, 0 deletions
src/day_16_2/mod.rs
src/main.rs
+4
-1
4 additions, 1 deletion
src/main.rs
with
545 additions
and
1 deletion
src/day_16_1/mod.rs
0 → 100644
+
257
−
0
View file @
abcfc3a1
use
std
::
collections
::
HashMap
;
use
crate
::
util
::
*
;
pub
fn
solve_from_str
(
input
:
&
str
)
->
u32
{
let
grid
=
to_grid
(
input
);
let
mut
energized
=
HashMap
::
new
();
follow_beam
(
0
,
0
,
Direction
::
Right
,
&
grid
,
&
mut
energized
);
energized
.iter
()
.map
(|
_
|
1
)
.sum
()
}
fn
follow_beam
(
x
:
i32
,
y
:
i32
,
direction
:
Direction
,
grid
:
&
Vec
<
Vec
<
char
>>
,
energized
:
&
mut
HashMap
<
(
i32
,
i32
),
Vec
<
Direction
>>
,
)
{
if
x
<
0
||
x
>=
grid
.len
()
as
i32
{
return
;
}
if
y
<
0
||
y
>=
grid
[
0
]
.len
()
as
i32
{
return
;
}
if
let
Some
(
directions
)
=
energized
.get_mut
(
&
(
x
,
y
))
{
if
directions
.contains
(
&
direction
)
{
return
;
}
else
{
directions
.push
(
direction
);
}
}
else
{
energized
.insert
((
x
,
y
),
vec!
[
direction
]);
}
match
grid
[
x
as
usize
][
y
as
usize
]
{
'.'
=>
follow_beam
(
x
+
direction
.to_vec
()
.0
,
y
+
direction
.to_vec
()
.1
,
direction
,
grid
,
energized
,
),
'/'
=>
match
direction
{
Direction
::
Up
=>
{
let
direction
=
Direction
::
Right
;
follow_beam
(
x
+
direction
.to_vec
()
.0
,
y
+
direction
.to_vec
()
.1
,
direction
,
grid
,
energized
,
);
}
Direction
::
Down
=>
{
let
direction
=
Direction
::
Left
;
follow_beam
(
x
+
direction
.to_vec
()
.0
,
y
+
direction
.to_vec
()
.1
,
direction
,
grid
,
energized
,
);
}
Direction
::
Left
=>
{
let
direction
=
Direction
::
Down
;
follow_beam
(
x
+
direction
.to_vec
()
.0
,
y
+
direction
.to_vec
()
.1
,
direction
,
grid
,
energized
,
);
}
Direction
::
Right
=>
{
let
direction
=
Direction
::
Up
;
follow_beam
(
x
+
direction
.to_vec
()
.0
,
y
+
direction
.to_vec
()
.1
,
direction
,
grid
,
energized
,
);
}
},
'\\'
=>
match
direction
{
Direction
::
Up
=>
{
let
direction
=
Direction
::
Left
;
follow_beam
(
x
+
direction
.to_vec
()
.0
,
y
+
direction
.to_vec
()
.1
,
direction
,
grid
,
energized
,
);
}
Direction
::
Down
=>
{
let
direction
=
Direction
::
Right
;
follow_beam
(
x
+
direction
.to_vec
()
.0
,
y
+
direction
.to_vec
()
.1
,
direction
,
grid
,
energized
,
);
}
Direction
::
Left
=>
{
let
direction
=
Direction
::
Up
;
follow_beam
(
x
+
direction
.to_vec
()
.0
,
y
+
direction
.to_vec
()
.1
,
direction
,
grid
,
energized
,
);
}
Direction
::
Right
=>
{
let
direction
=
Direction
::
Down
;
follow_beam
(
x
+
direction
.to_vec
()
.0
,
y
+
direction
.to_vec
()
.1
,
direction
,
grid
,
energized
,
);
}
},
'-'
=>
match
direction
{
Direction
::
Up
=>
{
let
direction
=
Direction
::
Left
;
follow_beam
(
x
+
direction
.to_vec
()
.0
,
y
+
direction
.to_vec
()
.1
,
direction
,
grid
,
energized
,
);
let
direction
=
Direction
::
Right
;
follow_beam
(
x
+
direction
.to_vec
()
.0
,
y
+
direction
.to_vec
()
.1
,
direction
,
grid
,
energized
,
);
}
Direction
::
Down
=>
{
let
direction
=
Direction
::
Left
;
follow_beam
(
x
+
direction
.to_vec
()
.0
,
y
+
direction
.to_vec
()
.1
,
direction
,
grid
,
energized
,
);
let
direction
=
Direction
::
Right
;
follow_beam
(
x
+
direction
.to_vec
()
.0
,
y
+
direction
.to_vec
()
.1
,
direction
,
grid
,
energized
,
);
}
Direction
::
Left
=>
follow_beam
(
x
+
direction
.to_vec
()
.0
,
y
+
direction
.to_vec
()
.1
,
direction
,
grid
,
energized
,
),
Direction
::
Right
=>
follow_beam
(
x
+
direction
.to_vec
()
.0
,
y
+
direction
.to_vec
()
.1
,
direction
,
grid
,
energized
,
),
},
'|'
=>
match
direction
{
Direction
::
Up
=>
follow_beam
(
x
+
direction
.to_vec
()
.0
,
y
+
direction
.to_vec
()
.1
,
direction
,
grid
,
energized
,
),
Direction
::
Down
=>
follow_beam
(
x
+
direction
.to_vec
()
.0
,
y
+
direction
.to_vec
()
.1
,
direction
,
grid
,
energized
,
),
Direction
::
Left
=>
{
let
direction
=
Direction
::
Up
;
follow_beam
(
x
+
direction
.to_vec
()
.0
,
y
+
direction
.to_vec
()
.1
,
direction
,
grid
,
energized
,
);
let
direction
=
Direction
::
Down
;
follow_beam
(
x
+
direction
.to_vec
()
.0
,
y
+
direction
.to_vec
()
.1
,
direction
,
grid
,
energized
,
);
}
Direction
::
Right
=>
{
let
direction
=
Direction
::
Up
;
follow_beam
(
x
+
direction
.to_vec
()
.0
,
y
+
direction
.to_vec
()
.1
,
direction
,
grid
,
energized
,
);
let
direction
=
Direction
::
Down
;
follow_beam
(
x
+
direction
.to_vec
()
.0
,
y
+
direction
.to_vec
()
.1
,
direction
,
grid
,
energized
,
);
}
},
_
=>
panic!
(),
}
}
#[derive(Debug,
Clone,
Copy,
PartialEq,
Eq)]
enum
Direction
{
Up
,
Down
,
Left
,
Right
,
}
impl
Direction
{
fn
to_vec
(
&
self
)
->
(
i32
,
i32
)
{
match
self
{
Direction
::
Up
=>
(
0
,
-
1
),
Direction
::
Down
=>
(
0
,
1
),
Direction
::
Left
=>
(
-
1
,
0
),
Direction
::
Right
=>
(
1
,
0
),
}
}
}
This diff is collapsed.
Click to expand it.
src/day_16_2/mod.rs
0 → 100644
+
284
−
0
View file @
abcfc3a1
use
std
::
collections
::
HashMap
;
use
crate
::
util
::
*
;
pub
fn
solve_from_str
(
input
:
&
str
)
->
u32
{
let
grid
=
to_grid
(
input
);
let
mut
max
=
0
;
for
x
in
0
..
grid
.len
()
as
i32
{
let
mut
energized
=
HashMap
::
new
();
follow_beam
(
x
,
0
,
Direction
::
Down
,
&
grid
,
&
mut
energized
);
max
=
u32
::
max
(
max
,
energized
.iter
()
.map
(|
_
|
1
)
.sum
());
}
for
x
in
0
..
grid
.len
()
as
i32
{
let
mut
energized
=
HashMap
::
new
();
follow_beam
(
x
,
grid
[
0
]
.len
()
as
i32
-
1
,
Direction
::
Up
,
&
grid
,
&
mut
energized
);
max
=
u32
::
max
(
max
,
energized
.iter
()
.map
(|
_
|
1
)
.sum
());
}
for
y
in
0
..
grid
[
0
]
.len
()
as
i32
{
let
mut
energized
=
HashMap
::
new
();
follow_beam
(
0
,
y
,
Direction
::
Right
,
&
grid
,
&
mut
energized
);
max
=
u32
::
max
(
max
,
energized
.iter
()
.map
(|
_
|
1
)
.sum
());
}
for
y
in
0
..
grid
[
0
]
.len
()
as
i32
{
let
mut
energized
=
HashMap
::
new
();
follow_beam
(
grid
.len
()
as
i32
-
1
,
y
,
Direction
::
Left
,
&
grid
,
&
mut
energized
);
max
=
u32
::
max
(
max
,
energized
.iter
()
.map
(|
_
|
1
)
.sum
());
}
max
}
fn
follow_beam
(
x
:
i32
,
y
:
i32
,
direction
:
Direction
,
grid
:
&
Vec
<
Vec
<
char
>>
,
energized
:
&
mut
HashMap
<
(
i32
,
i32
),
Vec
<
Direction
>>
,
)
{
if
x
<
0
||
x
>=
grid
.len
()
as
i32
{
return
;
}
if
y
<
0
||
y
>=
grid
[
0
]
.len
()
as
i32
{
return
;
}
if
let
Some
(
directions
)
=
energized
.get_mut
(
&
(
x
,
y
))
{
if
directions
.contains
(
&
direction
)
{
return
;
}
else
{
directions
.push
(
direction
);
}
}
else
{
energized
.insert
((
x
,
y
),
vec!
[
direction
]);
}
match
grid
[
x
as
usize
][
y
as
usize
]
{
'.'
=>
follow_beam
(
x
+
direction
.to_vec
()
.0
,
y
+
direction
.to_vec
()
.1
,
direction
,
grid
,
energized
,
),
'/'
=>
match
direction
{
Direction
::
Up
=>
{
let
direction
=
Direction
::
Right
;
follow_beam
(
x
+
direction
.to_vec
()
.0
,
y
+
direction
.to_vec
()
.1
,
direction
,
grid
,
energized
,
);
}
Direction
::
Down
=>
{
let
direction
=
Direction
::
Left
;
follow_beam
(
x
+
direction
.to_vec
()
.0
,
y
+
direction
.to_vec
()
.1
,
direction
,
grid
,
energized
,
);
}
Direction
::
Left
=>
{
let
direction
=
Direction
::
Down
;
follow_beam
(
x
+
direction
.to_vec
()
.0
,
y
+
direction
.to_vec
()
.1
,
direction
,
grid
,
energized
,
);
}
Direction
::
Right
=>
{
let
direction
=
Direction
::
Up
;
follow_beam
(
x
+
direction
.to_vec
()
.0
,
y
+
direction
.to_vec
()
.1
,
direction
,
grid
,
energized
,
);
}
},
'\\'
=>
match
direction
{
Direction
::
Up
=>
{
let
direction
=
Direction
::
Left
;
follow_beam
(
x
+
direction
.to_vec
()
.0
,
y
+
direction
.to_vec
()
.1
,
direction
,
grid
,
energized
,
);
}
Direction
::
Down
=>
{
let
direction
=
Direction
::
Right
;
follow_beam
(
x
+
direction
.to_vec
()
.0
,
y
+
direction
.to_vec
()
.1
,
direction
,
grid
,
energized
,
);
}
Direction
::
Left
=>
{
let
direction
=
Direction
::
Up
;
follow_beam
(
x
+
direction
.to_vec
()
.0
,
y
+
direction
.to_vec
()
.1
,
direction
,
grid
,
energized
,
);
}
Direction
::
Right
=>
{
let
direction
=
Direction
::
Down
;
follow_beam
(
x
+
direction
.to_vec
()
.0
,
y
+
direction
.to_vec
()
.1
,
direction
,
grid
,
energized
,
);
}
},
'-'
=>
match
direction
{
Direction
::
Up
=>
{
let
direction
=
Direction
::
Left
;
follow_beam
(
x
+
direction
.to_vec
()
.0
,
y
+
direction
.to_vec
()
.1
,
direction
,
grid
,
energized
,
);
let
direction
=
Direction
::
Right
;
follow_beam
(
x
+
direction
.to_vec
()
.0
,
y
+
direction
.to_vec
()
.1
,
direction
,
grid
,
energized
,
);
}
Direction
::
Down
=>
{
let
direction
=
Direction
::
Left
;
follow_beam
(
x
+
direction
.to_vec
()
.0
,
y
+
direction
.to_vec
()
.1
,
direction
,
grid
,
energized
,
);
let
direction
=
Direction
::
Right
;
follow_beam
(
x
+
direction
.to_vec
()
.0
,
y
+
direction
.to_vec
()
.1
,
direction
,
grid
,
energized
,
);
}
Direction
::
Left
=>
follow_beam
(
x
+
direction
.to_vec
()
.0
,
y
+
direction
.to_vec
()
.1
,
direction
,
grid
,
energized
,
),
Direction
::
Right
=>
follow_beam
(
x
+
direction
.to_vec
()
.0
,
y
+
direction
.to_vec
()
.1
,
direction
,
grid
,
energized
,
),
},
'|'
=>
match
direction
{
Direction
::
Up
=>
follow_beam
(
x
+
direction
.to_vec
()
.0
,
y
+
direction
.to_vec
()
.1
,
direction
,
grid
,
energized
,
),
Direction
::
Down
=>
follow_beam
(
x
+
direction
.to_vec
()
.0
,
y
+
direction
.to_vec
()
.1
,
direction
,
grid
,
energized
,
),
Direction
::
Left
=>
{
let
direction
=
Direction
::
Up
;
follow_beam
(
x
+
direction
.to_vec
()
.0
,
y
+
direction
.to_vec
()
.1
,
direction
,
grid
,
energized
,
);
let
direction
=
Direction
::
Down
;
follow_beam
(
x
+
direction
.to_vec
()
.0
,
y
+
direction
.to_vec
()
.1
,
direction
,
grid
,
energized
,
);
}
Direction
::
Right
=>
{
let
direction
=
Direction
::
Up
;
follow_beam
(
x
+
direction
.to_vec
()
.0
,
y
+
direction
.to_vec
()
.1
,
direction
,
grid
,
energized
,
);
let
direction
=
Direction
::
Down
;
follow_beam
(
x
+
direction
.to_vec
()
.0
,
y
+
direction
.to_vec
()
.1
,
direction
,
grid
,
energized
,
);
}
},
_
=>
panic!
(),
}
}
#[derive(Debug,
Clone,
Copy,
PartialEq,
Eq)]
enum
Direction
{
Up
,
Down
,
Left
,
Right
,
}
impl
Direction
{
fn
to_vec
(
&
self
)
->
(
i32
,
i32
)
{
match
self
{
Direction
::
Up
=>
(
0
,
-
1
),
Direction
::
Down
=>
(
0
,
1
),
Direction
::
Left
=>
(
-
1
,
0
),
Direction
::
Right
=>
(
1
,
0
),
}
}
}
This diff is collapsed.
Click to expand it.
src/main.rs
+
4
−
1
View file @
abcfc3a1
...
...
@@ -7,8 +7,11 @@ mod day_12_2;
mod
day_13_1
;
mod
day_13_2
;
mod
day_14_1
;
mod
day_14_2
;
mod
day_15_1
;
mod
day_15_2
;
mod
day_16_1
;
mod
day_16_2
;
mod
day_1_1
;
mod
day_1_2
;
mod
day_2_1
;
...
...
@@ -35,7 +38,7 @@ use std::fs;
fn
main
()
{
let
input
=
fs
::
read_to_string
(
"input"
)
.expect
(
"Could not read input!"
);
let
solution
=
day_1
2
_2
::
solve_from_str
(
input
.trim_end
());
let
solution
=
day_1
6
_2
::
solve_from_str
(
input
.trim_end
());
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