My attempts to solve puzzles of Advent of Code
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

feat: day 6

+81 -1
+2
data/2023/day6.txt
··· 1 + Time: 40 81 77 72 2 + Distance: 219 1012 1365 1089
+1 -1
src/main.rs
··· 3 3 pub mod solutions; 4 4 5 5 fn main() { 6 - let sol = solve!(2023; 5; 2); 6 + let sol = solve!(2023; 6; 2); 7 7 println!("{:?}", sol); 8 8 }
+76
src/solutions/year_2023/day6.rs
··· 1 + use std::ops::Sub; 2 + use aoc_lib::{create_solution, Input, Solver}; 3 + 4 + pub struct Day6 {} 5 + 6 + fn solve_x(x: usize, y: usize) -> Vec<usize> { 7 + (1..x).fold(vec![], |mut acc, a| { 8 + if a * (x - a) > y { 9 + acc.push(a) 10 + } 11 + acc 12 + }) 13 + } 14 + 15 + fn solve_xs(inp: Vec<(usize, usize)>) -> Vec<usize> { 16 + inp.into_iter().map(|(x, y)| solve_x(x, y).len()).collect() 17 + } 18 + 19 + fn solve_eq(x: usize, y: usize) -> usize { 20 + let s = ((x as f64).powf(2.0) - (4.0 * y as f64)).abs().sqrt(); 21 + let root1: f64 = (x as f64 - s) / 2.0; 22 + let root2: f64 = (x as f64 + s) / 2.0; 23 + 24 + if root1.floor() == root1 { 25 + (root2 - (root1 + 1.0)) as usize + 1 26 + } else if root2.ceil() == root2 { 27 + ((root2 - 1.0) - root1) as usize + 1 28 + } else { 29 + (root2 - root1) as usize + 1 30 + } 31 + } 32 + 33 + fn solve_eqs(inp: Vec<(usize, usize)>) -> usize { 34 + inp.into_iter().map(|(x, y)| solve_eq(x, y)).product() 35 + } 36 + 37 + create_solution!( 38 + out_1 => usize; 39 + out_2 => usize; 40 + year => 2023; 41 + day => 6; 42 + sol_1 => |input: Input| { 43 + let inp = vec![ 44 + (40, 219), 45 + (81, 1012), 46 + (77, 1365), 47 + (72, 1089), 48 + ]; 49 + // Some(solve_xs(inp).iter().product()) 50 + Some(solve_eqs(inp)) 51 + }; 52 + sol_2 => |input: Input| { 53 + let inp = vec![ 54 + (40817772, 219101213651089), 55 + ]; 56 + // Some(solve_xs(inp).iter().product()) 57 + Some(solve_eqs(inp)) 58 + }; 59 + ); 60 + 61 + #[cfg(test)] 62 + mod tests { 63 + use super::*; 64 + 65 + #[test] 66 + fn test_solve_xs() { 67 + let inp = vec![ 68 + (7, 9), 69 + (15, 40), 70 + (30, 200), 71 + ]; 72 + 73 + let act: usize = solve_xs(inp).iter().product(); 74 + assert_eq!(act, 288) 75 + } 76 + }
+2
src/solutions/year_2023/mod.rs
··· 5 5 mod day4; 6 6 mod day13; 7 7 mod day5; 8 + mod day6; 8 9 9 10 pub use day1::*; 10 11 pub use day2::*; 11 12 pub use day3::*; 12 13 pub use day4::*; 13 14 pub use day5::*; 15 + pub use day6::*; 14 16 pub use day13::*;