diff --git a/Cargo.toml b/Cargo.toml index 3a86177..19d7abf 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -34,8 +34,13 @@ name = "bench_aoc_7" harness = false [dependencies] +bstr = "1.12.1" [profile.bench] opt-level = 3 lto = "fat" # "Link Time Optimization" - Critical for micro-benchmarks codegen-units = 1 # Slows compile time, but makes faster code panic = "abort" # Removes stack unwinding checks (faster) + +[profile.release] +debug = true +overflow-checks = true diff --git a/benches/bench_aoc_7.rs b/benches/bench_aoc_7.rs index 840148a..f582e34 100644 --- a/benches/bench_aoc_7.rs +++ b/benches/bench_aoc_7.rs @@ -7,11 +7,16 @@ fn bench_aoc_7_part1(c: &mut Criterion) { c.bench_function("bench_aoc7 part 1", |b| b.iter(|| aoc_7::solve_p1(input))); } +fn bench_aoc_7_part2(c: &mut Criterion) { + let input = black_box(include_str!("../src/aoc/input/full_input_aoc7.txt")); + c.bench_function("bench_aoc7 part 2", |b| b.iter(|| aoc_7::solve_p2(input))); +} criterion_group!( benches_p7, - bench_aoc_7_part1 + bench_aoc_7_part1, + bench_aoc_7_part2, ); criterion_main!(benches_p7); \ No newline at end of file diff --git a/src/aoc/aoc_7.rs b/src/aoc/aoc_7.rs index 2f5cd52..fb4ac55 100644 --- a/src/aoc/aoc_7.rs +++ b/src/aoc/aoc_7.rs @@ -39,33 +39,43 @@ pub fn solve_p1(input: &str) -> u64 { answer } -pub fn solve_p2(input: &str) -> u128 { +pub fn solve_p2(input: &str) -> u64 { let mut answer = 0; - let mut data = Vec::new(); + let mut data = Vec::with_capacity(256); for line in input.lines() { - let chars = line.chars().collect::>(); + let chars = line.bytes().map(|mut x| {if x == b'.' {x = 0; x as u64} else {x as u64}}).collect::>(); data.push(chars); } let start_idx = data[0].iter().len() / 2 ; - data[1][start_idx] = '|'; - - answer = traverse(&(1, start_idx), &data); + data[0][start_idx] = 1; + for (i,line) in data.clone().iter().enumerate() { + //println!("{:?}", data[i]); + for (j,char) in line.iter().enumerate() { + //println!("{}", data[i][j]); + if data[i][j] == 94 { + //println!("{}", data[i][j]); + if data[i-1][j] != 0 { + //println!("{}", data[i-1][j]); + let power = data[i-1][j]; + data[i][j-1] += power; + data[i][j+1] += power; + data[i+1][j-1] = data[i][j-1]; + data[i+1][j+1] = data[i][j+1]; + + } + } + else if data[i][j] != 0 { + //println!("{}", data[i][j]); + if i+1 < data.len() { + if data[i+1][j] != 94 { + data[i+1][j] = data[i][j]; + } + } + } + } + answer = data[data.len()-1].iter().sum(); + } answer } -fn traverse(idx : &(usize, usize), data_in: &Vec>) -> u128 { - let mut count = 0; - if idx.0 +1 >= data_in.len() -6 { - 1 - }else { - if data_in[idx.0 + 1][idx.1] == '^' { - count += traverse(&(idx.0+2, idx.1 -1), &data_in); - count += traverse(&(idx.0+2, idx.1 +1), &data_in); - return count; - } - count += traverse(&(idx.0+1, idx.1), &data_in); - count - } - -} \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index bcefe3e..f4326c2 100644 --- a/src/main.rs +++ b/src/main.rs @@ -19,6 +19,8 @@ fn main() { let input = include_bytes!("aoc/input/full_input_aoc4.txt"); let answer = aoc_4::solve_p1(input); println!("{}", answer); + + */ /* let input = include_str!("aoc/input/full_input_aoc5.txt"); @@ -44,7 +46,7 @@ fn main() { let input = include_str!("aoc/input/full_input_aoc7.txt"); let answer = aoc_7::solve_p1(input); println!("answer: {}", answer); - let input = include_str!("aoc/input/test_input_aoc7.txt"); + let input = include_str!("aoc/input/full_input_aoc7.txt"); let answer = aoc_7::solve_p2(input); println!("answer: {}", answer); @@ -103,3 +105,32 @@ fn test_aoc_3_part2() { 172787336861064 ); } +#[test] +fn test_aoc_4_part1() { + assert_eq!( + aoc_4::solve_p1(include_bytes!("aoc/input/full_input_aoc4.txt")), + 1486 + ); +} +#[test] +fn test_aoc_4_part2() { + assert_eq!( + aoc_4::solve_p2_f(include_bytes!("aoc/input/full_input_aoc4.txt")), + 9024 + ); +} +#[test] +fn test_aoc_5_part1() { + assert_eq!( + aoc_5::solve_p1(include_str!("aoc/input/full_input_aoc5.txt")), + 865 + ); +} +#[test] +fn test_aoc_5_part2() { + assert_eq!( + aoc_5::solve_p2(include_str!("aoc/input/full_input_aoc5.txt")), + 352556672963116 + ); +} +