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

day 11, failing

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

goose.art 7e8f6ed0 c66586a7

verified
+265
+186
day11/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 = "anstream" 7 + version = "0.6.21" 8 + source = "registry+https://github.com/rust-lang/crates.io-index" 9 + checksum = "43d5b281e737544384e969a5ccad3f1cdd24b48086a0fc1b2a5262a26b8f4f4a" 10 + dependencies = [ 11 + "anstyle", 12 + "anstyle-parse", 13 + "anstyle-query", 14 + "anstyle-wincon", 15 + "colorchoice", 16 + "is_terminal_polyfill", 17 + "utf8parse", 18 + ] 19 + 20 + [[package]] 21 + name = "anstyle" 22 + version = "1.0.13" 23 + source = "registry+https://github.com/rust-lang/crates.io-index" 24 + checksum = "5192cca8006f1fd4f7237516f40fa183bb07f8fbdfedaa0036de5ea9b0b45e78" 25 + 26 + [[package]] 27 + name = "anstyle-parse" 28 + version = "0.2.7" 29 + source = "registry+https://github.com/rust-lang/crates.io-index" 30 + checksum = "4e7644824f0aa2c7b9384579234ef10eb7efb6a0deb83f9630a49594dd9c15c2" 31 + dependencies = [ 32 + "utf8parse", 33 + ] 34 + 35 + [[package]] 36 + name = "anstyle-query" 37 + version = "1.1.5" 38 + source = "registry+https://github.com/rust-lang/crates.io-index" 39 + checksum = "40c48f72fd53cd289104fc64099abca73db4166ad86ea0b4341abe65af83dadc" 40 + dependencies = [ 41 + "windows-sys", 42 + ] 43 + 44 + [[package]] 45 + name = "anstyle-wincon" 46 + version = "3.0.11" 47 + source = "registry+https://github.com/rust-lang/crates.io-index" 48 + checksum = "291e6a250ff86cd4a820112fb8898808a366d8f9f58ce16d1f538353ad55747d" 49 + dependencies = [ 50 + "anstyle", 51 + "once_cell_polyfill", 52 + "windows-sys", 53 + ] 54 + 55 + [[package]] 56 + name = "clap" 57 + version = "4.5.53" 58 + source = "registry+https://github.com/rust-lang/crates.io-index" 59 + checksum = "c9e340e012a1bf4935f5282ed1436d1489548e8f72308207ea5df0e23d2d03f8" 60 + dependencies = [ 61 + "clap_builder", 62 + "clap_derive", 63 + ] 64 + 65 + [[package]] 66 + name = "clap_builder" 67 + version = "4.5.53" 68 + source = "registry+https://github.com/rust-lang/crates.io-index" 69 + checksum = "d76b5d13eaa18c901fd2f7fca939fefe3a0727a953561fefdf3b2922b8569d00" 70 + dependencies = [ 71 + "anstream", 72 + "anstyle", 73 + "clap_lex", 74 + "strsim", 75 + ] 76 + 77 + [[package]] 78 + name = "clap_derive" 79 + version = "4.5.49" 80 + source = "registry+https://github.com/rust-lang/crates.io-index" 81 + checksum = "2a0b5487afeab2deb2ff4e03a807ad1a03ac532ff5a2cee5d86884440c7f7671" 82 + dependencies = [ 83 + "heck", 84 + "proc-macro2", 85 + "quote", 86 + "syn", 87 + ] 88 + 89 + [[package]] 90 + name = "clap_lex" 91 + version = "0.7.6" 92 + source = "registry+https://github.com/rust-lang/crates.io-index" 93 + checksum = "a1d728cc89cf3aee9ff92b05e62b19ee65a02b5702cff7d5a377e32c6ae29d8d" 94 + 95 + [[package]] 96 + name = "colorchoice" 97 + version = "1.0.4" 98 + source = "registry+https://github.com/rust-lang/crates.io-index" 99 + checksum = "b05b61dc5112cbb17e4b6cd61790d9845d13888356391624cbe7e41efeac1e75" 100 + 101 + [[package]] 102 + name = "day11" 103 + version = "0.1.0" 104 + dependencies = [ 105 + "clap", 106 + ] 107 + 108 + [[package]] 109 + name = "heck" 110 + version = "0.5.0" 111 + source = "registry+https://github.com/rust-lang/crates.io-index" 112 + checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea" 113 + 114 + [[package]] 115 + name = "is_terminal_polyfill" 116 + version = "1.70.2" 117 + source = "registry+https://github.com/rust-lang/crates.io-index" 118 + checksum = "a6cb138bb79a146c1bd460005623e142ef0181e3d0219cb493e02f7d08a35695" 119 + 120 + [[package]] 121 + name = "once_cell_polyfill" 122 + version = "1.70.2" 123 + source = "registry+https://github.com/rust-lang/crates.io-index" 124 + checksum = "384b8ab6d37215f3c5301a95a4accb5d64aa607f1fcb26a11b5303878451b4fe" 125 + 126 + [[package]] 127 + name = "proc-macro2" 128 + version = "1.0.103" 129 + source = "registry+https://github.com/rust-lang/crates.io-index" 130 + checksum = "5ee95bc4ef87b8d5ba32e8b7714ccc834865276eab0aed5c9958d00ec45f49e8" 131 + dependencies = [ 132 + "unicode-ident", 133 + ] 134 + 135 + [[package]] 136 + name = "quote" 137 + version = "1.0.42" 138 + source = "registry+https://github.com/rust-lang/crates.io-index" 139 + checksum = "a338cc41d27e6cc6dce6cefc13a0729dfbb81c262b1f519331575dd80ef3067f" 140 + dependencies = [ 141 + "proc-macro2", 142 + ] 143 + 144 + [[package]] 145 + name = "strsim" 146 + version = "0.11.1" 147 + source = "registry+https://github.com/rust-lang/crates.io-index" 148 + checksum = "7da8b5736845d9f2fcb837ea5d9e2628564b3b043a70948a3f0b778838c5fb4f" 149 + 150 + [[package]] 151 + name = "syn" 152 + version = "2.0.111" 153 + source = "registry+https://github.com/rust-lang/crates.io-index" 154 + checksum = "390cc9a294ab71bdb1aa2e99d13be9c753cd2d7bd6560c77118597410c4d2e87" 155 + dependencies = [ 156 + "proc-macro2", 157 + "quote", 158 + "unicode-ident", 159 + ] 160 + 161 + [[package]] 162 + name = "unicode-ident" 163 + version = "1.0.22" 164 + source = "registry+https://github.com/rust-lang/crates.io-index" 165 + checksum = "9312f7c4f6ff9069b165498234ce8be658059c6728633667c526e27dc2cf1df5" 166 + 167 + [[package]] 168 + name = "utf8parse" 169 + version = "0.2.2" 170 + source = "registry+https://github.com/rust-lang/crates.io-index" 171 + checksum = "06abde3611657adf66d383f00b093d7faecc7fa57071cce2578660c9f1010821" 172 + 173 + [[package]] 174 + name = "windows-link" 175 + version = "0.2.1" 176 + source = "registry+https://github.com/rust-lang/crates.io-index" 177 + checksum = "f0805222e57f7521d6a62e36fa9163bc891acd422f971defe97d64e70d0a4fe5" 178 + 179 + [[package]] 180 + name = "windows-sys" 181 + version = "0.61.2" 182 + source = "registry+https://github.com/rust-lang/crates.io-index" 183 + checksum = "ae137229bcbd6cdf0f7b80a31df61766145077ddf49416a728b02cb3921ff3fc" 184 + dependencies = [ 185 + "windows-link", 186 + ]
+7
day11/Cargo.toml
··· 1 + [package] 2 + name = "day11" 3 + version = "0.1.0" 4 + edition = "2024" 5 + 6 + [dependencies] 7 + clap = { version = "4.5.53", features = ["derive"] }
+10
day11/smallinput.txt
··· 1 + aaa: you hhh 2 + you: bbb ccc 3 + bbb: ddd eee 4 + ccc: ddd eee fff 5 + ddd: ggg 6 + eee: out 7 + fff: out 8 + ggg: out 9 + hhh: ccc fff iii 10 + iii: out
+42
day11/src/main.rs
··· 1 + mod server; 2 + use clap::Parser; 3 + use server::Server; 4 + 5 + #[derive(Parser)] 6 + struct Args { 7 + #[clap(short, long)] 8 + file: String, 9 + #[clap(short, long)] 10 + debug: bool, 11 + } 12 + 13 + fn main() { 14 + let args = Args::parse(); 15 + if args.debug { 16 + println!("=== Debug mode enabled ==="); 17 + } 18 + 19 + let mut nodes: Vec<Server> = Vec::new(); 20 + let file_contents = std::fs::read_to_string(&args.file).expect("Failed to read file"); 21 + 22 + for line in file_contents.lines() { 23 + let tokens = line.split_whitespace().collect::<Vec<_>>(); 24 + let source = tokens[0]; 25 + let destinations = &tokens[1..]; 26 + println!("Source: {}, Destinations: {:?}", source, destinations); 27 + let mut server = Server::new(&source[..source.len() - 1]); 28 + let source_exists = nodes.iter().any(|s| s.label == source); 29 + for next_label in destinations { 30 + let server_exists = nodes.iter().any(|s| s.label == **next_label); 31 + 32 + if !server_exists { 33 + let next_server = Server::new(next_label); 34 + nodes.push(next_server); 35 + } 36 + 37 + let index = nodes.iter().position(|s| s.label == **next_label).unwrap(); 38 + server.add_next(index); 39 + } 40 + } 41 + println!("{:?}", nodes); 42 + }
+20
day11/src/server.rs
··· 1 + use std::fmt::Debug; 2 + 3 + #[derive(Debug, Eq, PartialEq, Hash)] 4 + pub struct Server { 5 + pub label: String, 6 + next: Vec<usize>, // Store indices instead of references 7 + } 8 + 9 + impl Server { 10 + pub fn new(label: &str) -> Self { 11 + Server { 12 + label: label.to_string(), 13 + next: Vec::new(), 14 + } 15 + } 16 + 17 + pub fn add_next(&mut self, next_index: usize) { 18 + self.next.push(next_index); 19 + } 20 + }