Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 1e18537

Browse filesBrowse files
committed
Added day 20 Rust based solution
1 parent 386b6ad commit 1e18537
Copy full SHA for 1e18537

File tree

Expand file treeCollapse file tree

4 files changed

+131
-0
lines changed
Filter options
Expand file treeCollapse file tree

4 files changed

+131
-0
lines changed

‎2024/day20/Cargo.toml

Copy file name to clipboard
+11Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
[package]
2+
name = "day20"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
[dependencies]
7+
utils = { workspace = true }
8+
9+
[[bin]]
10+
name = "day20"
11+
path = "day20.rs"

‎2024/day20/day20.rs

Copy file name to clipboard
+91Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
use std::collections::HashSet;
2+
3+
use utils::{
4+
self,
5+
coordinates::{Direction, Point},
6+
};
7+
8+
fn get_track(map: Vec<&str>, size: i32) -> Vec<Point<i32>> {
9+
let mut walls = HashSet::<Point<i32>>::new();
10+
let mut end_pos = Point { x: 0, y: 0 };
11+
let mut track = vec![];
12+
13+
for (y, line) in (0..size).zip(map.iter().skip(1)) {
14+
for (x, tile) in (0..size).zip(line.bytes().skip(1)) {
15+
let point = Point {
16+
x: x as i32,
17+
y: y as i32,
18+
};
19+
20+
match tile {
21+
b'S' => {
22+
track.push(point);
23+
}
24+
b'E' => end_pos = point,
25+
b'#' => {
26+
walls.insert(point);
27+
}
28+
_ => {}
29+
};
30+
}
31+
}
32+
33+
while let Some(next_point) = Direction::CARDINAL
34+
.iter()
35+
.filter_map(|&dir| next_move(dir, *track.last().unwrap(), size))
36+
.find(|&next| !walls.contains(&next) && (track.len() < 2 || track[track.len() - 2] != next))
37+
{
38+
track.push(next_point);
39+
40+
if next_point == end_pos {
41+
break;
42+
}
43+
}
44+
45+
track
46+
}
47+
48+
fn next_move(direction: Point<i32>, start: Point<i32>, size: i32) -> Option<Point<i32>> {
49+
let next = start + direction;
50+
51+
match direction {
52+
Direction::UP if start.y > 0 => Some(next),
53+
Direction::RIGHT if start.x + 1 < size => Some(next),
54+
Direction::DOWN if start.y + 1 < size => Some(next),
55+
Direction::LEFT if start.x > 0 => Some(next),
56+
_ => None,
57+
}
58+
}
59+
60+
fn find_cheats(track: Vec<Point<i32>>, min_saving: usize, max_cheat_time: usize) -> usize {
61+
let mut res = 0;
62+
63+
for start in 0..track.len() {
64+
for end in (start + min_saving)..track.len() {
65+
let distance = track.get(start).unwrap().distance(track.get(end).unwrap());
66+
if distance <= max_cheat_time && distance <= (end - start - min_saving) as usize {
67+
res += 1;
68+
}
69+
}
70+
}
71+
72+
res
73+
}
74+
75+
fn part1(input: String) -> i32 {
76+
let size = input.lines().count() - 1;
77+
let track = get_track(input.lines().collect(), size as i32);
78+
79+
find_cheats(track, 100, 2) as i32
80+
}
81+
82+
fn part2(input: String) -> i32 {
83+
let size = input.lines().count() - 1;
84+
let track = get_track(input.lines().collect(), size as i32);
85+
86+
find_cheats(track, 100, 20) as i32
87+
}
88+
89+
fn main() {
90+
utils::run(20, vec!["input.txt"], &part1, &part2);
91+
}

‎2024/libs/utils/src/coordinates.rs

Copy file name to clipboardExpand all lines: 2024/libs/utils/src/coordinates.rs
+28Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,16 @@ pub struct Point<T> {
99
pub y: T,
1010
}
1111

12+
impl<T: Sub<Output = T>+Copy> Point<T>
13+
{
14+
pub fn distance(&self, other: &Point<T>) -> usize
15+
where
16+
T: Into<i64>,
17+
{
18+
((self.x - other.x).into().abs() + (self.y - other.y).into().abs()).try_into().unwrap()
19+
}
20+
}
21+
1222
impl<T: Add<Output = T> + Copy> Add for Point<T> {
1323
type Output = Point<T>;
1424
fn add(self, rhs: Point<T>) -> Point<T> {
@@ -99,4 +109,22 @@ impl Direction {
99109
pub const DOWN_LEFT: Point<i32> = Point { y: 1, x: -1 };
100110
pub const LEFT: Point<i32> = Point { y: 0, x: -1 };
101111
pub const NONE: Point<i32> = Point { y: 0, x: 0 };
112+
113+
pub const ALL: [Point<i32>; 8] = [
114+
self::Direction::UP_LEFT,
115+
self::Direction::UP,
116+
self::Direction::UP_RIGHT,
117+
self::Direction::RIGHT,
118+
self::Direction::DOWN_RIGHT,
119+
self::Direction::DOWN,
120+
self::Direction::DOWN_LEFT,
121+
self::Direction::LEFT,
122+
];
123+
124+
pub const CARDINAL: [Point<i32>; 4] = [
125+
self::Direction::UP,
126+
self::Direction::RIGHT,
127+
self::Direction::DOWN,
128+
self::Direction::LEFT,
129+
];
102130
}

‎readme.md

Copy file name to clipboardExpand all lines: readme.md
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ Para esta edição, planejo usar três linguagens de programação: Rust, C e Ja
2929
| Dia 17 | [Rust](./2024/day17/day17.rs) |
3030
| Dia 18 | [Rust](./2024/day18/day18.rs) |
3131
| Dia 19 | [Rust](./2024/day19/day19.rs) |
32+
| Dia 20 | [Rust](./2024/day20/day20.rs) |
3233

3334
### 2023
3435

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.