Advent of Code solutions
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

day 7

Rain e009aa81 1835897e

+286
+7
2025/rust/7/Cargo.lock
··· 1 + # This file is automatically @generated by Cargo. 2 + # It is not intended for manual editing. 3 + version = 4 4 + 5 + [[package]] 6 + name = "day7" 7 + version = "0.1.0"
+6
2025/rust/7/Cargo.toml
··· 1 + [package] 2 + name = "day7" 3 + version = "0.1.0" 4 + edition = "2024" 5 + 6 + [dependencies]
+142
2025/rust/7/input.txt
··· 1 + ......................................................................S...................................................................... 2 + ............................................................................................................................................. 3 + ......................................................................^...................................................................... 4 + ............................................................................................................................................. 5 + .....................................................................^.^..................................................................... 6 + ............................................................................................................................................. 7 + ....................................................................^.^.^.................................................................... 8 + ............................................................................................................................................. 9 + ...................................................................^...^.^................................................................... 10 + ............................................................................................................................................. 11 + ..................................................................^.^...^.^.................................................................. 12 + ............................................................................................................................................. 13 + .................................................................^...^.^.^.^................................................................. 14 + ............................................................................................................................................. 15 + ................................................................^.^.........^................................................................ 16 + ............................................................................................................................................. 17 + ...............................................................^.^...^.^.^.^.^............................................................... 18 + ............................................................................................................................................. 19 + ..............................................................^.^.^.^.^.^.....^.............................................................. 20 + ............................................................................................................................................. 21 + .............................................................^.^.....^...^.^.^.^............................................................. 22 + ............................................................................................................................................. 23 + ............................................................^...^.....^.^...^.^.^............................................................ 24 + ............................................................................................................................................. 25 + ...........................................................^.....^.^.^.^.^.^.^.^.^........................................................... 26 + ............................................................................................................................................. 27 + ..........................................................^...^...^.....^.^...^.^.^.......................................................... 28 + ............................................................................................................................................. 29 + .........................................................^.^.....^.^.....^.^.^.^.^.^......................................................... 30 + ............................................................................................................................................. 31 + ........................................................^...^.^.^.^...^.^.....^.....^........................................................ 32 + ............................................................................................................................................. 33 + .......................................................^.^.^.^.^.^.....^.....^.^.^.^.^....................................................... 34 + ............................................................................................................................................. 35 + ......................................................^.^.....^.^.^.....^.^...^...^.^.^...................................................... 36 + ............................................................................................................................................. 37 + .....................................................^.^.^.^.^.^.^.^.^.^.^.^.^.^.^.^.^.^..................................................... 38 + ............................................................................................................................................. 39 + ....................................................^.^.^...^.^.^.^.^.^.^.^...^...^.^.^.^.................................................... 40 + ............................................................................................................................................. 41 + ...................................................^.^...^.^.^.^.^.^...^.^.^.^.^...^...^.^................................................... 42 + ............................................................................................................................................. 43 + ..................................................^.^.^.^...^.^.^.^.^.^.^...^.^...^.^...^.^.................................................. 44 + ............................................................................................................................................. 45 + .................................................^.^.^.^...^.^.......^.^.^.^.....^.^.^.^.^.^................................................. 46 + ............................................................................................................................................. 47 + ................................................^...^.^.^.^.^.^.^.^.^.^.....^.^.^...^.^.^...^................................................ 48 + ............................................................................................................................................. 49 + ...............................................^.....^...^.^.^.^.^...^...^.^.^.^.^.^.^.^...^.^............................................... 50 + ............................................................................................................................................. 51 + ..............................................^...^.^...^.^...^.....^.^.^.^.^.^...^.^.^.......^.............................................. 52 + ............................................................................................................................................. 53 + .............................................^.^.^.^.^...^.^.^.^.^.^...^.....^.^.....^.^.^...^.^............................................. 54 + ............................................................................................................................................. 55 + ............................................^.^.^.^.^.^.^.^.^.^.^...^.^.....^...^.^...^.^.^.^...^............................................ 56 + ............................................................................................................................................. 57 + ...........................................^.^.....^.^.....^.^.^.^.^.^.^.^.^.^...^.^...^.^.^.....^........................................... 58 + ............................................................................................................................................. 59 + ..........................................^.^.^...^.....^.^.^.^.^...^.^.....^.^.^.^...^...^.^...^.^.......................................... 60 + ............................................................................................................................................. 61 + .........................................^.^...^.^.^.^.^.^.^.^.^.^.^.....^.....^.^.^.^.^.^.^.^.^.^.^......................................... 62 + ............................................................................................................................................. 63 + ........................................^.....^.^...^.^.^.^.....^.^.^.^...^...^.^.^.^.^...^.^...^.^.^........................................ 64 + ............................................................................................................................................. 65 + .......................................^.^.^.^.....^.^.^.......^.^.........^...^.^.^.^.....^.^.....^.^....................................... 66 + ............................................................................................................................................. 67 + ......................................^.^.....^.^.^...^.^...^.^.......^.^...^.^.^.^.^.^...^.....^.^...^...................................... 68 + ............................................................................................................................................. 69 + .....................................^.^.^...^.^.^.^.^.....^.^...^.....^.....^...^.^...^.......^.^.^.^.^..................................... 70 + ............................................................................................................................................. 71 + ....................................^.^.^...^...^.^.^...^.^...^.^.^.^.......^.....^.....^.^.......^.^.^.^.................................... 72 + ............................................................................................................................................. 73 + ...................................^.^.^.^.^...^...^.^.^.^.^.^.^.^.^.^.......^.^...^.^...^.^.^.^.^...^.^.^................................... 74 + ............................................................................................................................................. 75 + ..................................^...^.^.^.^...^.......^.^.^...^.^.......^.^.^.....^...^...^.^.^...^.^...^.................................. 76 + ............................................................................................................................................. 77 + .................................^...^.......^.^.^.....^.^...^.^...^.^.^.^.^.^.....^.^...^...^.^.^...^...^.^................................. 78 + ............................................................................................................................................. 79 + ................................^.^.^...^.^.^.^.^.^.^...^...^.^.......^.^...^.^.^...^.^...^.^.^.^.^...^...^.^................................ 80 + ............................................................................................................................................. 81 + ...............................^.^.....^.^.^...^.^.^.^...^.........^.^.^.^.........^.^.^...........^.^.^.^...^............................... 82 + ............................................................................................................................................. 83 + ..............................^.^...^.^...^...^...^...^.^.^.^.^.^...^.^.^...^.^.^...^.....^.^.^...^...^.^.^...^.............................. 84 + ............................................................................................................................................. 85 + .............................^.^.^.^.^.^.^.....^.^.^.^.^.^.....^...^.^.^.......^.^.^.^.....^.........^.^.^.....^............................. 86 + ............................................................................................................................................. 87 + ............................^...^...^.^.^...^...^.^.^.^.^.^.^.^.^.^.......^.^.......^.^.....^.^.^.^...^.^...^...^............................ 88 + ............................................................................................................................................. 89 + ...........................^.^.^...^.^.^.....^.....^.^.......^.^.^.....^.^.^...^.....^...^.^.^.^.....^.^.^.^.^.^.^........................... 90 + ............................................................................................................................................. 91 + ..........................^.....^.....^.^...^.^.^.^.^.^...^.^.^.^.^.^...^.^...^.^.^...^.^.^.....^.^.^...^.^...^...^.......................... 92 + ............................................................................................................................................. 93 + .........................^.^...^.^.^.^...^.^...^.^.........^.......^.^.^.^.^.^...^.^.^.^.^.^.^...^.^.^...^.^.^.^...^......................... 94 + ............................................................................................................................................. 95 + ........................^...^.^.^.^...^.^...^...^.^...^.^.^...^.^.^.^.^.^...^.^.^...^.......^.^.^.^.^...^.....^...^.^........................ 96 + ............................................................................................................................................. 97 + .......................^.^.^...^.^.^.....^.^...^.^.^...^...^.^.^...^.^.^...^.^.....^.^.^.^.^...^.^.........^.^.^.^.^.^....................... 98 + ............................................................................................................................................. 99 + ......................^.^.^.^...^.^.^.^.^.....^.^.^.....^.^.^.^.^...^.^.^.....^.^.....^...^.^.^.^.....^...........^.^.^...................... 100 + ............................................................................................................................................. 101 + .....................^.^.^.^.^.^...^.^.^.^.^.....^.^...^...^...^.^.^.^.^.....^.^.^.....^.^...^.......^.^.^.....^.^.^.^.^..................... 102 + ............................................................................................................................................. 103 + ....................^...^.^.......^.^.^.^.^.^.........^.^.^.^.......^.^...^.^.^.^.^.^.^.^.^...^.^.^.....^.^.^.^.^.^.^.^.^.................... 104 + ............................................................................................................................................. 105 + ...................^.^.......^.^...^.^.^.^.^.^...^.^...^.^...^.....^.^.^...^.^...^.^.^.^.^...^...^.^.....^.^...^.^.^.^.^.^................... 106 + ............................................................................................................................................. 107 + ..................^.^.........^.^.^...^.^...^...^.^.^.^.^.^.^.^.^.^.^.^...^.^.^.^.^.^.^.^.^.^.^...^.^.^.^.^...^.....^...^.^.................. 108 + ............................................................................................................................................. 109 + .................^.^.^...^.^.^.^.^.......^.^.....^.^...^.^.^...^.^.^...^.^.^.^.^...^.......^.^...^.^.^.........^.^...^.^.^.^................. 110 + ............................................................................................................................................. 111 + ................^.....^.^.^...^.^.^.^.^...^.^.^.^.^.^.^.^...^.^.^.^.^.^.^...^...^.^...^.^.^.^...^.^.^.^.^.^.....^...^.....^.^................ 112 + ............................................................................................................................................. 113 + ...............^.....^.^.^.^.^.^.^.^.^.^...^.^...^...^...^...^...^.^.^.^.........^.^.^.^.^...^.....^.^.......^...^...^.^.^.^.^............... 114 + ............................................................................................................................................. 115 + ..............^.^.^.^.^.^.^...^.^.^.^.^.^.^.^...^.^.^.^...^.^...^.^.^.^.^...^.^.^.^.^.^.^.^.....^.^...^.^.^.^.^...^.....^.....^.............. 116 + ............................................................................................................................................. 117 + .............^.^...^.^.....^.......^.....^.....^.^...^.^.^.^.....^.^.....^.....^.^...^.^.^.^.^...^.^.^.^.^.^.^.^.^.^.^.^.^.^.^.^............. 118 + ............................................................................................................................................. 119 + ............^.^...^.^.....^.^.^.^.^.^.^.^...^...^.^.^.^.^.^.^.^.^.^.^...^.....^.^...^...^.........^.^...^.^...^.^.^.^.^.^.^.^...^............ 120 + ............................................................................................................................................. 121 + ...........^...^.^.^...^.^...^...^.^...^...^.^.^.^...^.^.^...^.^.^.^.^...^.^.^.^.^.^.^.^.^.^.^.^.....^.^.^.^.^...^.^.^.....^.^.^.^........... 122 + ............................................................................................................................................. 123 + ..........^.^.........^.^...^.....^.^.^.^.^.^.^.^...^...^...^.^.^.^.^...^.^.^...^...^...^.^.^...^.^.^.^...^.^.^.^...^.....^...^...^.......... 124 + ............................................................................................................................................. 125 + .........^.^.^.^...^...^.^.^.^...^.^.^.^.^.^.....^...^.^.....^.^...^...^.^...^.^.^.^...^.^...^...^.^.^.^...^...^.....^.^.^...^...^.^......... 126 + ............................................................................................................................................. 127 + ........^...^.^.^.^...^.^.^.^.^.^.^...^.^.^.........^...^.^.^.^.....^.^...^.^.^.^.^.....^.^.^.^.^.^.^.^.^.^...^.^.^.....^.^.....^.^.^........ 128 + ............................................................................................................................................. 129 + .......^.......^.^.^.^...^.^...^.^...^.^.^.^.^.^.^.^.^...^.^.^...^...^...^.^.^...^.^.^...^.^.^.^.^.^.^...^.^.....^.^.^...^.^.^...^.^.^....... 130 + ............................................................................................................................................. 131 + ......^...^...^.^.....^.^.^.^.^.^.^...^.^.^.^...^.^.^...^.^.^.....^.^.^.....^.^.^.^.^...^.^.^.^.....^.^.^...^.^.^.....^.^.^.^.^.^...^.^...... 132 + ............................................................................................................................................. 133 + .....^.....^.^.^.^.^...^.........^.....^.......^.^.^...^.^.......^.^.^.^...^.^.^.^.^...^.^.^.^.^.^.^...^...^.^.^...^...^.^...^.^.....^.^..... 134 + ............................................................................................................................................. 135 + ....^.^.^.^...^.^.^.^.^.^.^...^.^.^.^.....^.^.^.....^.^.^.^...^.....^.^.....^...^.^...^.^.^.^.^.^.^.^.^.^.^.^.^.^...^.^.^...^.^.^...^...^.... 136 + ............................................................................................................................................. 137 + ...^...^.^.^.^.^.^...^.^.^.^.^.^.^.^.....^...^.^.^.^.^.^.....^.^.^.^.^.^...^.......^.^...^...^.^.^.^...^.^.^...^.^.^.^.^.^.^.^...^.^.^...^... 138 + ............................................................................................................................................. 139 + ..^.^.^.^...^.^.^.^.^...^.....^...^.^.^...^...^.^.^.^...^...^.^.^...^...........^.^.^.^.......^...^...^.^.^.^.^.^.^.^.^.^.^...^.^.^.^.^.^.^.. 140 + ............................................................................................................................................. 141 + .^.^.^.^.^.^.^.^.^.^.^.......^.^.^.^.^.^.^.^...^.^...^.^.^.^.^.^.^...^.^.^.^.^.^.^.^.^.^.....^.^.......^.^.^.^.^...^...^.^.^.^.......^.....^. 142 + .............................................................................................................................................
+131
2025/rust/7/src/main.rs
··· 1 + use std::collections::HashMap; 2 + 3 + fn part1(input: Vec<String>) -> usize { 4 + let mut beam_indexes: Vec<usize> = Vec::new(); 5 + let mut times_split = 0; 6 + 7 + for line in input { 8 + for (ci, c) in line.chars().enumerate() { 9 + match c { 10 + 'S' => beam_indexes.push(ci), 11 + '^' => { 12 + if let Some((idx, _)) = beam_indexes.iter().enumerate().find(|&(_, &i)| i == ci) 13 + { 14 + beam_indexes.remove(idx); 15 + times_split += 1; 16 + 17 + if !beam_indexes.contains(&(ci - 1)) { 18 + beam_indexes.push(ci - 1); 19 + } 20 + 21 + if !beam_indexes.contains(&(ci + 1)) { 22 + beam_indexes.push(ci + 1); 23 + } 24 + } 25 + } 26 + _ => continue, 27 + } 28 + } 29 + } 30 + 31 + times_split 32 + } 33 + 34 + fn get_count( 35 + lines: Vec<String>, 36 + line_idx: usize, 37 + char_idx: usize, 38 + seen: &mut HashMap<(usize, usize), usize>, 39 + ) -> usize { 40 + for i in line_idx.. { 41 + if i == lines.len() - 1 { 42 + return 1; 43 + } 44 + 45 + if lines.get(i).unwrap().chars().nth(char_idx) == Some('^') { 46 + // we'll check if we've hit this splitter before, if so we'll just return what we counted last time 47 + // if not we'll calculate it and store it incase we hit it again 48 + // this basically zeroes the time it takes to run, it's important! 49 + if let Some(&entry) = seen.get(&(i, char_idx)) { 50 + return entry; 51 + } else { 52 + let count = get_count(lines.clone(), i + 1, char_idx - 1, seen) 53 + + get_count(lines, i + 1, char_idx + 1, seen); 54 + seen.insert((i, char_idx), count); 55 + return count; 56 + } 57 + } 58 + } 59 + unreachable!(); 60 + } 61 + 62 + fn part2(input: Vec<String>) -> usize { 63 + let start = input 64 + .first() 65 + .unwrap() 66 + .chars() 67 + .enumerate() 68 + .find(|&(_, c)| c == 'S') 69 + .unwrap() 70 + .0; 71 + 72 + get_count(input, 1, start, &mut HashMap::new()) 73 + } 74 + 75 + fn main() { 76 + let file_contents = include_str!("../input.txt"); 77 + let parsed: Vec<String> = file_contents.lines().map(str::to_string).collect(); 78 + 79 + println!("Part 1 Results: {}", part1(parsed.clone())); 80 + println!("Part 2 Results: {}", part2(parsed)); 81 + } 82 + 83 + mod tests { 84 + #[test] 85 + fn part1() { 86 + let input = r#".......S....... 87 + ............... 88 + .......^....... 89 + ............... 90 + ......^.^...... 91 + ............... 92 + .....^.^.^..... 93 + ............... 94 + ....^.^...^.... 95 + ............... 96 + ...^.^...^.^... 97 + ............... 98 + ..^...^.....^.. 99 + ............... 100 + .^.^.^.^.^...^. 101 + ..............."#; 102 + 103 + let parsed = input.lines().map(str::to_string).collect::<Vec<String>>(); 104 + 105 + assert_eq!(super::part1(parsed), 21); 106 + } 107 + 108 + #[test] 109 + fn part2() { 110 + let input = r#".......S....... 111 + ............... 112 + .......^....... 113 + ............... 114 + ......^.^...... 115 + ............... 116 + .....^.^.^..... 117 + ............... 118 + ....^.^...^.... 119 + ............... 120 + ...^.^...^.^... 121 + ............... 122 + ..^...^.....^.. 123 + ............... 124 + .^.^.^.^.^...^. 125 + ..............."#; 126 + 127 + let parsed = input.lines().map(str::to_string).collect::<Vec<String>>(); 128 + 129 + assert_eq!(super::part2(parsed), 40); 130 + } 131 + }