aoc_3
This commit is contained in:
parent
baa97512d6
commit
2e51059d51
@ -12,4 +12,9 @@ harness = false
|
||||
|
||||
[[bench]]
|
||||
name = "bench_aoc_2"
|
||||
harness = false
|
||||
harness = false
|
||||
[[bench]]
|
||||
name = "bench_aoc_3"
|
||||
harness = false
|
||||
[dependencies]
|
||||
strength_reduce = "0.2.4"
|
||||
@ -14,7 +14,14 @@ fn bench_aoc_1_part2(c: &mut Criterion) {
|
||||
|
||||
fn bench_aoc_1_part2_f(c: &mut Criterion) {
|
||||
let input = black_box(include_bytes!("../src/aoc/input/full_input_aoc1.txt"));
|
||||
c.bench_function("bench_aoc2 part fast", |b| b.iter(|| aoc_1::solve_2f(input)));
|
||||
c.bench_function("bench_aoc2 part fast", |b| {
|
||||
b.iter(|| aoc_1::solve_2f(input))
|
||||
});
|
||||
}
|
||||
criterion_group!(benches_p1, bench_aoc_1_part1, bench_aoc_1_part2, bench_aoc_1_part2_f);
|
||||
criterion_group!(
|
||||
benches_p1,
|
||||
bench_aoc_1_part1,
|
||||
bench_aoc_1_part2,
|
||||
bench_aoc_1_part2_f
|
||||
);
|
||||
criterion_main!(benches_p1);
|
||||
|
||||
@ -7,10 +7,29 @@ fn bench_aoc_2_part1(c: &mut Criterion) {
|
||||
c.bench_function("bench_aoc2 part 1", |b| b.iter(|| aoc_2::solve_p1(input)));
|
||||
}
|
||||
|
||||
fn bench_aoc_2_part1_f(c: &mut Criterion) {
|
||||
let input = black_box(include_str!("../src/aoc/input/full_input_aoc2.txt"));
|
||||
c.bench_function("bench_aoc2 part 1 fast", |b| {
|
||||
b.iter(|| aoc_2::solve_p1_unsafe(input))
|
||||
});
|
||||
}
|
||||
fn bench_aoc_2_part1_faster(c: &mut Criterion) {
|
||||
let input = black_box(include_bytes!("../src/aoc/input/full_input_aoc2.txt"));
|
||||
c.bench_function("bench_aoc2 part 1 faster", |b| {
|
||||
b.iter(|| aoc_2::solve_p1_bytes(input))
|
||||
});
|
||||
}
|
||||
|
||||
fn bench_aoc_2_part2(c: &mut Criterion) {
|
||||
let input = black_box(include_str!("../src/aoc/input/full_input_aoc2.txt"));
|
||||
c.bench_function("bench_aoc2 part 1", |b| b.iter(|| aoc_2::solve_p2(input)));
|
||||
}
|
||||
|
||||
criterion_group!(benches_p2, bench_aoc_2_part1, bench_aoc_2_part2);
|
||||
criterion_main!(benches_p2);
|
||||
criterion_group!(
|
||||
benches_p2,
|
||||
bench_aoc_2_part1,
|
||||
bench_aoc_2_part1_f,
|
||||
bench_aoc_2_part1_faster,
|
||||
bench_aoc_2_part2
|
||||
);
|
||||
criterion_main!(benches_p2);
|
||||
|
||||
35
benches/bench_aoc_3.rs
Normal file
35
benches/bench_aoc_3.rs
Normal file
@ -0,0 +1,35 @@
|
||||
use aoc2025::aoc::aoc_3;
|
||||
use criterion::{Criterion, criterion_group, criterion_main};
|
||||
use std::hint::black_box;
|
||||
|
||||
fn bench_aoc_3_part1(c: &mut Criterion) {
|
||||
let input = black_box(include_bytes!("../src/aoc/input/full_input_aoc3.txt"));
|
||||
c.bench_function("bench_aoc3 part 1", |b| b.iter(|| aoc_3::solve_p1(input)));
|
||||
}
|
||||
fn bench_aoc_3_part1_fast(c: &mut Criterion) {
|
||||
let input = black_box(include_bytes!("../src/aoc/input/full_input_aoc3.txt"));
|
||||
c.bench_function("bench_aoc3 part 1 fast", |b| {
|
||||
b.iter(|| aoc_3::solve_p1_fast(input))
|
||||
});
|
||||
}
|
||||
|
||||
fn bench_aoc_3_part2(c: &mut Criterion) {
|
||||
let input = black_box(include_bytes!("../src/aoc/input/full_input_aoc3.txt"));
|
||||
c.bench_function("bench_aoc3 part 2", |b| b.iter(|| aoc_3::solve_p2(input)));
|
||||
}
|
||||
|
||||
fn bench_aoc_3_part2_fast(c: &mut Criterion) {
|
||||
let input = black_box(include_bytes!("../src/aoc/input/full_input_aoc3.txt"));
|
||||
c.bench_function("bench_aoc3 part 2 fast", |b| {
|
||||
b.iter(|| aoc_3::solve_p2_fast(input))
|
||||
});
|
||||
}
|
||||
|
||||
criterion_group!(
|
||||
benches_p3,
|
||||
bench_aoc_3_part1,
|
||||
bench_aoc_3_part1_fast,
|
||||
bench_aoc_3_part2,
|
||||
bench_aoc_3_part2_fast
|
||||
);
|
||||
criterion_main!(benches_p3);
|
||||
@ -1,2 +1,3 @@
|
||||
pub mod aoc_1;
|
||||
pub mod aoc_2;
|
||||
pub mod aoc_3;
|
||||
|
||||
@ -1,7 +1,6 @@
|
||||
|
||||
pub fn solve_1(input :&'static[u8]) -> u64 {
|
||||
pub fn solve_1(input: &'static [u8]) -> u64 {
|
||||
let mut dial = 50;
|
||||
let mut answer =0;
|
||||
let mut answer = 0;
|
||||
|
||||
for line in input.split(|&b| b == b'\n') {
|
||||
let len = line.len();
|
||||
@ -15,7 +14,6 @@ pub fn solve_1(input :&'static[u8]) -> u64 {
|
||||
let ones = (line[2] & 0x0F) as i64;
|
||||
tens * 10 + ones
|
||||
} else {
|
||||
|
||||
let tens = (line[2] & 0x0F) as i64;
|
||||
let ones = (line[3] & 0x0F) as i64;
|
||||
tens * 10 + ones
|
||||
@ -23,63 +21,71 @@ pub fn solve_1(input :&'static[u8]) -> u64 {
|
||||
|
||||
if line[0] == b'R' {
|
||||
dial += val;
|
||||
}else {
|
||||
} else {
|
||||
dial += 100 - val;
|
||||
}
|
||||
if dial >= 100 {
|
||||
dial -= 100;
|
||||
}
|
||||
if dial == 0 { answer += 1 }
|
||||
if dial == 0 {
|
||||
answer += 1
|
||||
}
|
||||
}
|
||||
answer
|
||||
}
|
||||
|
||||
pub fn solve_1_f(input :&'static[u8]) -> u32 {
|
||||
pub fn solve_1_f(input: &'static [u8]) -> u32 {
|
||||
let mut dial = 50;
|
||||
let mut answer =0;
|
||||
|
||||
let mut answer = 0;
|
||||
|
||||
for line in input.split(|&b| b == b'\n') {
|
||||
let len = line.len();
|
||||
|
||||
let val =match len {
|
||||
let val = match len {
|
||||
2 => line[1] as i64 - 48,
|
||||
3 => 10 * line[1] as i64 + line[2] as i64 - 48 - 480,
|
||||
_ => {answer+=line[1] as u32 - 48; 10 * line[2] as i64 + line[ 3] as i64 - 48 - 480},
|
||||
_ => {
|
||||
answer += line[1] as u32 - 48;
|
||||
10 * line[2] as i64 + line[3] as i64 - 48 - 480
|
||||
}
|
||||
};
|
||||
if input[0] == b'R' {
|
||||
dial += val;
|
||||
}else {
|
||||
} else {
|
||||
dial += 100 - val;
|
||||
}
|
||||
if dial >= 100 {
|
||||
dial -= 100;
|
||||
}
|
||||
if dial == 0 { answer += 1 }
|
||||
if dial == 0 {
|
||||
answer += 1
|
||||
}
|
||||
}
|
||||
answer
|
||||
}
|
||||
|
||||
pub fn solve_2(input :&'static[u8]) -> u64 {
|
||||
pub fn solve_2(input: &'static [u8]) -> u64 {
|
||||
let mut dial = 50;
|
||||
let mut answer =0;
|
||||
let mut answer = 0;
|
||||
|
||||
let mut index = 0;
|
||||
let mut bytes = 0;
|
||||
while index < input.len() {
|
||||
if input[index] == b'\n' || index == input.len() - 1 {
|
||||
if index == input.len() - 1 {
|
||||
bytes +=1;
|
||||
bytes += 1;
|
||||
}
|
||||
let num =match bytes {
|
||||
2 => input[index -1] as i64 - 48,
|
||||
3 => 10 * input[index -2] as i64 + input[index -1] as i64 - 48 - 480,
|
||||
_ => {answer+=input[index-3] as u64 - 48; 10 * input[index -2] as i64 + input[index -1] as i64 - 48 - 480},
|
||||
let num = match bytes {
|
||||
2 => input[index - 1] as i64 - 48,
|
||||
3 => 10 * input[index - 2] as i64 + input[index - 1] as i64 - 48 - 480,
|
||||
_ => {
|
||||
answer += input[index - 3] as u64 - 48;
|
||||
10 * input[index - 2] as i64 + input[index - 1] as i64 - 48 - 480
|
||||
}
|
||||
};
|
||||
|
||||
match input[index - bytes] {
|
||||
b'L' => {
|
||||
|
||||
if num > dial {
|
||||
if dial != 0 {
|
||||
answer += 1;
|
||||
@ -95,12 +101,9 @@ pub fn solve_2(input :&'static[u8]) -> u64 {
|
||||
answer += 1;
|
||||
|
||||
dial = num + dial - 100;
|
||||
|
||||
}else {
|
||||
} else {
|
||||
dial = 0;
|
||||
}
|
||||
|
||||
|
||||
} else {
|
||||
dial = num + dial;
|
||||
}
|
||||
@ -110,31 +113,36 @@ pub fn solve_2(input :&'static[u8]) -> u64 {
|
||||
answer += 1;
|
||||
}
|
||||
bytes = 0;
|
||||
}else {
|
||||
bytes +=1;
|
||||
} else {
|
||||
bytes += 1;
|
||||
}
|
||||
index +=1;
|
||||
index += 1;
|
||||
}
|
||||
answer as u64
|
||||
}
|
||||
|
||||
pub fn solve_2f(input :&'static[u8]) -> u64 {
|
||||
pub fn solve_2f(input: &'static [u8]) -> u64 {
|
||||
let mut dial: i64 = 50;
|
||||
let mut answer = 0;
|
||||
|
||||
for line in input.split(|&b| b == b'\n') {
|
||||
let len = line.len();
|
||||
|
||||
let val =match len {
|
||||
let val = match len {
|
||||
2 => line[1] as i64 - 48,
|
||||
3 => 10 * line[1] as i64 + line[2] as i64 - 48 - 480,
|
||||
_ => {answer+=line[1] as u64 - 48; 10 * line[2] as i64 + line[ 3] as i64 - 48 - 480},
|
||||
_ => {
|
||||
answer += line[1] as u64 - 48;
|
||||
10 * line[2] as i64 + line[3] as i64 - 48 - 480
|
||||
}
|
||||
};
|
||||
|
||||
if line[0] == b'L' {
|
||||
let next_dial = dial - val;
|
||||
if next_dial < 0 {
|
||||
if dial != 0 { answer += 1; }
|
||||
if dial != 0 {
|
||||
answer += 1;
|
||||
}
|
||||
dial = next_dial + 100;
|
||||
} else {
|
||||
dial = next_dial;
|
||||
@ -143,7 +151,9 @@ pub fn solve_2f(input :&'static[u8]) -> u64 {
|
||||
// 'R' case
|
||||
let next_dial = dial + val;
|
||||
if next_dial >= 100 {
|
||||
if next_dial != 100 { answer += 1; }
|
||||
if next_dial != 100 {
|
||||
answer += 1;
|
||||
}
|
||||
dial = next_dial - 100;
|
||||
} else {
|
||||
dial = next_dial;
|
||||
@ -155,4 +165,4 @@ pub fn solve_2f(input :&'static[u8]) -> u64 {
|
||||
}
|
||||
}
|
||||
answer
|
||||
}
|
||||
}
|
||||
|
||||
131
src/aoc/aoc_3.rs
Normal file
131
src/aoc/aoc_3.rs
Normal file
@ -0,0 +1,131 @@
|
||||
pub fn solve_p1(input: &[u8]) -> u32 {
|
||||
let mut answer = 0;
|
||||
let mut i = 1;
|
||||
let len = input.len();
|
||||
|
||||
while i < len {
|
||||
let mut j = i;
|
||||
let mut num = input[j-1];
|
||||
let mut num2 = input[j];
|
||||
|
||||
while input[j+1] != 10 {
|
||||
|
||||
if input[j] > num {
|
||||
num = input[j];
|
||||
num2 = input[j + 1];
|
||||
} else if input[j] > num2 {
|
||||
num2 = input[j];
|
||||
}
|
||||
j +=1;
|
||||
if j+2 == len {
|
||||
break;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
if input[j] > num2 {
|
||||
num2 =input[j ];
|
||||
}
|
||||
answer += (((num - 48) * 10) + num2 - 48) as u32;
|
||||
|
||||
i = j + 3;
|
||||
}
|
||||
|
||||
answer
|
||||
}
|
||||
|
||||
pub fn solve_p1_fast(input: &[u8]) -> u32 {
|
||||
let mut answer = 0;
|
||||
let mut i = 1;
|
||||
let len = input.len();
|
||||
|
||||
let mut num = input[0];
|
||||
let mut num2 = input[1];
|
||||
while i < len {
|
||||
let mut j = i;
|
||||
num = input[j-1];
|
||||
num2 = input[j];
|
||||
|
||||
while input[j+1] != 10 {
|
||||
|
||||
if input[j] > num {
|
||||
num = input[j];
|
||||
num2 = input[j + 1];
|
||||
} else if input[j] > num2 {
|
||||
num2 = input[j];
|
||||
}
|
||||
j +=1;
|
||||
if j+2 == len {
|
||||
break;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
if input[j] > num2 {
|
||||
num2 =input[j ];
|
||||
}
|
||||
answer += (((num - 48) * 10) + num2 - 48) as u32;
|
||||
|
||||
i = j + 3;
|
||||
}
|
||||
|
||||
answer
|
||||
}
|
||||
|
||||
pub fn solve_p2(input: &[u8]) -> u64 {
|
||||
let mut answer = 0;
|
||||
|
||||
for line in input.split(|&x| x == 10) {
|
||||
let mut end = line.len() - 12;
|
||||
let total = 12;
|
||||
let mut start = 0;
|
||||
let mut val = [0u8; 12];
|
||||
for k in 0..total {
|
||||
let mut num = line[start];
|
||||
start += 1;
|
||||
for i in start..=end {
|
||||
if line[i] > num {
|
||||
num = line[i];
|
||||
start = i + 1;
|
||||
}
|
||||
}
|
||||
val[k] = num;
|
||||
end += 1;
|
||||
}
|
||||
let mut num = 0;
|
||||
for i in 0..=11 {
|
||||
num = num * 10 + (val[i] - 48) as u64;
|
||||
}
|
||||
answer += num;
|
||||
}
|
||||
answer
|
||||
}
|
||||
|
||||
pub fn solve_p2_fast(input: &[u8]) -> u64 {
|
||||
let mut answer = 0;
|
||||
|
||||
for line in input.split(|&x| x == 10) {
|
||||
let mut end = line.len() - 12;
|
||||
let total = 12;
|
||||
let mut start = 0;
|
||||
let mut val = [0u8; 12];
|
||||
for k in 0..total {
|
||||
let mut num = line[start];
|
||||
start += 1;
|
||||
for i in start..=end {
|
||||
if line[i] > num {
|
||||
num = line[i];
|
||||
start = i + 1;
|
||||
}
|
||||
}
|
||||
val[k] = num;
|
||||
end += 1;
|
||||
}
|
||||
let mut num = 0;
|
||||
for i in 0..=11 {
|
||||
num = num * 10 + (val[i] - 48) as u64;
|
||||
}
|
||||
answer += num;
|
||||
}
|
||||
answer
|
||||
}
|
||||
4
src/aoc/input/test_input_aoc3.txt
Normal file
4
src/aoc/input/test_input_aoc3.txt
Normal file
@ -0,0 +1,4 @@
|
||||
987654321111111
|
||||
811111111111119
|
||||
234234234234278
|
||||
818181911112111
|
||||
60
src/main.rs
60
src/main.rs
@ -1,35 +1,65 @@
|
||||
|
||||
|
||||
pub mod aoc;
|
||||
use crate::aoc::*;
|
||||
|
||||
fn main() {
|
||||
let input = include_str!("aoc/input/full_input_aoc2.txt");
|
||||
let answer = aoc_2::solve_p1(input);
|
||||
let input = include_bytes!("aoc/input/full_input_aoc3.txt");
|
||||
let answer = aoc_3::solve_p1(input);
|
||||
println!("{}", answer);
|
||||
let input2 = include_bytes!("aoc/input/full_input_aoc3.txt");
|
||||
let answer = aoc_3::solve_p1_fast(input2);
|
||||
println!("{}", answer);
|
||||
|
||||
let input = include_bytes!("aoc/input/test_input_aoc2.txt");
|
||||
//let answer = aoc_2::solve_p1_f(input);
|
||||
println!("{:?}", input);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_aoc_1_1() {
|
||||
assert_eq!(aoc_1::solve_1( include_bytes!("aoc/input/test_input_aoc1.txt")), 3);
|
||||
assert_eq!(aoc_1::solve_1( include_bytes!("aoc/input/full_input_aoc1.txt")), 1195);
|
||||
assert_eq!(
|
||||
aoc_1::solve_1(include_bytes!("aoc/input/test_input_aoc1.txt")),
|
||||
3
|
||||
);
|
||||
assert_eq!(
|
||||
aoc_1::solve_1(include_bytes!("aoc/input/full_input_aoc1.txt")),
|
||||
1195
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_aoc_1_2() {
|
||||
assert_eq!(aoc_1::solve_2f(include_bytes!("aoc/input/test_input_aoc1.txt")), 6);
|
||||
assert_eq!(aoc_1::solve_2f(include_bytes!("aoc/input/full_input_aoc1.txt")), 6770);
|
||||
assert_eq!(
|
||||
aoc_1::solve_2f(include_bytes!("aoc/input/test_input_aoc1.txt")),
|
||||
6
|
||||
);
|
||||
assert_eq!(
|
||||
aoc_1::solve_2f(include_bytes!("aoc/input/full_input_aoc1.txt")),
|
||||
6770
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_aoc_2_part1() {
|
||||
assert_eq!(aoc_2::solve_p1(include_str!("aoc/input/test_input_aoc2.txt")), 1227775554);
|
||||
assert_eq!(
|
||||
aoc_2::solve_p1(include_str!("aoc/input/test_input_aoc2.txt")),
|
||||
1227775554
|
||||
);
|
||||
}
|
||||
#[test]
|
||||
fn test_aoc_2_part2() {
|
||||
assert_eq!(aoc_2::solve_p2(include_str!("aoc/input/test_input_aoc2.txt")), 4174379265);
|
||||
}
|
||||
assert_eq!(
|
||||
aoc_2::solve_p2(include_str!("aoc/input/test_input_aoc2.txt")),
|
||||
4174379265
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_aoc_3_part1() {
|
||||
assert_eq!(
|
||||
aoc_3::solve_p1(include_bytes!("aoc/input/full_input_aoc3.txt")),
|
||||
17359
|
||||
);
|
||||
}
|
||||
#[test]
|
||||
fn test_aoc_3_part2() {
|
||||
assert_eq!(
|
||||
aoc_3::solve_p2_fast(include_bytes!("aoc/input/full_input_aoc3.txt")),
|
||||
172787336861064
|
||||
);
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user