Trying to do advent of code in Rust. I am very new to rust so please help if you see me doing something stupid!!
1
fork

Configure Feed

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

part 1 done, part 2 very much in a conceptualization state

Signed-off-by: goose.art <business@goose.art>

goose.art 5e807a70 36b676e7

verified
+304
+7
day7/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
day7/Cargo.toml
··· 1 + [package] 2 + name = "day7" 3 + version = "0.1.0" 4 + edition = "2024" 5 + 6 + [dependencies]
+142
day7/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 + .............................................................................................................................................
+16
day7/smallinput.txt
··· 1 + .......S....... 2 + ............... 3 + .......^....... 4 + ............... 5 + ......^.^...... 6 + ............... 7 + .....^.^.^..... 8 + ............... 9 + ....^.^...^.... 10 + ............... 11 + ...^.^...^.^... 12 + ............... 13 + ..^...^.....^.. 14 + ............... 15 + .^.^.^.^.^...^. 16 + ...............
+133
day7/src/main.rs
··· 1 + use std::fmt::Debug; 2 + use std::fs; 3 + 4 + struct TachyonManifold { 5 + data: Vec<Vec<char>>, 6 + height: usize, 7 + width: usize, 8 + } 9 + 10 + struct TachyonBeam { 11 + start_row: usize, 12 + start_col: usize, 13 + end_row: usize, 14 + } 15 + 16 + impl TachyonManifold { 17 + fn new(input: String) -> Self { 18 + let data: Vec<Vec<char>> = input.lines().map(|line| line.chars().collect()).collect(); 19 + let height = data.len(); 20 + let width = data[0].len(); 21 + Self { 22 + data, 23 + height, 24 + width, 25 + } 26 + } 27 + 28 + fn propagate_beam(&mut self, row: usize) -> u32 { 29 + let mut split_count = 0u32; 30 + let row_data = self.data[row].clone(); 31 + 32 + // THIS ANNOYINGLY CONTAINS AN IMMUTABLE BORROW for self.data[row] 33 + // SO IT BREAKS ALL MY MUTABLE REFERENCES. 34 + // THIS IS WHY THERE IS ALL THE CLONE()ING 35 + for (index, c) in row_data.iter().enumerate() { 36 + match c { 37 + 'S' | '|' => { 38 + if self.data[row + 1][index] == '.' { 39 + self.data[row + 1][index] = '|' 40 + } 41 + } 42 + '^' => { 43 + if self.data[row.saturating_sub(1)][index] == '|' { 44 + self.data[row][index + 1] = '|'; 45 + self.data[row][index.saturating_sub(1)] = '|'; 46 + split_count += 1; 47 + } 48 + } 49 + _ => continue, 50 + } 51 + } 52 + 53 + // ANNOYINGLY HAVE TO RE-CLONE DATA BC CANT FIGURE OUT HOW TO EDIT IN PLACE 54 + let row_data = self.data[row].clone(); 55 + 56 + for (index, c) in row_data.iter().enumerate() { 57 + match c { 58 + '|' => { 59 + if self.data[row + 1][index] == '.' { 60 + self.data[row + 1][index] = '|' 61 + } 62 + } 63 + _ => continue, 64 + } 65 + } 66 + split_count 67 + } 68 + 69 + fn quantum_propagate(&mut self, row: usize) -> u32 { 70 + let mut split_count = 0u32; 71 + let row_data = self.data[row].clone(); 72 + 73 + for (index, c) in row_data.iter().enumerate() { 74 + match c { 75 + '|' => { 76 + if self.data[row + 1][index] == '.' { 77 + self.data[row + 1][index] = '|' 78 + } 79 + } 80 + _ => continue, 81 + } 82 + } 83 + split_count 84 + } 85 + } 86 + 87 + // Quantum propagation thoughts 88 + // looks like a tree structure 89 + // every time we find a split, could push a new Tachyon Manifold onto the stack 90 + // --- Wait okay its one beam 91 + // so really we just need to store a stack of L/R decisions that represent 92 + // a map of outcomes 93 + // problem is... how do we distinguish whether tachyons path was unique? 94 + // save every path taken as a a string & convert to a set? 95 + // or are they just guaranteed unique structurally 96 + 97 + impl Debug for TachyonManifold { 98 + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { 99 + writeln!( 100 + f, 101 + "TachyonManifold of size {}, {} {{", 102 + self.width, self.height 103 + )?; 104 + for row in &self.data { 105 + for &cell in row { 106 + write!(f, "{}", cell)?; 107 + } 108 + writeln!(f)?; 109 + } 110 + writeln!(f, "}}")?; 111 + Ok(()) 112 + } 113 + } 114 + 115 + fn main() { 116 + let mut manifold = TachyonManifold::new(fs::read_to_string("input.txt").unwrap()); 117 + 118 + let mut splits = 0u32; 119 + for row in 0..manifold.height - 1 { 120 + splits += manifold.propagate_beam(row); 121 + println!("{:?}", manifold); 122 + } 123 + 124 + // Weird observation: for the small example provided 125 + // answer for total number of paths == the number of beams 126 + let beam_count = manifold.data[2..] 127 + .iter() 128 + .map(|row| row.iter().filter(|&cell| *cell == '|').count()) 129 + .sum::<usize>(); 130 + 131 + println!("Beam split {} times.", splits); 132 + println!("Total possible paths: {}", beam_count / 2); 133 + }