add: day 10

This commit is contained in:
Charles
2024-12-10 21:03:22 -08:00
parent 05b61fffb9
commit f467526323
2 changed files with 45 additions and 11 deletions
+18
View File
@@ -0,0 +1,18 @@
import pygame
pygame.init()
screen = pygame.display.set_mode((570, 570))
with open("/home/charles/Downloads/input", 'r') as inputFile:
lines = [
[int(n) for n in line]
for line in inputFile.read().split('\n')
]
lines.pop()
for x in range(len(lines)):
for y in range(len(lines)):
val = lines[x][y]
screen.fill((0, 25 * val, 0), pygame.Rect(x * 10, y * 10, 10, 10))
pygame.display.flip()
pygame.image.save(screen, "day10vis.jpeg")
pygame.quit()
+27 -11
View File
@@ -26,29 +26,45 @@ fn solve(lines: Vec<String>) -> SResult<(usize, usize)> {
}
}
let mut total = 0;
let mut total = HashSet::new();
let mut total2 = HashSet::new();
for start in starts.into_iter() {
let mut visited = HashSet::new();
let mut stack = VecDeque::new();
stack.push_back(start);
// We track each location, and the paths we took to get there
let mut visited: HashMap<Pair, HashSet<Vec<Pair>>> = HashMap::default();
let mut stack: VecDeque<(Pair, Vec<Pair>)> = VecDeque::new();
stack.push_back((start, vec!()));
while stack.len() > 0 {
let cur = stack.pop_front().unwrap();
visited.insert(cur);
let (cur, path) = stack.pop_front().unwrap();
visited.entry(cur).or_insert_with(|| HashSet::new()).insert(path.clone());
if grid[cur.0][cur.1] == 9 {
total += 1;
total.insert((start, cur));
total2.insert((start, cur, path));
continue;
}
for vel in [(1, 0), (-1, 0), (0, 1), (0, -1)] {
if let Some(n) = next(cur, vel, width) {
if grid[n.0][n.1] == grid[cur.0][cur.1] + 1 && !visited.contains(&n) {
stack.push_front(n);
if grid[n.0][n.1] != grid[cur.0][cur.1] + 1 {
continue;
}
let mut new_path = path.clone();
new_path.push(n);
let mut should_visit = false;
if !visited.contains_key(&n) {
should_visit = true;
} else if !visited.get(&n).unwrap().contains(&new_path) {
should_visit = true;
}
if should_visit {
stack.push_back((n, new_path));
}
}
}
}
}
Ok((total, 0))
Ok((total.len(), total2.len()))
}
#[cfg(test)]
mod tests {
use advent_of_code_2024::input;
@@ -58,6 +74,6 @@ mod tests {
fn sample_input() {
let strings: Vec<String> = input!("d10p1.txt");
let got = solve(strings).unwrap();
assert_eq!(got, (36, 0));
assert_eq!(got, (36, 81));
}
}