Day 6
This commit is contained in:
		
							parent
							
								
									05c89e5f11
								
							
						
					
					
						commit
						8b702bca9f
					
				
					 2 changed files with 44 additions and 0 deletions
				
			
		
							
								
								
									
										43
									
								
								src/day6.rs
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										43
									
								
								src/day6.rs
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,43 @@ | |||
| use aoc_runner_derive::{aoc, aoc_generator}; | ||||
| use std::collections::HashSet; | ||||
| #[aoc_generator(day6)] | ||||
| pub fn input_generator(input: &str) -> Vec<Vec<HashSet<char>>> { | ||||
|     input | ||||
|         .split("\n\n") | ||||
|         .map(|block| { | ||||
|             block | ||||
|                 .split("\n") | ||||
|                 .map(|line| line.chars().collect()) | ||||
|                 .collect() | ||||
|         }) | ||||
|         .collect() | ||||
| } | ||||
| 
 | ||||
| #[aoc(day6, part1)] | ||||
| fn solve_part1(data: &Vec<Vec<HashSet<char>>>) -> usize { | ||||
|     let mut total = 0; | ||||
|     for group in data { | ||||
|         let mut answers: HashSet<char> = HashSet::new(); | ||||
|         for person in group { | ||||
|             answers.extend(person); | ||||
|         } | ||||
|         total += answers.len(); | ||||
|     } | ||||
|     return total; | ||||
| } | ||||
| 
 | ||||
| #[aoc(day6, part2)] | ||||
| fn solve_part2(data: &Vec<Vec<HashSet<char>>>) -> usize { | ||||
|     let mut total = 0; | ||||
|     for group in data { | ||||
|         let mut answers: HashSet<char> = HashSet::new(); | ||||
|         for person in group { | ||||
|             answers.extend(person); | ||||
|         } | ||||
|         for person in group { | ||||
|             answers = answers.intersection(&person).map(|v| *v).collect(); | ||||
|         } | ||||
|         total += answers.len(); | ||||
|     } | ||||
|     return total; | ||||
| } | ||||
|  | @ -4,4 +4,5 @@ pub mod day2; | |||
| pub mod day3; | ||||
| pub mod day4; | ||||
| pub mod day5; | ||||
| pub mod day6; | ||||
| aoc_lib! { year = 2020 } | ||||
|  |  | |||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue