Compare commits
9 Commits
498680aaca
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
| 4b44cfba67 | |||
| f22f67161b | |||
| 6465a1597a | |||
| b0bde44ff4 | |||
| fd853da84e | |||
| fad32426b9 | |||
| eb4d08968b | |||
| 00f434a9c1 | |||
| 6b350ded94 |
21
Cargo.toml
21
Cargo.toml
@@ -21,9 +21,30 @@ harness = false
|
|||||||
[[bench]]
|
[[bench]]
|
||||||
name = "bench_aoc_4"
|
name = "bench_aoc_4"
|
||||||
harness = false
|
harness = false
|
||||||
|
[[bench]]
|
||||||
|
name = "bench_aoc_5"
|
||||||
|
harness = false
|
||||||
|
|
||||||
|
[[bench]]
|
||||||
|
name = "bench_aoc_6"
|
||||||
|
harness = false
|
||||||
|
|
||||||
|
[[bench]]
|
||||||
|
name = "bench_aoc_7"
|
||||||
|
harness = false
|
||||||
|
|
||||||
|
[[bench]]
|
||||||
|
name = "bench_aoc_8"
|
||||||
|
harness = false
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
|
bstr = "1.12.1"
|
||||||
[profile.bench]
|
[profile.bench]
|
||||||
opt-level = 3
|
opt-level = 3
|
||||||
lto = "fat" # "Link Time Optimization" - Critical for micro-benchmarks
|
lto = "fat" # "Link Time Optimization" - Critical for micro-benchmarks
|
||||||
codegen-units = 1 # Slows compile time, but makes faster code
|
codegen-units = 1 # Slows compile time, but makes faster code
|
||||||
panic = "abort" # Removes stack unwinding checks (faster)
|
panic = "abort" # Removes stack unwinding checks (faster)
|
||||||
|
|
||||||
|
[profile.release]
|
||||||
|
debug = true
|
||||||
|
overflow-checks = true
|
||||||
|
|||||||
@@ -16,7 +16,7 @@ fn bench_aoc_4_part2(c: &mut Criterion) {
|
|||||||
}
|
}
|
||||||
fn bench_aoc_4_part2_f(c: &mut Criterion) {
|
fn bench_aoc_4_part2_f(c: &mut Criterion) {
|
||||||
let input = black_box(include_bytes!("../src/aoc/input/full_input_aoc4.txt"));
|
let input = black_box(include_bytes!("../src/aoc/input/full_input_aoc4.txt"));
|
||||||
c.bench_function("bench_aoc4 part 2", |b| b.iter(|| aoc_4::solve_p2_f(input)));
|
c.bench_function("bench_aoc4 part 2 fast", |b| b.iter(|| aoc_4::solve_p2_f(input)));
|
||||||
}
|
}
|
||||||
|
|
||||||
criterion_group!(
|
criterion_group!(
|
||||||
|
|||||||
20
benches/bench_aoc_5.rs
Normal file
20
benches/bench_aoc_5.rs
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
use std::hint::black_box;
|
||||||
|
use criterion::{criterion_group, criterion_main, Criterion};
|
||||||
|
use aoc2025::aoc::aoc_5;
|
||||||
|
|
||||||
|
fn bench_aoc_5_part1(c: &mut Criterion) {
|
||||||
|
let input = black_box(include_str!("../src/aoc/input/full_input_aoc5.txt"));
|
||||||
|
c.bench_function("bench_aoc5 part 1", |b| b.iter(|| aoc_5::solve_p1(input)));
|
||||||
|
}
|
||||||
|
|
||||||
|
fn bench_aoc_5_part2(c: &mut Criterion) {
|
||||||
|
let input = black_box(include_str!("../src/aoc/input/full_input_aoc5.txt"));
|
||||||
|
c.bench_function("bench_aoc5 part 2", |b| b.iter(|| aoc_5::solve_p2(input)));
|
||||||
|
}
|
||||||
|
|
||||||
|
criterion_group!(
|
||||||
|
benches_p5,
|
||||||
|
bench_aoc_5_part1,
|
||||||
|
bench_aoc_5_part2
|
||||||
|
);
|
||||||
|
criterion_main!(benches_p5);
|
||||||
22
benches/bench_aoc_6.rs
Normal file
22
benches/bench_aoc_6.rs
Normal file
@@ -0,0 +1,22 @@
|
|||||||
|
use std::hint::black_box;
|
||||||
|
use criterion::{criterion_group, criterion_main, Criterion};
|
||||||
|
use aoc2025::aoc::aoc_6;
|
||||||
|
|
||||||
|
fn bench_aoc_6_part1(c: &mut Criterion) {
|
||||||
|
let input = black_box(include_str!("../src/aoc/input/full_input_aoc6.txt"));
|
||||||
|
c.bench_function("bench_aoc6 part 1", |b| b.iter(|| aoc_6::solve_p1(input)));
|
||||||
|
}
|
||||||
|
|
||||||
|
fn bench_aoc_6_part2(c: &mut Criterion) {
|
||||||
|
let input = black_box(include_str!("../src/aoc/input/full_input_aoc6.txt"));
|
||||||
|
c.bench_function("bench_aoc6 part 2", |b| b.iter(|| aoc_6::solve_p2_f(input)));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
criterion_group!(
|
||||||
|
benches_p6,
|
||||||
|
bench_aoc_6_part1,
|
||||||
|
bench_aoc_6_part2,
|
||||||
|
);
|
||||||
|
criterion_main!(benches_p6);
|
||||||
22
benches/bench_aoc_7.rs
Normal file
22
benches/bench_aoc_7.rs
Normal file
@@ -0,0 +1,22 @@
|
|||||||
|
use std::hint::black_box;
|
||||||
|
use criterion::{criterion_group, criterion_main, Criterion};
|
||||||
|
use aoc2025::aoc::aoc_7;
|
||||||
|
|
||||||
|
fn bench_aoc_7_part1(c: &mut Criterion) {
|
||||||
|
let input = black_box(include_str!("../src/aoc/input/full_input_aoc7.txt"));
|
||||||
|
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_bytes!("../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_part2,
|
||||||
|
);
|
||||||
|
criterion_main!(benches_p7);
|
||||||
15
benches/bench_aoc_8.rs
Normal file
15
benches/bench_aoc_8.rs
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
use std::hint::black_box;
|
||||||
|
use criterion::{criterion_group, criterion_main, Criterion};
|
||||||
|
use aoc2025::aoc::aoc_8;
|
||||||
|
|
||||||
|
fn bench_aoc_8_part1(c: &mut Criterion) {
|
||||||
|
let input = black_box(include_str!("../src/aoc/input/full_input_aoc8.txt"));
|
||||||
|
c.bench_function("bench_aoc8 part 1", |b| b.iter(|| aoc_8::solve_p1(input)));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
criterion_group!(
|
||||||
|
benches_p8,
|
||||||
|
bench_aoc_8_part1,
|
||||||
|
);
|
||||||
|
criterion_main!(benches_p8);
|
||||||
@@ -2,3 +2,7 @@ pub mod aoc_1;
|
|||||||
pub mod aoc_2;
|
pub mod aoc_2;
|
||||||
pub mod aoc_3;
|
pub mod aoc_3;
|
||||||
pub mod aoc_4;
|
pub mod aoc_4;
|
||||||
|
pub mod aoc_5;
|
||||||
|
pub mod aoc_6;
|
||||||
|
pub mod aoc_7;
|
||||||
|
pub mod aoc_8;
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
use strength_reduce::StrengthReducedU64;
|
|
||||||
pub fn solve_p1(input: &str) -> u64 {
|
pub fn solve_p1(input: &str) -> u64 {
|
||||||
let mut answer = 0;
|
let mut answer = 0;
|
||||||
for line in input.split(',') {
|
for line in input.split(',') {
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
|
use std::simd::prelude::*;
|
||||||
pub fn solve_p1(input: &[u8]) -> u32 {
|
pub fn solve_p1(input: &[u8]) -> u32 {
|
||||||
let mut answer = 0;
|
let mut answer = 0;
|
||||||
let mut i = 1;
|
let mut i = 1;
|
||||||
@@ -36,37 +37,58 @@ pub fn solve_p1(input: &[u8]) -> u32 {
|
|||||||
|
|
||||||
pub fn solve_p1_fast(input: &[u8]) -> u32 {
|
pub fn solve_p1_fast(input: &[u8]) -> u32 {
|
||||||
let mut answer = 0;
|
let mut answer = 0;
|
||||||
let mut i = 1;
|
let size = input.len()/100 -1;
|
||||||
let len = input.len();
|
//let size = 1;
|
||||||
|
const LANES: usize = 33;
|
||||||
|
let mut idx = 0;
|
||||||
|
type SimdVec = Simd<u8, LANES>;
|
||||||
|
for j in 0..size {
|
||||||
|
let line = &input[idx..idx+100];
|
||||||
|
//println!("{:?}", line);
|
||||||
|
for i in (1..=9) {
|
||||||
|
let target = SimdVec::splat(58-i);
|
||||||
|
|
||||||
let mut num = input[0];
|
let (prefix, chunks, suffix) = line.as_simd::<LANES>();
|
||||||
let mut num2 = input[1];
|
|
||||||
while i < len {
|
|
||||||
let mut j = i;
|
|
||||||
num = input[j-1];
|
|
||||||
num2 = input[j];
|
|
||||||
|
|
||||||
while input[j+1] != 10 {
|
// 4. Process the main SIMD chunks
|
||||||
|
let mask :u128 = (chunks[0].simd_eq(target).to_bitmask() as u128) | (chunks[1].simd_eq(target).to_bitmask() as u128) << LANES | (chunks[2].simd_eq(target).to_bitmask() as u128) << 2*LANES ;
|
||||||
|
let count = mask.count_ones() ;
|
||||||
|
// println!("{:?} ", count);
|
||||||
|
|
||||||
if input[j] > num {
|
if count >= 2 {
|
||||||
num = input[j];
|
answer += ((10 * (10 - i)) + 10 -i) as u32;
|
||||||
num2 = input[j + 1];
|
break;
|
||||||
} else if input[j] > num2 {
|
} else if count == 1 {
|
||||||
num2 = input[j];
|
if 58-i == suffix[0] {
|
||||||
}
|
answer += ((10 * (10-i)) + suffix[0] - 48) as u32;
|
||||||
j +=1;
|
break;
|
||||||
if j+2 == len {
|
}
|
||||||
|
if (58-i)-1 == suffix[0] {
|
||||||
|
answer += ((10 * (10-i)) + suffix[0] - 48) as u32;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
let idx = mask.trailing_zeros() as usize +1;
|
||||||
|
//println!("{:?}, {}, {}", line, idx, 58-i);
|
||||||
|
let line = &line[idx..100];
|
||||||
|
let mut num = suffix[0];
|
||||||
|
//println!("{:?}", line);
|
||||||
|
for v in line.iter() {
|
||||||
|
if v > &num {
|
||||||
|
num = *v;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
//println!("{}", num);
|
||||||
|
answer += (((10 - i) * 10) + num -48) as u32;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
idx += 101;
|
||||||
|
|
||||||
if input[j] > num2 {
|
// 5. Handle the remaining elements (head and tail) via standard scalar iteration
|
||||||
num2 =input[j ];
|
// These are the parts of the array that didn't fit into a perfect 32-byte chunk
|
||||||
}
|
|
||||||
answer += (((num - 48) * 10) + num2 - 48) as u32;
|
|
||||||
|
|
||||||
i = j + 3;
|
//println!("nines {}", count + scalar_check(prefix) + scalar_check(suffix));
|
||||||
}
|
}
|
||||||
|
|
||||||
answer
|
answer
|
||||||
@@ -103,7 +125,6 @@ pub fn solve_p2(input: &[u8]) -> u64 {
|
|||||||
|
|
||||||
pub fn solve_p2_fast(input: &[u8]) -> u64 {
|
pub fn solve_p2_fast(input: &[u8]) -> u64 {
|
||||||
let mut answer = 0;
|
let mut answer = 0;
|
||||||
|
|
||||||
for line in input.split(|&x| x == 10) {
|
for line in input.split(|&x| x == 10) {
|
||||||
let mut end = line.len() - 12;
|
let mut end = line.len() - 12;
|
||||||
let total = 12;
|
let total = 12;
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
|
|
||||||
use std::simd::prelude::*;
|
use std::simd::prelude::*;
|
||||||
pub fn solve_p1(input: &[u8]) -> u64 {
|
pub fn solve_p1(input: &[u8]) -> u64 {
|
||||||
let mut answer = 0;
|
let mut answer = 0;
|
||||||
@@ -68,11 +69,16 @@ pub fn solve_p1_f(input: &[u8]) -> u64 {
|
|||||||
const LANES: usize = 8;
|
const LANES: usize = 8;
|
||||||
type SimdVec = Simd<u8, LANES>;
|
type SimdVec = Simd<u8, LANES>;
|
||||||
|
|
||||||
|
let mut data = [0;8];
|
||||||
|
|
||||||
|
let offsets = usizex8::from_array([0, 1, 2, 140, 142, 240, 241, 242]);
|
||||||
|
|
||||||
for i in len+1..input.len()-len-1 {
|
for i in len+1..input.len()-len-1 {
|
||||||
if input[i] == b'@' {
|
if input[i] == b'@' {
|
||||||
let data= [input[i-1], input[i+1], input[i+len], input[i +len+1], input[i+len-1], input[i-(len+1)], input[i-(len -1)], input[i-len]];
|
data= [input[i-1], input[i+1], input[i+len], input[i +len +1], input[i+len -1], input[i-len], input[i -(len +1)], input[i-(len -1)]];
|
||||||
let (pre, chk, post) = data.as_simd();
|
let chk = SimdVec::from_array(data);
|
||||||
let count = chk[0].simd_eq(target).to_bitmask().count_ones();
|
let count = chk.simd_eq(target).to_bitmask().count_ones();
|
||||||
|
//let count = vals.simd_eq(target).to_bitmask().count_ones();
|
||||||
if count <4 {
|
if count <4 {
|
||||||
answer += 1;
|
answer += 1;
|
||||||
}
|
}
|
||||||
@@ -80,9 +86,9 @@ pub fn solve_p1_f(input: &[u8]) -> u64 {
|
|||||||
}
|
}
|
||||||
for i in 1..len {
|
for i in 1..len {
|
||||||
if input[i] == b'@' {
|
if input[i] == b'@' {
|
||||||
let data= [input[i-1], input[i+1], input[i+len], input[i +len +1], input[i+len -1], 0,0,0];
|
data= [input[i-1], input[i+1], input[i+len], input[i +len +1], input[i+len -1], 0,0,0];
|
||||||
let (pre, chk, post) = data.as_simd();
|
let chk = SimdVec::from_array(data);
|
||||||
let count = chk[0].simd_eq(target).to_bitmask().count_ones();
|
let count = chk.simd_eq(target).to_bitmask().count_ones();
|
||||||
if count <4 {
|
if count <4 {
|
||||||
answer += 1;
|
answer += 1;
|
||||||
}
|
}
|
||||||
@@ -90,9 +96,9 @@ pub fn solve_p1_f(input: &[u8]) -> u64 {
|
|||||||
}
|
}
|
||||||
for i in input.len()-len..input.len()-1 {
|
for i in input.len()-len..input.len()-1 {
|
||||||
if input[i] == b'@' {
|
if input[i] == b'@' {
|
||||||
let data= [input[i-1], input[i+1], input[i-len], input[i -(len +1)], input[i-(len-1)], 0,0,0];
|
data= [input[i-1], input[i+1], input[i-len], input[i -(len +1)], input[i-(len-1)], 0,0,0];
|
||||||
let (pre, chk, post) = data.as_simd();
|
let chk = SimdVec::from_array(data);
|
||||||
let count = chk[0].simd_eq(target).to_bitmask().count_ones();
|
let count = chk.simd_eq(target).to_bitmask().count_ones();
|
||||||
if count <4 {
|
if count <4 {
|
||||||
|
|
||||||
answer += 1;
|
answer += 1;
|
||||||
@@ -100,9 +106,9 @@ pub fn solve_p1_f(input: &[u8]) -> u64 {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
if input[len] == b'@' {
|
if input[len] == b'@' {
|
||||||
let data= [input[len-1], input[len+1], input[0], 0, input[1], 0,0,0];
|
data= [input[len-1], input[len+1], input[0], 0, input[1], 0,0,0];
|
||||||
let (pre, chk, post) = data.as_simd();
|
let chk = SimdVec::from_array(data);
|
||||||
let count = chk[0].simd_eq(target).to_bitmask().count_ones();
|
let count = chk.simd_eq(target).to_bitmask().count_ones();
|
||||||
if count <4 {
|
if count <4 {
|
||||||
|
|
||||||
answer += 1;
|
answer += 1;
|
||||||
@@ -110,9 +116,9 @@ pub fn solve_p1_f(input: &[u8]) -> u64 {
|
|||||||
}
|
}
|
||||||
let idx = input.len()-len -1;
|
let idx = input.len()-len -1;
|
||||||
if input[idx] == b'@' {
|
if input[idx] == b'@' {
|
||||||
let data= [input[idx-1], 0, input[idx - len], input[idx + len], input[idx - (len + 1)], input[idx + (len -1)],0,0];
|
data= [input[idx-1], 0, input[idx - len], input[idx + len], input[idx - (len + 1)], input[idx + (len -1)],0,0];
|
||||||
let (pre, chk, post) = data.as_simd();
|
let chk = SimdVec::from_array(data);
|
||||||
let count = chk[0].simd_eq(target).to_bitmask().count_ones();
|
let count = chk.simd_eq(target).to_bitmask().count_ones();
|
||||||
if count <4 {
|
if count <4 {
|
||||||
|
|
||||||
answer += 1;
|
answer += 1;
|
||||||
@@ -127,6 +133,7 @@ pub fn solve_p1_f(input: &[u8]) -> u64 {
|
|||||||
answer
|
answer
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
pub fn solve_p2(input: &str) -> u16 {
|
pub fn solve_p2(input: &str) -> u16 {
|
||||||
let mut answer = 0;
|
let mut answer = 0;
|
||||||
let mut data:Vec<Vec<char>> = Vec::with_capacity(256);
|
let mut data:Vec<Vec<char>> = Vec::with_capacity(256);
|
||||||
|
|||||||
72
src/aoc/aoc_5.rs
Normal file
72
src/aoc/aoc_5.rs
Normal file
@@ -0,0 +1,72 @@
|
|||||||
|
pub fn solve_p1(input: &str) -> u64 {
|
||||||
|
let mut answer = 0;
|
||||||
|
let mut parse_range = true;
|
||||||
|
let mut ranges = Vec::new();
|
||||||
|
let mut numbers = Vec::new();
|
||||||
|
for line in input.lines() {
|
||||||
|
if line.is_empty() {
|
||||||
|
parse_range = false;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if parse_range {
|
||||||
|
let nums = line.split("-").map(|x| x.parse::<u64>().unwrap()).collect::<Vec<_>>();
|
||||||
|
ranges.push(nums);
|
||||||
|
} else {
|
||||||
|
let num = line.parse::<u64>().unwrap();
|
||||||
|
numbers.push(num);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
ranges.sort();
|
||||||
|
|
||||||
|
for num in numbers {
|
||||||
|
for range in &ranges{
|
||||||
|
if num >= range[0] {
|
||||||
|
if num <= range[1] {
|
||||||
|
answer += 1;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
//println!("{:?} {:?}", ranges, numbers);
|
||||||
|
|
||||||
|
|
||||||
|
answer
|
||||||
|
}
|
||||||
|
pub fn solve_p2(input: &str) -> u64 {
|
||||||
|
let mut answer = 0;
|
||||||
|
let mut ranges = Vec::new();
|
||||||
|
for line in input.lines() {
|
||||||
|
if line.is_empty() {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
let nums = line.split("-").map(|x| x.parse::<u64>().unwrap()).collect::<Vec<_>>();
|
||||||
|
ranges.push(nums);
|
||||||
|
}
|
||||||
|
ranges.sort();
|
||||||
|
let mut new_ranges:Vec<Vec<u64>> = Vec::new();
|
||||||
|
let mut new_range = Vec::<u64>::new();
|
||||||
|
new_range.push(ranges[0][0]);
|
||||||
|
new_range.push(ranges[0][1]);
|
||||||
|
for range in ranges.iter() {
|
||||||
|
if range[0] >= new_range[0] && range[0] <= new_range[1] {
|
||||||
|
if range[1] > new_range[1] {
|
||||||
|
new_range[1] = range[1];
|
||||||
|
}
|
||||||
|
}else if range[0] > new_range[1] {
|
||||||
|
new_ranges.push(Vec::from([new_range[0], new_range[1]]));
|
||||||
|
new_range[0] = range[0];
|
||||||
|
new_range[1] = range[1];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
new_ranges.push(Vec::from([new_range[0], new_range[1]]));
|
||||||
|
for range in new_ranges {
|
||||||
|
answer += range[1] - range[0] + 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
answer
|
||||||
|
}
|
||||||
158
src/aoc/aoc_6.rs
Normal file
158
src/aoc/aoc_6.rs
Normal file
@@ -0,0 +1,158 @@
|
|||||||
|
pub fn solve_p1(input: &str) -> u64 {
|
||||||
|
let mut answer = 0;
|
||||||
|
let lines = input.lines().count();
|
||||||
|
let input = input.split_ascii_whitespace().collect::<Vec<_>>();
|
||||||
|
// println!("{:?}", input.len()/lines);
|
||||||
|
|
||||||
|
let len = input.len()/lines;
|
||||||
|
for i in 0..len {
|
||||||
|
if input[i+(lines-1)*len] == "+" {
|
||||||
|
let mut sum = input[i].parse::<u64>().unwrap();
|
||||||
|
for j in 1..lines-1{
|
||||||
|
sum += input[i+ len* j].parse::<u64>().unwrap();
|
||||||
|
}
|
||||||
|
answer += sum;
|
||||||
|
} else {
|
||||||
|
let mut product = input[i].parse::<u64>().unwrap();
|
||||||
|
for j in 1..lines-1{
|
||||||
|
product *= input[i+ len* j].parse::<u64>().unwrap();
|
||||||
|
}
|
||||||
|
answer += product;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
answer
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn solve_p2(input: &str) -> u64 {
|
||||||
|
let mut answer = 0;
|
||||||
|
let lines = 4;
|
||||||
|
let input = input.lines().collect::<Vec<_>>();
|
||||||
|
//println!("{:?}", input);
|
||||||
|
let mut len = [0;5];
|
||||||
|
for i in 0..lines{
|
||||||
|
len[i] = input[i].len();
|
||||||
|
}
|
||||||
|
|
||||||
|
let max_idx = *len.iter().max().unwrap();
|
||||||
|
|
||||||
|
|
||||||
|
let mut temp_ans = 0;
|
||||||
|
let mut start_idx = 0;
|
||||||
|
let mut op = false;
|
||||||
|
for i in 0..max_idx{
|
||||||
|
if i == start_idx {
|
||||||
|
if input[4].chars().collect::<Vec<_>>()[i] == '+'{
|
||||||
|
op = false;
|
||||||
|
}else {
|
||||||
|
op = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let mut num1 = ' ';
|
||||||
|
if i < len[0] {
|
||||||
|
num1 = input[0].chars().collect::<Vec<_>>()[i];
|
||||||
|
}
|
||||||
|
let mut num2 = ' ';
|
||||||
|
if i < len[1] {
|
||||||
|
num2 = input[1].chars().collect::<Vec<_>>()[i];
|
||||||
|
}
|
||||||
|
let mut num3 = ' ';
|
||||||
|
if i < len[2] {
|
||||||
|
num3 = input[2].chars().collect::<Vec<_>>()[i];
|
||||||
|
}
|
||||||
|
let mut num4 = ' ';
|
||||||
|
if i < len[3] {
|
||||||
|
num4 = input[3].chars().collect::<Vec<_>>()[i];
|
||||||
|
}
|
||||||
|
let full_num = format!("{}{}{}{}",num1 , num2 , num3, num4).trim().parse::<u64>().unwrap_or(0);
|
||||||
|
|
||||||
|
if full_num != 0 {
|
||||||
|
if op {
|
||||||
|
if temp_ans != 0{
|
||||||
|
temp_ans *= full_num;
|
||||||
|
} else {
|
||||||
|
temp_ans = full_num;
|
||||||
|
}
|
||||||
|
|
||||||
|
} else {
|
||||||
|
temp_ans += full_num;
|
||||||
|
}
|
||||||
|
}else {
|
||||||
|
//println!("{}", temp_ans);
|
||||||
|
answer += temp_ans;
|
||||||
|
start_idx = i+1;
|
||||||
|
temp_ans = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
answer += temp_ans;
|
||||||
|
|
||||||
|
|
||||||
|
answer
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn solve_p2_f(input: &str) -> u64 {
|
||||||
|
let mut answer = 0;
|
||||||
|
let lines = 4;
|
||||||
|
let input = input.lines().map(| x| {x.chars().collect()}).collect::<Vec<Vec<char>>>();
|
||||||
|
println!("{:?}", input);
|
||||||
|
let mut len = [0;5];
|
||||||
|
for i in 0..lines{
|
||||||
|
len[i] = input[i].len();
|
||||||
|
}
|
||||||
|
|
||||||
|
let max_idx = *len.iter().max().unwrap();
|
||||||
|
|
||||||
|
|
||||||
|
let mut temp_ans = 0;
|
||||||
|
let mut start_idx = 0;
|
||||||
|
let mut op = false;
|
||||||
|
for i in 0..max_idx{
|
||||||
|
if i == start_idx {
|
||||||
|
if input[4][i] == '+'{
|
||||||
|
op = false;
|
||||||
|
}else {
|
||||||
|
op = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let mut num1 = ' ';
|
||||||
|
if i < len[0] {
|
||||||
|
num1 = input[0][i];
|
||||||
|
}
|
||||||
|
let mut num2 = ' ';
|
||||||
|
if i < len[1] {
|
||||||
|
num2 = input[1][i];
|
||||||
|
}
|
||||||
|
let mut num3 = ' ';
|
||||||
|
if i < len[2] {
|
||||||
|
num3 = input[2][i];
|
||||||
|
}
|
||||||
|
let mut num4 = ' ';
|
||||||
|
if i < len[3] {
|
||||||
|
num4 = input[3][i];
|
||||||
|
}
|
||||||
|
let full_num = format!("{}{}{}{}",num1 , num2 , num3, num4).trim().parse::<u64>().unwrap_or(0);
|
||||||
|
|
||||||
|
if full_num != 0 {
|
||||||
|
if op {
|
||||||
|
if temp_ans != 0{
|
||||||
|
temp_ans *= full_num;
|
||||||
|
} else {
|
||||||
|
temp_ans = full_num;
|
||||||
|
}
|
||||||
|
|
||||||
|
} else {
|
||||||
|
temp_ans += full_num;
|
||||||
|
}
|
||||||
|
}else {
|
||||||
|
//println!("{}", temp_ans);
|
||||||
|
answer += temp_ans;
|
||||||
|
start_idx = i+1;
|
||||||
|
temp_ans = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
answer += temp_ans;
|
||||||
|
|
||||||
|
|
||||||
|
answer
|
||||||
|
}
|
||||||
83
src/aoc/aoc_7.rs
Normal file
83
src/aoc/aoc_7.rs
Normal file
@@ -0,0 +1,83 @@
|
|||||||
|
|
||||||
|
|
||||||
|
pub fn solve_p1(input: &str) -> u64 {
|
||||||
|
let mut answer = 0;
|
||||||
|
let lines = input.lines().count();
|
||||||
|
let mut data = Vec::new();
|
||||||
|
for line in input.lines() {
|
||||||
|
let chars = line.chars().collect::<Vec<char>>();
|
||||||
|
data.push(chars);
|
||||||
|
}
|
||||||
|
let start_idx = data[0].iter().len() / 2 ;
|
||||||
|
data[1][start_idx] = '|';
|
||||||
|
|
||||||
|
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] == '^' {
|
||||||
|
if data[i-1][j] == '|' {
|
||||||
|
answer += 1;
|
||||||
|
data[i][j+1] = '|';
|
||||||
|
data[i][j-1] = '|';
|
||||||
|
data[i+1][j-1] = '|';
|
||||||
|
data[i+1][j+1] = '|';
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if data[i][j] == '|' {
|
||||||
|
if i+1 < data.len() {
|
||||||
|
if data[i+1][j] != '^' {
|
||||||
|
data[i+1][j] = '|';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
answer
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn solve_p2(input: &[u8]) -> u64 {
|
||||||
|
let mut answer = 0;
|
||||||
|
let mut data = Vec::with_capacity(256);
|
||||||
|
for line in input.split(|x1| {*x1 == b'\n'}).step_by(1) {
|
||||||
|
let chars = line.into_iter().map(|mut x| {if x == &b'.' {x = &0; *x as u64} else {*x as u64}}).collect::<Vec<u64>>();
|
||||||
|
data.push(chars);
|
||||||
|
}
|
||||||
|
let start_idx = data[0].iter().len() / 2 ;
|
||||||
|
data[0][start_idx] = 1;
|
||||||
|
|
||||||
|
|
||||||
|
for i in 0..data.len() {
|
||||||
|
//println!("{:?}", data[i]);
|
||||||
|
for j in 0..data[0].len(){
|
||||||
|
//println!("{}", data[i][j]);
|
||||||
|
if data[i][j] == 94 {
|
||||||
|
data[i][j] = 0;
|
||||||
|
//println!("{}", data[i][j]);
|
||||||
|
if data[i-1][j] != 0 || data[i][j-1] == 49 {
|
||||||
|
//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
|
||||||
|
}
|
||||||
56
src/aoc/aoc_8.rs
Normal file
56
src/aoc/aoc_8.rs
Normal file
@@ -0,0 +1,56 @@
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
pub fn solve_p1(input: &str) -> u64 {
|
||||||
|
let mut answer = 0;
|
||||||
|
let mut cords = Vec::new();
|
||||||
|
for line in input.lines() {
|
||||||
|
let data = line.split(',').flat_map(|d| d.parse::<i64>()).collect_array::<3>().unwrap();
|
||||||
|
cords.push(data);
|
||||||
|
}
|
||||||
|
//println!("{:?}", cords);
|
||||||
|
|
||||||
|
let mut closest:Vec<[i64; 3]> = Vec::new();
|
||||||
|
let mut pairs:Vec<([i64;3], [i64;3])> = Vec::new();
|
||||||
|
for i in 0..cords.len() {
|
||||||
|
let mut distance = i64::MAX;;
|
||||||
|
for j in 0..cords.len() {
|
||||||
|
if i != j {
|
||||||
|
let distance_x = cords[i][0] - cords[j][0];
|
||||||
|
let distance_y = cords[i][1] - cords[j][1];
|
||||||
|
let distance_z = cords[i][2] - cords[j][2];
|
||||||
|
let new_distance = ((distance_x.pow(2) + distance_y.pow(2)) + distance_z.pow(2)).isqrt() ;
|
||||||
|
if new_distance < distance {
|
||||||
|
distance = new_distance;
|
||||||
|
if i < closest.len() {
|
||||||
|
closest[i] = cords[j];
|
||||||
|
} else {
|
||||||
|
closest.insert(i, cords[j]);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for i in 0..closest.len() {
|
||||||
|
if !pairs.contains(&(cords[i], closest[i])) && !pairs.contains(&(closest[i], cords[i])) {
|
||||||
|
if cords[i][0] < closest[i][0] {
|
||||||
|
pairs.push((cords[i], closest[i]));
|
||||||
|
} else {
|
||||||
|
pairs.push((closest[i], cords[i]));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
pairs.sort_unstable();
|
||||||
|
//vec of vecs containing all cords combined together
|
||||||
|
let mut circuits: Vec<Vec<[i64;3]>> = Vec::new();
|
||||||
|
//circuits[0].push(pairs[0].0);
|
||||||
|
for i in 0..pairs.len() {
|
||||||
|
|
||||||
|
}
|
||||||
|
// make tree, insert data check if cord in tree
|
||||||
|
//println!("{:?}", pairs);
|
||||||
|
answer
|
||||||
|
}
|
||||||
11
src/aoc/input/test_input_aoc5.txt
Normal file
11
src/aoc/input/test_input_aoc5.txt
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
3-5
|
||||||
|
10-14
|
||||||
|
16-20
|
||||||
|
12-18
|
||||||
|
|
||||||
|
1
|
||||||
|
5
|
||||||
|
8
|
||||||
|
11
|
||||||
|
17
|
||||||
|
32
|
||||||
4
src/aoc/input/test_input_aoc6.txt
Normal file
4
src/aoc/input/test_input_aoc6.txt
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
123 328 51 64
|
||||||
|
45 64 387 23
|
||||||
|
6 98 215 314
|
||||||
|
* + * +
|
||||||
16
src/aoc/input/test_input_aoc7.txt
Normal file
16
src/aoc/input/test_input_aoc7.txt
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
.......S.......
|
||||||
|
...............
|
||||||
|
.......^.......
|
||||||
|
...............
|
||||||
|
......^.^......
|
||||||
|
...............
|
||||||
|
.....^.^.^.....
|
||||||
|
...............
|
||||||
|
....^.^...^....
|
||||||
|
...............
|
||||||
|
...^.^...^.^...
|
||||||
|
...............
|
||||||
|
..^...^.....^..
|
||||||
|
...............
|
||||||
|
.^.^.^.^.^...^.
|
||||||
|
...............
|
||||||
20
src/aoc/input/test_input_aoc8.txt
Normal file
20
src/aoc/input/test_input_aoc8.txt
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
162,817,812
|
||||||
|
57,618,57
|
||||||
|
906,360,560
|
||||||
|
592,479,940
|
||||||
|
352,342,300
|
||||||
|
466,668,158
|
||||||
|
542,29,236
|
||||||
|
431,825,988
|
||||||
|
739,650,466
|
||||||
|
52,470,668
|
||||||
|
216,146,977
|
||||||
|
819,987,18
|
||||||
|
117,168,530
|
||||||
|
805,96,715
|
||||||
|
346,949,466
|
||||||
|
970,615,88
|
||||||
|
941,993,340
|
||||||
|
862,61,35
|
||||||
|
984,92,344
|
||||||
|
425,690,689
|
||||||
@@ -1,2 +1,3 @@
|
|||||||
#![feature(portable_simd)]
|
#![feature(portable_simd)]
|
||||||
|
#![feature(exact_length_collection)]
|
||||||
pub mod aoc;
|
pub mod aoc;
|
||||||
|
|||||||
75
src/main.rs
75
src/main.rs
@@ -1,4 +1,8 @@
|
|||||||
#![feature(portable_simd)] // Enable the nightly feature
|
#![feature(portable_simd)]
|
||||||
|
#![feature(exact_length_collection)]
|
||||||
|
extern crate core;
|
||||||
|
|
||||||
|
// Enable the nightly feature
|
||||||
pub mod aoc;
|
pub mod aoc;
|
||||||
use crate::aoc::*;
|
use crate::aoc::*;
|
||||||
|
|
||||||
@@ -11,14 +15,48 @@ fn main() {
|
|||||||
let answer = aoc_3::solve_p1_fast(input2);
|
let answer = aoc_3::solve_p1_fast(input2);
|
||||||
println!("{}", answer);
|
println!("{}", answer);
|
||||||
*/
|
*/
|
||||||
|
/*
|
||||||
|
let input = include_bytes!("aoc/input/full_input_aoc4.txt");
|
||||||
|
let answer = aoc_4::solve_p1_f(input);
|
||||||
|
println!("{}", answer);
|
||||||
|
|
||||||
let input = include_bytes!("aoc/input/full_input_aoc4.txt");
|
let input = include_bytes!("aoc/input/full_input_aoc4.txt");
|
||||||
let answer = aoc_4::solve_p2_f(input);
|
let answer = aoc_4::solve_p1(input);
|
||||||
println!("{}", answer);
|
println!("{}", answer);
|
||||||
|
|
||||||
let input = include_str!("aoc/input/full_input_aoc4.txt");
|
|
||||||
let answer = aoc_4::solve_p2(input);
|
*/
|
||||||
|
/*
|
||||||
|
let input = include_str!("aoc/input/full_input_aoc5.txt");
|
||||||
|
let answer = aoc_5::solve_p1(input);
|
||||||
println!("{}", answer);
|
println!("{}", answer);
|
||||||
|
|
||||||
|
let input = include_str!("aoc/input/test_input_aoc5.txt");
|
||||||
|
let answer = aoc_5::solve_p2(input);
|
||||||
|
println!("{}", answer);
|
||||||
|
|
||||||
|
*/
|
||||||
|
/*
|
||||||
|
let input = include_str!("aoc/input/full_input_aoc6.txt");
|
||||||
|
let answer = aoc_6::solve_p1(input);
|
||||||
|
println!("answer: {}", answer);
|
||||||
|
|
||||||
|
let input = include_str!("aoc/input/full_input_aoc6.txt");
|
||||||
|
let answer = aoc_6::solve_p2(input);
|
||||||
|
println!("answer: {}", answer);
|
||||||
|
|
||||||
|
*/
|
||||||
|
/*
|
||||||
|
let input = include_str!("aoc/input/full_input_aoc7.txt");
|
||||||
|
let answer = aoc_7::solve_p1(input);
|
||||||
|
println!("answer: {}", answer);
|
||||||
|
let input = include_bytes!("aoc/input/full_input_aoc7.txt");
|
||||||
|
let answer = aoc_7::solve_p2(input);
|
||||||
|
println!("answer: {}", answer);
|
||||||
|
*/
|
||||||
|
let input = include_str!("aoc/input/test_input_aoc8.txt");
|
||||||
|
let answer = aoc_8::solve_p1(input);
|
||||||
|
println!("answer: {}", answer);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
@@ -74,3 +112,32 @@ fn test_aoc_3_part2() {
|
|||||||
172787336861064
|
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
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user