updated day1 solutions

This commit is contained in:
Ishan Jain 2023-12-01 14:06:56 +05:30
parent af973212d8
commit 7474d03451
Signed by: ishan
GPG Key ID: 0506DB2A1CC75C27
3 changed files with 84 additions and 109 deletions

View File

@ -1,23 +1,38 @@
#![feature(test)]
extern crate test;
const INPUTS: [&'static str; 2] = [include_str!("./sample.txt"), include_str!("./input.txt")]; const INPUTS: [&'static str; 2] = [include_str!("./sample.txt"), include_str!("./input.txt")];
fn main() { fn main() {
for input in INPUTS.iter() { for input in INPUTS.iter() {
let mut total = 0; let mut total = 0;
for line in input.split('\n') { for line in input.split('\n') {
let first = line total += process(line);
.chars()
.find(|c| c.is_numeric())
.map_or(0, |x| 10 * x.to_digit(10).unwrap());
let last = line
.chars()
.rev()
.find(|c| c.is_numeric())
.map_or(0, |x| x.to_digit(10).unwrap());
total += (first + last);
} }
println!("total = {}", total); println!("total = {}", total);
} }
} }
fn process(data: &str) -> u32 {
let first = data
.chars()
.find(|c| c.is_numeric())
.map_or(0, |x| 10 * x.to_digit(10).unwrap());
let last = data
.chars()
.rev()
.find(|c| c.is_numeric())
.map_or(0, |x| x.to_digit(10).unwrap());
first + last
}
#[bench]
fn part1(b: &mut test::Bencher) {
b.iter(|| {
let v = process(INPUTS[1]);
test::black_box(v);
});
}

View File

@ -1,118 +1,72 @@
use std::collections::VecDeque; #![feature(test)]
extern crate test;
const INPUTS: [&str; 2] = [include_str!("./sample.txt"), include_str!("./input.txt")]; const INPUTS: [&str; 2] = [include_str!("./sample.txt"), include_str!("./input.txt")];
fn main() { fn process(data: &str) -> u64 {
for input in INPUTS.iter() { let mut total: u64 = 0;
let mut total: u64 = 0;
for line in input.split('\n') { for line in data.split('\n') {
let line: Vec<char> = line.chars().collect(); let mut first = 0;
let mut last = 0;
let mut first = None; let mut window = Vec::with_capacity(line.len());
let mut last = 0; let mut j = 0;
let mut tmp: VecDeque<char> = VecDeque::new(); for (i, c) in line.chars().enumerate() {
if (window.len() - j) >= 5 {
j += 1;
}
for c in line.iter() { match c {
if tmp.len() >= 5 { v @ '1'..='9' if first == 0 => {
tmp.pop_front(); let num = v as u8 - b'0';
first = 10 * num;
last = num;
j = i + 1;
} }
v @ '1'..='9' => {
let num = v as u8 - b'0';
last = num;
match c { j = i + 1;
'1' => {
if first.is_none() {
first = Some(10 * 1);
}
last = 1;
tmp.clear();
}
'2' => {
if first.is_none() {
first = Some(10 * 2);
}
last = 2;
tmp.clear();
}
'3' => {
if first.is_none() {
first = Some(10 * 3);
}
last = 3;
tmp.clear();
}
'4' => {
if first.is_none() {
first = Some(10 * 4);
}
last = 4;
tmp.clear();
}
'5' => {
if first.is_none() {
first = Some(10 * 5);
}
last = 5;
tmp.clear();
}
'6' => {
if first.is_none() {
first = Some(10 * 6);
}
last = 6;
tmp.clear();
}
'7' => {
if first.is_none() {
first = Some(10 * 7);
}
last = 7;
tmp.clear();
}
'8' => {
if first.is_none() {
first = Some(10 * 8);
}
last = 8;
tmp.clear();
}
'9' => {
if first.is_none() {
first = Some(10 * 9);
}
last = 9;
tmp.clear();
}
c => {
tmp.push_back(*c);
}
} }
_ => (),
}
window.push(c);
if let Some(digit) = get_digit(tmp.make_contiguous()) { if window.len() - j >= 3 {
if first.is_none() { if let Some(digit) = get_digit(&window[j..]) {
first = Some(10 * digit); if first == 0 {
first = 10 * digit;
} }
last = digit; last = digit;
} }
} }
if let Some(digit) = get_digit(tmp.make_contiguous()) {
if first.is_none() {
first = Some(10 * digit);
}
last = digit;
}
total += first.unwrap_or(0) as u64 + last as u64;
} }
println!("total = {}", total); if let Some(digit) = get_digit(&window[j..]) {
if first == 0 {
first = 10 * digit;
}
last = digit;
}
total += first as u64 + last as u64;
}
total
}
fn main() {
for input in INPUTS.iter() {
println!("total = {}", process(input));
} }
} }
fn get_digit(set: &[char]) -> Option<u8> { #[inline]
const fn get_digit(set: &[char]) -> Option<u8> {
match set { match set {
['t', 'h', 'r', 'e', 'e'] => Some(3), ['t', 'h', 'r', 'e', 'e'] => Some(3),
['s', 'e', 'v', 'e', 'n'] => Some(7), ['s', 'e', 'v', 'e', 'n'] => Some(7),
@ -132,3 +86,11 @@ fn get_digit(set: &[char]) -> Option<u8> {
_ => None, _ => None,
} }
} }
#[bench]
fn part2(b: &mut test::Bencher) {
b.iter(|| {
let v = process(INPUTS[1]);
test::black_box(v);
});
}

View File

@ -5,5 +5,3 @@ xtwone3four
4nineeightseven2 4nineeightseven2
zoneight234 zoneight234
7pqrstsixteen 7pqrstsixteen
2tqbxgrrpmxqfglsqjkqthree6nhjvbxpflhr1eightwohr