2022 Day 6 Rust and C++ ports for benching
This commit is contained in:
parent
d4a3d8f89b
commit
ccb36ea4af
|
@ -0,0 +1,55 @@
|
||||||
|
#include <cinttypes>
|
||||||
|
#include <fstream>
|
||||||
|
#include <iostream>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
bool substring_unique_copyless(std::vector<uint8_t> *s, size_t right, size_t length) {
|
||||||
|
for (size_t i = (right-length); i<right; i++) {
|
||||||
|
uint8_t c = (*s)[i]; //s[i];
|
||||||
|
for (size_t j = (i+1); j<right; j++) {
|
||||||
|
if ((*s)[j] == c) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
size_t find_first_unique_run(std::vector<uint8_t> *s, size_t num, size_t skip) {
|
||||||
|
for (size_t right = skip; right<s->size(); right++) {
|
||||||
|
if (substring_unique_copyless(s, right, num)) {
|
||||||
|
return right;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
void run_once(std::vector<uint8_t> *s) {
|
||||||
|
size_t four = find_first_unique_run(s, 4, 4);
|
||||||
|
size_t fourteen = find_first_unique_run(s, 14, four);
|
||||||
|
std::cout << "Part 1: " << four << '\n';
|
||||||
|
std::cout << "Part 2: " << fourteen << '\n';
|
||||||
|
}
|
||||||
|
|
||||||
|
void run_many(std::vector<uint8_t> *s, size_t iterations) {
|
||||||
|
size_t four = 0;
|
||||||
|
size_t fourteen = 0;
|
||||||
|
for (size_t i = 0; i<iterations; i++) {
|
||||||
|
four = find_first_unique_run(s, 4, 4);
|
||||||
|
fourteen = find_first_unique_run(s, 14, four);
|
||||||
|
}
|
||||||
|
std::cout << "Part 1: " << four << '\n';
|
||||||
|
std::cout << "Part 2: " << fourteen << '\n';
|
||||||
|
std::cout << "Completed " << iterations << " iterations\n";
|
||||||
|
}
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
std::ifstream input_file("input/6", std::ios::binary | std::ios::ate);
|
||||||
|
const std::streamoff eof_position = static_cast<std::streamoff>(input_file.tellg());
|
||||||
|
std::vector<uint8_t> input = std::vector<uint8_t>(eof_position);
|
||||||
|
input_file.seekg(0, std::ios::beg);
|
||||||
|
input_file.read(reinterpret_cast<char*>(input.data()), eof_position);
|
||||||
|
|
||||||
|
// run_once(&input);
|
||||||
|
run_many(&input, 1000000);
|
||||||
|
}
|
|
@ -0,0 +1,50 @@
|
||||||
|
#![allow(dead_code)]
|
||||||
|
use std::fs;
|
||||||
|
|
||||||
|
type Int = usize;
|
||||||
|
|
||||||
|
fn substring_unique_copyless(s: &Vec<u8>, right: Int, length: Int) -> bool {
|
||||||
|
for i in (right-length)..right {
|
||||||
|
let c = s[i];
|
||||||
|
for j in (i+1)..right {
|
||||||
|
if s[j] == c {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
fn find_first_unique_run(s: &Vec<u8>, num: Int, skip: Int) -> Int {
|
||||||
|
for right in num.max(skip)..s.len() {
|
||||||
|
if substring_unique_copyless(s, right, num) {
|
||||||
|
return right;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
fn run_once(s: &Vec<u8>) {
|
||||||
|
let four = find_first_unique_run(&s, 4, 0);
|
||||||
|
let fourteen = find_first_unique_run(&s, 14, four);
|
||||||
|
println!("Part 1: {}", four);
|
||||||
|
println!("Part 2: {}", fourteen);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn run_many(s: &Vec<u8>, iterations: usize) {
|
||||||
|
let mut four = 0;
|
||||||
|
let mut fourteen = 0;
|
||||||
|
for _ in 0..iterations {
|
||||||
|
four = find_first_unique_run(&s, 4, 0);
|
||||||
|
fourteen = find_first_unique_run(&s, 14, four);
|
||||||
|
}
|
||||||
|
println!("Part 1: {}", four);
|
||||||
|
println!("Part 2: {}", fourteen);
|
||||||
|
println!("Completed {} iterations", iterations);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
let input: Vec<u8> = fs::read("input/6").expect("Input not found!");
|
||||||
|
// run_once(&input);
|
||||||
|
run_many(&input, 1_000_000);
|
||||||
|
}
|
Loading…
Reference in New Issue