2023-12-02 10:00:46 +00:00
|
|
|
#![feature(slice_split_once)]
|
2023-12-02 05:33:06 +00:00
|
|
|
#![feature(test)]
|
|
|
|
extern crate test;
|
|
|
|
|
2023-12-02 10:00:46 +00:00
|
|
|
const INPUTS: [&[u8]; 2] = [
|
|
|
|
include_bytes!("./sample.txt"),
|
|
|
|
include_bytes!("./input.txt"),
|
|
|
|
];
|
2023-12-02 05:33:06 +00:00
|
|
|
|
2023-12-02 10:00:46 +00:00
|
|
|
fn process(data: &[u8]) -> u64 {
|
2023-12-02 05:33:06 +00:00
|
|
|
let mut total = 0;
|
|
|
|
|
2023-12-02 10:00:46 +00:00
|
|
|
for line in data.split(|&x| x == b'\n') {
|
2023-12-02 05:33:06 +00:00
|
|
|
if line.is_empty() {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
let mut min_red = 0;
|
|
|
|
let mut min_green = 0;
|
|
|
|
let mut min_blue = 0;
|
|
|
|
|
2023-12-02 10:00:46 +00:00
|
|
|
let line = &line[5..];
|
|
|
|
let (_, mut line) = read_int(line);
|
2023-12-02 05:41:35 +00:00
|
|
|
|
2023-12-02 10:00:46 +00:00
|
|
|
while !line.is_empty() {
|
|
|
|
let (num, l) = read_int(&line[1..]);
|
|
|
|
|
|
|
|
match l {
|
|
|
|
[b'r', ..] => {
|
|
|
|
min_red = min_red.max(num);
|
|
|
|
line = &l[3..];
|
|
|
|
}
|
|
|
|
[b'g', ..] => {
|
|
|
|
min_green = min_green.max(num);
|
|
|
|
line = &l[5..];
|
|
|
|
}
|
|
|
|
[b'b', ..] => {
|
|
|
|
min_blue = min_blue.max(num);
|
|
|
|
line = &l[4..];
|
|
|
|
}
|
|
|
|
[b' ', ..] => line = l,
|
|
|
|
_ => (),
|
|
|
|
}
|
2023-12-02 05:33:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
total += min_red * min_green * min_blue;
|
|
|
|
}
|
|
|
|
|
|
|
|
total
|
|
|
|
}
|
|
|
|
|
2023-12-02 10:00:46 +00:00
|
|
|
fn read_int(line: &[u8]) -> (u64, &[u8]) {
|
|
|
|
let mut num = 0u64;
|
|
|
|
let mut multiplier = 10_000u64;
|
|
|
|
let mut ops_done = 0;
|
|
|
|
|
|
|
|
for i in 0..line.len() {
|
|
|
|
let c = line[i];
|
|
|
|
|
|
|
|
if c.is_ascii_digit() {
|
|
|
|
let c = line[i] - b'0';
|
|
|
|
ops_done += 1;
|
|
|
|
num += multiplier * c as u64;
|
|
|
|
multiplier /= 10;
|
|
|
|
} else if num != 0 {
|
|
|
|
return (num / (10u64.pow(5 - ops_done)), &line[i + 1..]);
|
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
(num / (10u64.pow(5 - ops_done)), line)
|
|
|
|
}
|
|
|
|
|
2023-12-02 05:33:06 +00:00
|
|
|
fn main() {
|
|
|
|
for input in INPUTS.iter() {
|
|
|
|
println!("total = {}", process(input));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[bench]
|
2023-12-04 05:56:08 +00:00
|
|
|
fn part2(b: &mut test::Bencher) {
|
2023-12-02 05:33:06 +00:00
|
|
|
b.iter(|| {
|
|
|
|
let v = process(INPUTS[1]);
|
|
|
|
test::black_box(v);
|
|
|
|
});
|
|
|
|
}
|