switched to workspaces.
This should let me make a cross-day library.
This commit is contained in:
		
							parent
							
								
									1469c3a32b
								
							
						
					
					
						commit
						242989bb95
					
				
					 57 changed files with 156 additions and 89 deletions
				
			
		
							
								
								
									
										10
									
								
								days/day04/Cargo.toml
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										10
									
								
								days/day04/Cargo.toml
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,10 @@
 | 
			
		|||
[package]
 | 
			
		||||
name = "day04"
 | 
			
		||||
authors.workspace = true
 | 
			
		||||
description.workspace = true
 | 
			
		||||
version.workspace = true
 | 
			
		||||
edition.workspace = true
 | 
			
		||||
 | 
			
		||||
[dependencies]
 | 
			
		||||
once_cell.workspace = true
 | 
			
		||||
regex.workspace = true
 | 
			
		||||
							
								
								
									
										1000
									
								
								days/day04/src/input.txt
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										1000
									
								
								days/day04/src/input.txt
									
										
									
									
									
										Normal file
									
								
							
										
											
												File diff suppressed because it is too large
												Load diff
											
										
									
								
							
							
								
								
									
										14
									
								
								days/day04/src/main.rs
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										14
									
								
								days/day04/src/main.rs
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,14 @@
 | 
			
		|||
mod part1;
 | 
			
		||||
mod part2;
 | 
			
		||||
mod utilities;
 | 
			
		||||
 | 
			
		||||
