aoc_2
This commit is contained in:
parent
b705f1ceec
commit
baa97512d6
@ -9,3 +9,7 @@ criterion = { version = "0.7", features = ["html_reports"] }
|
||||
[[bench]]
|
||||
name = "bench_aoc_1"
|
||||
harness = false
|
||||
|
||||
[[bench]]
|
||||
name = "bench_aoc_2"
|
||||
harness = false
|
||||
16
benches/bench_aoc_2.rs
Normal file
16
benches/bench_aoc_2.rs
Normal file
@ -0,0 +1,16 @@
|
||||
use aoc2025::aoc::aoc_2;
|
||||
use criterion::{Criterion, criterion_group, criterion_main};
|
||||
use std::hint::black_box;
|
||||
|
||||
fn bench_aoc_2_part1(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_p1(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);
|
||||
@ -1 +1,2 @@
|
||||
pub mod aoc_1;
|
||||
pub mod aoc_2;
|
||||
|
||||
@ -1,4 +1,3 @@
|
||||
use std::hint::unreachable_unchecked;
|
||||
|
||||
pub fn solve_1(input :&'static[u8]) -> u64 {
|
||||
let mut dial = 50;
|
||||
@ -39,7 +38,7 @@ pub fn solve_1_f(input :&'static[u8]) -> u32 {
|
||||
let mut dial = 50;
|
||||
let mut answer =0;
|
||||
|
||||
let mut index = 0;
|
||||
|
||||
for line in input.split(|&b| b == b'\n') {
|
||||
let len = line.len();
|
||||
|
||||
|
||||
39
src/aoc/aoc_2.rs
Normal file
39
src/aoc/aoc_2.rs
Normal file
@ -0,0 +1,39 @@
|
||||
use std::io::BufRead;
|
||||
|
||||
pub fn solve_p1(input: &str ) -> u64 {
|
||||
let mut answer = 0;
|
||||
for line in input.split(',') {
|
||||
let (num1, num2) = line.split_once('-').map(|n| {(n.0.parse::<u64>().unwrap(), n.1.parse::<u64>().unwrap())}).unwrap();
|
||||
for i in num1..=num2 {
|
||||
let text = i.to_string();
|
||||
if text.len()%2 == 0 {
|
||||
let splits = text.split_at(text.len()/2);
|
||||
if splits.0==splits.1 {
|
||||
answer +=i;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
answer
|
||||
}
|
||||
|
||||
pub fn solve_p2(input: &str ) -> u64 {
|
||||
let mut answer = 0;
|
||||
for line in input.split(',') {
|
||||
let num_range = line.split('-').map(|num| { num.parse::<u64>().unwrap_or(0) }).collect::<Vec<u64>>();
|
||||
for i in num_range[0]..=num_range[1] {
|
||||
let text = i.to_string();
|
||||
for j in 1..=text.len() / 2 {
|
||||
if text.len() % j == 0 {
|
||||
if text[0..j].repeat(text.len() / j) == text {
|
||||
answer += i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
answer
|
||||
}
|
||||
1
src/aoc/input/test_input_aoc2.txt
Normal file
1
src/aoc/input/test_input_aoc2.txt
Normal file
@ -0,0 +1 @@
|
||||
11-22,95-115,998-1012,1188511880-1188511890,222220-222224,1698522-1698528,446443-446449,38593856-38593862,565653-565659,824824821-824824827,2121212118-2121212124
|
||||
21
src/main.rs
21
src/main.rs
@ -1,10 +1,16 @@
|
||||
|
||||
|
||||
pub mod aoc;
|
||||
use crate::aoc::*;
|
||||
|
||||
fn main() {
|
||||
println!("answer: {}", aoc_1::solve_1_f( include_bytes!("aoc/input/full_input_aoc1.txt")));
|
||||
println!("answer: {}", aoc_1::solve_2(include_bytes!("aoc/input/full_input_aoc1.txt")));
|
||||
//println!("{:?}", include_bytes!("aoc/input/full_input_aoc1.txt"));
|
||||
let input = include_str!("aoc/input/full_input_aoc2.txt");
|
||||
let answer = aoc_2::solve_p1(input);
|
||||
println!("{}", answer);
|
||||
|
||||
let input = include_bytes!("aoc/input/test_input_aoc2.txt");
|
||||
//let answer = aoc_2::solve_p1_f(input);
|
||||
println!("{:?}", input);
|
||||
}
|
||||
|
||||
#[test]
|
||||
@ -18,3 +24,12 @@ 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);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_aoc_2_part1() {
|
||||
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);
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user