Skip to content

Commit

Permalink
Updates and cleaning
Browse files Browse the repository at this point in the history
  • Loading branch information
Mishco committed Dec 13, 2024
1 parent c1aea0e commit 6217eec
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 37 deletions.
30 changes: 15 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,21 @@ Solutions for [Advent of Code](https://adventofcode.com/2024) in [Rust](https://

| Day | Part 1 | Part 2 |
| :---: | :---: | :---: |
| [Day 1](./src/bin/01.rs) | `67.1µs` | `86.5µs` |
| [Day 2](./src/bin/02.rs) | `122.0µs` | `236.9µs` |
| [Day 3](./src/bin/03.rs) | `256.4µs` | `294.9µs` |
| [Day 4](./src/bin/04.rs) | `257.6µs` | `81.1µs` |
| [Day 5](./src/bin/05.rs) | `186.0µs` | `259.8µs` |
| [Day 6](./src/bin/06.rs) | `155.6µs` | `177.2ms` |
| [Day 7](./src/bin/07.rs) | `1.1ms` | `331.9ms` |
| [Day 8](./src/bin/08.rs) | `70.5µs` | `52.7µs` |
| [Day 9](./src/bin/09.rs) | `110.8µs` | `536.0ms` |
| [Day 10](./src/bin/10.rs) | `142.9µs` | `81.9µs` |
| [Day 11](./src/bin/11.rs) | `131.1µs` | `5.2ms` |
| [Day 12](./src/bin/12.rs) | `3.4ms` | `5.1ms` |
| [Day 13](./src/bin/13.rs) | `44.0µs` | `39.5µs` |

**Total: 1062.58ms**
| [Day 1](./src/bin/01.rs) | `65.5µs` | `85.5µs` |
| [Day 2](./src/bin/02.rs) | `873.1µs` | `965.8µs` |
| [Day 3](./src/bin/03.rs) | `269.4µs` | `286.9µs` |
| [Day 4](./src/bin/04.rs) | `289.3µs` | `89.9µs` |
| [Day 5](./src/bin/05.rs) | `184.0µs` | `275.0µs` |
| [Day 6](./src/bin/06.rs) | `162.6µs` | `183.5ms` |
| [Day 7](./src/bin/07.rs) | `1.1ms` | `335.8ms` |
| [Day 8](./src/bin/08.rs) | `71.4µs` | `53.6µs` |
| [Day 9](./src/bin/09.rs) | `118.0µs` | `541.1ms` |
| [Day 10](./src/bin/10.rs) | `196.6µs` | `85.6µs` |
| [Day 11](./src/bin/11.rs) | `132.3µs` | `5.3ms` |
| [Day 12](./src/bin/12.rs) | `3.5ms` | `5.1ms` |
| [Day 13](./src/bin/13.rs) | `45.1µs` | `39.6µs` |

**Total: 1079.69ms**
<!--- benchmarking table --->


Expand Down
16 changes: 8 additions & 8 deletions src/bin/06.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ fn part1(mtx: &Mtx) -> Option<usize> {
while let Some('#') = get(mtx, r + dr, c + dc, r_max, c_max) {
(dr, dc) = turn_right((dr, dc));
}
if let None = get(mtx, r + dr, c + dc, r_max, c_max) {
if get(mtx, r + dr, c + dc, r_max, c_max).is_none() {
break;
}
(r, c) = (r + dr, c + dc);
Expand All @@ -62,9 +62,12 @@ fn obstacle_loops(mtx: &mut Mtx, r: i32, c: i32, dr: i32, dc: i32, r_max: i32, c
while let Some('#') = get(mtx, r + dr, c + dc, r_max, c_max) {
(dr, dc) = turn_right((dr, dc));
}
if let None = get(mtx, r + dr, c + dc, r_max, c_max) {
if get(mtx, r + dr, c + dc, r_max, c_max).is_none() {
break;
}
// if let None = get(mtx, r + dr, c + dc, r_max, c_max) {
// break;
// }
(r, c) = (r + dr, c + dc);
}
}
Expand All @@ -86,7 +89,7 @@ fn part2(mtx: &mut Mtx) -> Option<usize> {
while let Some('#') = get(mtx, r + dr, c + dc, r_max, c_max) {
(dr, dc) = turn_right((dr, dc));
}
if let None = get(mtx, r + dr, c + dc, r_max, c_max) {
if get(mtx, r + dr, c + dc, r_max, c_max).is_none() {
break;
}
(r, c) = (r + dr, c + dc);
Expand All @@ -110,18 +113,15 @@ pub fn part_one(input: &str) -> Option<usize> {
.lines()
.map(|line| line.chars().collect::<Vec<_>>())
.collect::<Mtx>();
let result = part1(&input);
result
part1(&input)
}

pub fn part_two(input: &str) -> Option<usize> {
let mut input = input
.lines()
.map(|line| line.chars().collect::<Vec<_>>())
.collect::<Mtx>();
let result = part2(&mut input);
// println!("Part2: {:?}", result);
result
part2(&mut input)
}

#[cfg(test)]
Expand Down
20 changes: 8 additions & 12 deletions src/bin/10.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@ advent_of_code::solution!(10);

use std::collections::HashMap;

const DIRECTIONS: [(isize, isize); 4] = [(0, -1), (-1, 0), (1, 0), (0, 1)]; // Left, Up, Down, Right
// Left, Up, Down, Right
// let directions = [(0, -1), (-1, 0), (1, 0), (0, 1)];
const DIRECTIONS: [(isize, isize); 4] = [(0, -1), (-1, 0), (1, 0), (0, 1)];

pub fn part_one_another(input: &str) -> Option<u32> {
let matrix: Vec<Vec<u32>> = input
Expand Down Expand Up @@ -39,8 +37,7 @@ pub fn part_one_another(input: &str) -> Option<u32> {
let mut stack: Vec<(usize, usize)> = vec![];
stack.push((x, y));
let mut existing: HashMap<(usize, usize), bool> = HashMap::new();
while stack.len() > 0 {
let (cur_x, cur_y) = stack.pop().unwrap();
while let Some((cur_x, cur_y)) = stack.pop() {
let cur_val = matrix[cur_x][cur_y];
if cur_val == 9 {
existing.insert((cur_x, cur_y), true);
Expand Down Expand Up @@ -99,13 +96,11 @@ pub fn part_two(input: &str) -> Option<usize> {
})
.collect();


for (x, y) in trailheads {
let mut stack: Vec<(usize, usize)> = vec![];
stack.push((x, y));

while stack.len() > 0 {
let (current_x, current_y) = stack.pop().unwrap();
while let Some((current_x, current_y)) = stack.pop() {
let value = matrix[current_x][current_y];
if value == 9 {
result += 1;
Expand All @@ -116,10 +111,11 @@ pub fn part_two(input: &str) -> Option<usize> {
let new_x: usize = (current_x as isize + dx) as usize;
let new_y: usize = (current_y as isize + dy) as usize;

if new_x < matrix.len() && new_y < matrix[0].len() {
if matrix[new_x][new_y] == value + 1 {
stack.push((new_x, new_y));
}
if new_x < matrix.len()
&& new_y < matrix[0].len()
&& matrix[new_x][new_y] == value + 1
{
stack.push((new_x, new_y));
}
}
}
Expand Down
6 changes: 4 additions & 2 deletions src/bin/13.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ pub fn part_one(input: &str) -> Option<usize> {
.split(|c: char| !c.is_ascii_digit())
.filter(|w| !w.is_empty())
.map(|w| w.parse().unwrap())
.collect_tuple() {
.collect_tuple()
{
Some(x) => x,
None => continue,
};
Expand All @@ -38,7 +39,8 @@ pub fn part_two(input: &str) -> Option<usize> {
.split(|c: char| !c.is_ascii_digit())
.filter(|w| !w.is_empty())
.map(|w| w.parse().unwrap())
.collect_tuple( ) {
.collect_tuple()
{
Some(x) => x,
None => continue,
};
Expand Down

0 comments on commit 6217eec

Please sign in to comment.