Not working for some reason
This commit is contained in:
		
							
								
								
									
										55
									
								
								day_09/src/lib.rs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										55
									
								
								day_09/src/lib.rs
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,55 @@ | ||||
| use nom::{ | ||||
|     character::complete, | ||||
|     multi::separated_list1, | ||||
|     IResult, | ||||
| }; | ||||
| use ndarray::prelude::*; | ||||
|  | ||||
| fn parse_line(input: &str) -> IResult<&str, Vec<i64>> { | ||||
|         separated_list1(complete::space1, complete::i64)(input) | ||||
| } | ||||
|  | ||||
| fn expand(sequence: Vec<i64>) -> Array2<i64> { | ||||
|     let mut reduced = Array::zeros((sequence.len()+1, sequence.len()+1)); | ||||
|     reduced.slice_mut(s!(0,0..sequence.len())).assign(&Array::from_vec(sequence.clone())); | ||||
|     for i in 1..sequence.len()-1 { | ||||
|         for j in 1..i { | ||||
|             reduced[[j,i+1-j]] = reduced[[0,3]] - reduced[[0,2]]; | ||||
|             reduced[[j,i+1-j]] = reduced[[j-1,i+2-j]] - reduced[[j-1,i+1-j]]; | ||||
|         } | ||||
|         reduced[[i,0]] = reduced[[i-1,1]] - reduced[[i-1,0]]; | ||||
|         reduced[[i,1]] = reduced[[i-1,2]] - reduced[[i-1,1]]; | ||||
|     } | ||||
|     reduced | ||||
| } | ||||
|  | ||||
| fn extrapolate(mut reduced: Array2<i64>) -> i64 { | ||||
|     let first_i = reduced.outer_iter().position(|row| {row.sum() == 0}).unwrap() - 1; | ||||
|     let mut j = reduced.slice(s!(first_i,..)).iter().position(|value| {value == &0}).unwrap(); | ||||
|     for i in (0..=first_i).rev() { | ||||
|         reduced[[i,j]] = reduced[[i, j-1]] + reduced[[i+1, j-1]]; | ||||
|         j += 1; | ||||
|     } | ||||
|     reduced[[0, reduced.shape()[1]-1]] | ||||
| } | ||||
|  | ||||
| pub fn part1(input: &str) -> String { | ||||
|     input.lines().map(|line| { | ||||
|         parse_line(line).unwrap().1 | ||||
|     }).map(|x| { | ||||
|         dbg!(&x); | ||||
|         let reduced = expand(x); | ||||
|         dbg!(extrapolate(reduced.clone())); | ||||
|         extrapolate(reduced) | ||||
|     }).sum::<i64>().to_string() | ||||
| } | ||||
|  | ||||
| pub fn part2(input: &str) -> String { | ||||
|     todo!() | ||||
| } | ||||
|  | ||||
| pub mod prelude { | ||||
|     pub use super::part1; | ||||
|     pub use super::part2; | ||||
| } | ||||
|  | ||||
							
								
								
									
										28
									
								
								day_09/src/main.rs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										28
									
								
								day_09/src/main.rs
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,28 @@ | ||||
| use day_09::prelude::*; | ||||
|  | ||||
| fn main() { | ||||
|     let input = include_str!("../input.txt"); | ||||
|     println!("{}", part1(input)); | ||||
|     println!("{}", part2(input)); | ||||
| } | ||||
|  | ||||
| #[cfg(test)] | ||||
| mod tests { | ||||
|     use super::*; | ||||
|  | ||||
|     #[test] | ||||
|     fn test_part1() { | ||||
|         let test_input = "0 3 6 9 12 15 | ||||
| 1 3 6 10 15 21 | ||||
| 10 13 16 21 30 45"; | ||||
|         assert_eq!(part1(test_input), "114".to_string()); | ||||
|         panic!(); | ||||
|     } | ||||
|  | ||||
|     #[test] | ||||
|     fn test_part2() { | ||||
|         let test_input = ""; | ||||
|         assert_eq!(part2(test_input), "".to_string()); | ||||
|     } | ||||
|  | ||||
| } | ||||
		Reference in New Issue
	
	Block a user
	 Connor Johnstone
					Connor Johnstone