fn main() {
 | 
			
		||||
    let _input = include_str!("./input.txt");
 | 
			
		||||
    let _structured_input = utilities::parse(_input);
 | 
			
		||||
 | 
			
		||||
    println!("Part One");
 | 
			
		||||
    println!("Result: {}", part1::part1(&_structured_input));
 | 
			
		||||
 | 
			
		||||
    println!("Part Two");
 | 
			
		||||
    println!("Result: {}", part2::part2(&_structured_input));
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										27
									
								
								days/day04/src/part1.rs
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										27
									
								
								days/day04/src/part1.rs
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,27 @@
 | 
			
		|||
use crate::utilities::*;
 | 
			
		||||
 | 
			
		||||
pub fn part1(input: &[(Range, Range)]) -> usize {
 | 
			
		||||
    input
 | 
			
		||||
        .iter()
 | 
			
		||||
        .filter(|tuple| tuple.0.complete_overlap(&tuple.1))
 | 
			
		||||
        .count()
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#[cfg(test)]
 | 
			
		||||
mod tests {
 | 
			
		||||
    use super::*;
 | 
			
		||||
 | 
			
		||||
    #[test]
 | 
			
		||||
    fn test_part1() {
 | 
			
		||||
        let input = vec![
 | 
			
		||||
            (Range::new(2, 4), Range::new(6, 8)),
 | 
			
		||||
            (Range::new(2, 3), Range::new(4, 5)),
 | 
			
		||||
            (Range::new(5, 7), Range::new(7, 9)),
 | 
			
		||||
            (Range::new(2, 8), Range::new(3, 7)),
 | 
			
		||||
            (Range::new(6, 6), Range::new(4, 6)),
 | 
			
		||||
            (Range::new(2, 6), Range::new(4, 8)),
 | 
			
		||||
            (Range::new(19, 30), Range::new(5, 18)),
 | 
			
		||||
        ];
 | 
			
		||||
        assert_eq!(part1(&input), 2);
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										27
									
								
								days/day04/src/part2.rs
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										27
									
								
								days/day04/src/part2.rs
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,27 @@
 | 
			
		|||
use crate::utilities::*;
 | 
			
		||||
 | 
			
		||||
pub fn part2(input: &[(Range, Range)]) -> usize {
 | 
			
		||||
    input
 | 
			
		||||
        .iter()
 | 
			
		||||
        .filter(|tuple| tuple.0.any_overlap(&tuple.1))
 | 
			
		||||
        .count()
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#[cfg(test)]
 | 
			
		||||
mod tests {
 | 
			
		||||
    use super::*;
 | 
			
		||||
 | 
			
		||||
    #[test]
 | 
			
		||||
    fn test_part2() {
 | 
			
		||||
        let input = vec![
 | 
			
		||||
            (Range::new(2, 4), Range::new(6, 8)),
 | 
			
		||||
            (Range::new(2, 3), Range::new(4, 5)),
 | 
			
		||||
            (Range::new(5, 7), Range::new(7, 9)),
 | 
			
		||||
            (Range::new(2, 8), Range::new(3, 7)),
 | 
			
		||||
            (Range::new(6, 6), Range::new(4, 6)),
 | 
			
		||||
            (Range::new(2, 6), Range::new(4, 8)),
 | 
			
		||||
            (Range::new(19, 30), Range::new(5, 18)),
 | 
			
		||||
        ];
 | 
			
		||||
        assert_eq!(part2(&input), 4);
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										79
									
								
								days/day04/src/utilities.rs
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										79
									
								
								days/day04/src/utilities.rs
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,79 @@
 | 
			
		|||
use once_cell::sync::Lazy;
 | 
			
		||||
use regex::Regex;
 | 
			
		||||
#[derive(Debug, PartialEq, Eq)]
 | 
			
		||||
pub struct Range {
 | 
			
		||||
    start: u16,
 | 
			
		||||
    end: u16,
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
impl Range {
 | 
			
		||||
    pub fn new(start: u16, end: u16) -> Self {
 | 
			
		||||
        Self {
 | 
			
		||||
            start: start.min(end),
 | 
			
		||||
            end: end.max(start),
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    pub fn any_overlap(&self, other: &Self) -> bool {
 | 
			
		||||
        self.start <= other.end && self.end >= other.start
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    pub fn calc_overlap(&self, other: &Self) -> Range {
 | 
			
		||||
        let overlap_start = self.start.min(other.start);
 | 
			
		||||
        let overlap_end = self.end.max(other.end);
 | 
			
		||||
        Range::new(overlap_start, overlap_end)
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    pub fn complete_overlap(&self, other: &Self) -> bool {
 | 
			
		||||
        self.calc_overlap(other) == *self || self.calc_overlap(other) == *other
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static PARSE_REGEX: Lazy<Regex> = Lazy::new(|| Regex::new(r"^(\d+)-(\d+),(\d+)-(\d+)").unwrap());
 | 
			
		||||
 | 
			
		||||
pub fn parse(input: &str) -> Vec<(Range, Range)> {
 | 
			
		||||
    input
 | 
			
		||||
        .lines()
 | 
			
		||||
        .map(|line| {
 | 
			
		||||
            let cap = PARSE_REGEX.captures(line).unwrap();
 | 
			
		||||
            (
 | 
			
		||||
                Range::new(
 | 
			
		||||
                    cap.get(1).unwrap().as_str().parse().unwrap(),
 | 
			
		||||
                    cap.get(2).unwrap().as_str().parse().unwrap(),
 | 
			
		||||
                ),
 | 
			
		||||
                Range::new(
 | 
			
		||||
                    cap.get(3).unwrap().as_str().parse().unwrap(),
 | 
			
		||||
                    cap.get(4).unwrap().as_str().parse().unwrap(),
 | 
			
		||||
                ),
 | 
			
		||||
            )
 | 
			
		||||
        })
 | 
			
		||||
        .collect()
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#[cfg(test)]
 | 
			
		||||
mod tests {
 | 
			
		||||
    use super::*;
 | 
			
		||||
 | 
			
		||||
    #[test]
 | 
			
		||||
    fn test_parse() {
 | 
			
		||||
        let input = "2-4,6-8
 | 
			
		||||
2-3,4-5
 | 
			
		||||
5-7,7-9
 | 
			
		||||
2-8,3-7
 | 
			
		||||
6-6,4-6
 | 
			
		||||
2-6,4-8
 | 
			
		||||
19-30,5-18";
 | 
			
		||||
        assert_eq!(
 | 
			
		||||
            parse(input),
 | 
			
		||||
            vec![
 | 
			
		||||
                (Range::new(2, 4), Range::new(6, 8)),
 | 
			
		||||
                (Range::new(2, 3), Range::new(4, 5)),
 | 
			
		||||
                (Range::new(5, 7), Range::new(7, 9)),
 | 
			
		||||
                (Range::new(2, 8), Range::new(3, 7)),
 | 
			
		||||
                (Range::new(6, 6), Range::new(4, 6)),
 | 
			
		||||
                (Range::new(2, 6), Range::new(4, 8)),
 | 
			
		||||
                (Range::new(19, 30), Range::new(5, 18)),
 | 
			
		||||
            ]
 | 
			
		||||
        );
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue