optimized day1 solutions
This commit is contained in:
parent
e1e03896d5
commit
9d74ae5775
|
@ -1,32 +1,35 @@
|
||||||
#![feature(test)]
|
#![feature(test)]
|
||||||
extern crate test;
|
extern crate test;
|
||||||
|
|
||||||
const INPUTS: [&'static str; 2] = [include_str!("./sample.txt"), include_str!("./input.txt")];
|
const INPUTS: [&[u8]; 2] = [
|
||||||
|
include_bytes!("./sample.txt"),
|
||||||
|
include_bytes!("./input.txt"),
|
||||||
|
];
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
for input in INPUTS.iter() {
|
for input in INPUTS.iter() {
|
||||||
let mut total = 0;
|
println!("total = {}", process(input));
|
||||||
for line in input.split('\n') {
|
|
||||||
total += process(line);
|
|
||||||
}
|
|
||||||
|
|
||||||
println!("total = {}", total);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn process(data: &str) -> u32 {
|
fn process(input: &[u8]) -> u32 {
|
||||||
let first = data
|
let mut total = 0;
|
||||||
.chars()
|
for line in input.split(|&x| x == b'\n') {
|
||||||
.find(|c| c.is_numeric())
|
let first = line
|
||||||
.map_or(0, |x| 10 * x.to_digit(10).unwrap());
|
.iter()
|
||||||
|
.find(|c| c.is_ascii_digit())
|
||||||
|
.map_or(0, |x| 10 * (x - b'0'));
|
||||||
|
|
||||||
let last = data
|
let last = line
|
||||||
.chars()
|
.iter()
|
||||||
.rev()
|
.rev()
|
||||||
.find(|c| c.is_numeric())
|
.find(|c| c.is_ascii_digit())
|
||||||
.map_or(0, |x| x.to_digit(10).unwrap());
|
.map_or(0, |x| x - b'0');
|
||||||
|
|
||||||
first + last
|
total += first as u32 + last as u32;
|
||||||
|
}
|
||||||
|
|
||||||
|
total
|
||||||
}
|
}
|
||||||
|
|
||||||
#[bench]
|
#[bench]
|
||||||
|
|
|
@ -1,16 +1,17 @@
|
||||||
#![feature(test)]
|
#![feature(test)]
|
||||||
extern crate test;
|
extern crate test;
|
||||||
|
|
||||||
const INPUTS: [&str; 2] = [include_str!("./sample.txt"), include_str!("./input.txt")];
|
const INPUTS: [&[u8]; 2] = [
|
||||||
|
include_bytes!("./sample.txt"),
|
||||||
|
include_bytes!("./input.txt"),
|
||||||
|
];
|
||||||
|
|
||||||
fn process(data: &str) -> u64 {
|
fn process(data: &[u8]) -> u64 {
|
||||||
let mut total: u64 = 0;
|
let mut total: u64 = 0;
|
||||||
|
|
||||||
for line in data.split('\n') {
|
for line in data.split(|&x| x == b'\n') {
|
||||||
let line: Vec<char> = line.chars().collect();
|
let first = 10 * find_forward(line);
|
||||||
|
let last = find_backward(line);
|
||||||
let first = 10 * find_forward(&line);
|
|
||||||
let last = find_backward(&line);
|
|
||||||
|
|
||||||
total += first as u64 + last as u64;
|
total += first as u64 + last as u64;
|
||||||
}
|
}
|
||||||
|
@ -18,13 +19,13 @@ fn process(data: &str) -> u64 {
|
||||||
total
|
total
|
||||||
}
|
}
|
||||||
|
|
||||||
fn find_forward(iter: &[char]) -> u8 {
|
fn find_forward(iter: &[u8]) -> u8 {
|
||||||
let mut start = 0;
|
let mut start = 0;
|
||||||
let mut end = 0;
|
let mut end = 0;
|
||||||
|
|
||||||
for c in iter.iter() {
|
for c in iter.iter() {
|
||||||
if ('1'..='9').contains(c) {
|
if (b'1'..=b'9').contains(c) {
|
||||||
return *c as u8 - b'0';
|
return c - b'0';
|
||||||
}
|
}
|
||||||
|
|
||||||
if end - start >= 5 {
|
if end - start >= 5 {
|
||||||
|
@ -41,7 +42,7 @@ fn find_forward(iter: &[char]) -> u8 {
|
||||||
|
|
||||||
get_digit(&iter[start..end]).unwrap_or(0)
|
get_digit(&iter[start..end]).unwrap_or(0)
|
||||||
}
|
}
|
||||||
fn find_backward(iter: &[char]) -> u8 {
|
fn find_backward(iter: &[u8]) -> u8 {
|
||||||
if iter.is_empty() {
|
if iter.is_empty() {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
@ -49,8 +50,8 @@ fn find_backward(iter: &[char]) -> u8 {
|
||||||
let mut end = iter.len();
|
let mut end = iter.len();
|
||||||
|
|
||||||
for c in iter.iter().rev() {
|
for c in iter.iter().rev() {
|
||||||
if ('1'..='9').contains(c) {
|
if (b'1'..=b'9').contains(c) {
|
||||||
return *c as u8 - b'0';
|
return c - b'0';
|
||||||
}
|
}
|
||||||
if end - start >= 5 {
|
if end - start >= 5 {
|
||||||
end -= 1;
|
end -= 1;
|
||||||
|
@ -75,23 +76,36 @@ fn main() {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
const fn get_digit(set: &[char]) -> Option<u8> {
|
const fn get_digit(set: &[u8]) -> Option<u8> {
|
||||||
match set {
|
match set {
|
||||||
['t', 'h', 'r', 'e', 'e'] => Some(3),
|
[b't', b'h', b'r', b'e', b'e'] => Some(3),
|
||||||
['s', 'e', 'v', 'e', 'n'] => Some(7),
|
[b's', b'e', b'v', b'e', b'n'] => Some(7),
|
||||||
['e', 'i', 'g', 'h', 't'] => Some(8),
|
[b'e', b'i', b'g', b'h', b't'] => Some(8),
|
||||||
['o', 'n', 'e', _, _] | ['o', 'n', 'e'] | [_, 'o', 'n', 'e'] | [_, _, 'o', 'n', 'e'] => {
|
|
||||||
Some(1)
|
[b'o', b'n', b'e', _, _]
|
||||||
|
| [b'o', b'n', b'e']
|
||||||
|
| [_, b'o', b'n', b'e']
|
||||||
|
| [_, _, b'o', b'n', b'e'] => Some(1),
|
||||||
|
|
||||||
|
[b't', b'w', b'o', _, _]
|
||||||
|
| [b't', b'w', b'o']
|
||||||
|
| [_, b't', b'w', b'o']
|
||||||
|
| [_, _, b't', b'w', b'o'] => Some(2),
|
||||||
|
|
||||||
|
[b's', b'i', b'x', _, _]
|
||||||
|
| [b's', b'i', b'x']
|
||||||
|
| [_, b's', b'i', b'x']
|
||||||
|
| [_, _, b's', b'i', b'x'] => Some(6),
|
||||||
|
|
||||||
|
[b'n', b'i', b'n', b'e', _] | [b'n', b'i', b'n', b'e'] | [_, b'n', b'i', b'n', b'e'] => {
|
||||||
|
Some(9)
|
||||||
}
|
}
|
||||||
['t', 'w', 'o', _, _] | ['t', 'w', 'o'] | [_, 't', 'w', 'o'] | [_, _, 't', 'w', 'o'] => {
|
[b'f', b'o', b'u', b'r', _] | [b'f', b'o', b'u', b'r'] | [_, b'f', b'o', b'u', b'r'] => {
|
||||||
Some(2)
|
Some(4)
|
||||||
}
|
}
|
||||||
['s', 'i', 'x', _, _] | ['s', 'i', 'x'] | [_, 's', 'i', 'x'] | [_, _, 's', 'i', 'x'] => {
|
[b'f', b'i', b'v', b'e', _] | [b'f', b'i', b'v', b'e'] | [_, b'f', b'i', b'v', b'e'] => {
|
||||||
Some(6)
|
Some(5)
|
||||||
}
|
}
|
||||||
['n', 'i', 'n', 'e', _] | ['n', 'i', 'n', 'e'] | [_, 'n', 'i', 'n', 'e'] => Some(9),
|
|
||||||
['f', 'o', 'u', 'r', _] | ['f', 'o', 'u', 'r'] | [_, 'f', 'o', 'u', 'r'] => Some(4),
|
|
||||||
['f', 'i', 'v', 'e', _] | ['f', 'i', 'v', 'e'] | [_, 'f', 'i', 'v', 'e'] => Some(5),
|
|
||||||
_ => None,
|
_ => None,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user