2023-12-02 05:33:06 +00:00
|
|
|
#![feature(test)]
|
|
|
|
extern crate test;
|
|
|
|
|
|
|
|
const INPUTS: [&str; 2] = [include_str!("./sample.txt"), include_str!("./input.txt")];
|
|
|
|
|
2023-12-02 05:41:35 +00:00
|
|
|
fn process(data: &str) -> u64 {
|
2023-12-02 05:33:06 +00:00
|
|
|
let mut total = 0;
|
|
|
|
|
|
|
|
for line in data.lines() {
|
|
|
|
if line.is_empty() {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
let (_, remain) = line.split_once(':').unwrap();
|
|
|
|
|
2023-12-02 05:41:35 +00:00
|
|
|
let moves = remain.split(';').map(|x| {
|
|
|
|
let mut output = 0u64;
|
2023-12-02 05:33:06 +00:00
|
|
|
let cubes = x.split(',');
|
|
|
|
|
|
|
|
for cube in cubes {
|
|
|
|
let cube = cube.trim();
|
|
|
|
let (count, color) = cube.split_once(' ').unwrap();
|
|
|
|
|
2023-12-02 05:41:35 +00:00
|
|
|
let count = count.parse::<u64>().unwrap();
|
2023-12-02 05:33:06 +00:00
|
|
|
|
|
|
|
match color {
|
2023-12-02 05:41:35 +00:00
|
|
|
"red" => output |= count << 32,
|
|
|
|
"green" => output |= count << 16,
|
|
|
|
"blue" => output |= count,
|
2023-12-02 05:33:06 +00:00
|
|
|
_ => unreachable!(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
output
|
|
|
|
});
|
|
|
|
|
|
|
|
let mut min_red = 0;
|
|
|
|
let mut min_green = 0;
|
|
|
|
let mut min_blue = 0;
|
|
|
|
|
|
|
|
for mmove in moves {
|
2023-12-02 05:41:35 +00:00
|
|
|
let red = (mmove & (0xffff << 32)) >> 32;
|
|
|
|
let green = (mmove & (0xffff << 16)) >> 16;
|
|
|
|
let blue = mmove & 0xffff;
|
|
|
|
|
|
|
|
min_red = min_red.max(red);
|
|
|
|
min_green = min_green.max(green);
|
|
|
|
min_blue = min_blue.max(blue);
|
2023-12-02 05:33:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
total += min_red * min_green * min_blue;
|
|
|
|
}
|
|
|
|
|
|
|
|
total
|
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
for input in INPUTS.iter() {
|
|
|
|
println!("total = {}", process(input));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[bench]
|
|
|
|
fn part1(b: &mut test::Bencher) {
|
|
|
|
b.iter(|| {
|
|
|
|
let v = process(INPUTS[1]);
|
|
|
|
test::black_box(v);
|
|
|
|
});
|
|
|
|
}
|