use std::collections::HashMap; pub fn part_two(input_lines: Vec) -> String { let mut full_line: String = String::new(); for line in input_lines { full_line.push_str(&line); } let init_sequences = full_line.split(',').collect::>(); let mut boxes: HashMap> = HashMap::new(); for i in 0..256 { boxes.insert(i,Vec::new()); } for seq in init_sequences { let parsed_seq = parse_sequence(seq); let hash = hash_sequence(parsed_seq[0].clone()); println!("{}: {:?}", hash, parsed_seq.clone()); if parsed_seq[1] == "=" { let mut lenses = boxes.get(&hash).unwrap().clone(); let mut found: bool = false; for i in 0..lenses.len() { let mut lens = lenses[i].clone(); if lens.0 == parsed_seq[0] { found = true; lens.1 = parsed_seq[2].parse().unwrap(); lenses[i] = lens; boxes.insert(hash, lenses.clone()); break; } } if !found { lenses.push((parsed_seq[0].to_string(), parsed_seq[2].parse().unwrap())); boxes.insert(hash, lenses.clone()); } } else if parsed_seq[1] == "-" { let mut lenses = boxes.get(&hash).unwrap().clone(); for i in 0..lenses.len() { let lens = lenses[i].clone(); if lens.0 == parsed_seq[0] { lenses.remove(i); boxes.insert(hash, lenses.clone()); break; } } } } println!(); for (k, v) in &boxes { if v.len() > 0 { println!("{}: {:?}", k, v); } } String::from("Not implemented") } fn hash_sequence(sequence: String) -> usize { let mut hash = 0; for c in sequence.chars() { hash += c as usize; hash *= 17; hash %= 256; } hash } fn parse_sequence(sequence: &str) -> Vec { let mut res: Vec = Vec::new(); let split_sequence: Vec<&str> = sequence.split(['=','-']).collect(); res.push(split_sequence[0].to_string()); if sequence.contains('-') { res.push('-'.to_string()); } else if sequence.contains('=') { res.push('='.to_string()); } if split_sequence.len() > 1 { res.push(split_sequence[1].to_string()); } res }