diff --git a/apollo_viz.py b/apollo_viz.py new file mode 100644 index 0000000..fc9838b --- /dev/null +++ b/apollo_viz.py @@ -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() diff --git a/src/bin/d10p1.rs b/src/bin/d10p1.rs index 2b72d5c..2bd224d 100644 --- a/src/bin/d10p1.rs +++ b/src/bin/d10p1.rs @@ -26,29 +26,45 @@ fn solve(lines: Vec) -> 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>> = HashMap::default(); + let mut stack: VecDeque<(Pair, Vec)> = 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 = input!("d10p1.txt"); let got = solve(strings).unwrap(); - assert_eq!(got, (36, 0)); + assert_eq!(got, (36, 81)); } }