day2: optimized
This commit is contained in:
parent
1e5f230504
commit
eccd47a130
|
@ -3,14 +3,15 @@ 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")];
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug, Copy, Clone)]
|
||||||
pub enum Move {
|
pub enum Move {
|
||||||
Red(u32),
|
Red(u32),
|
||||||
Green(u32),
|
Green(u32),
|
||||||
Blue(u32),
|
Blue(u32),
|
||||||
|
Unknown,
|
||||||
}
|
}
|
||||||
|
|
||||||
fn process(data: &str) -> u32 {
|
fn process(data: &str) -> u64 {
|
||||||
let mut total = 0;
|
let mut total = 0;
|
||||||
|
|
||||||
for line in data.lines() {
|
for line in data.lines() {
|
||||||
|
@ -20,26 +21,24 @@ fn process(data: &str) -> u32 {
|
||||||
let (gid, remain) = line.split_once(':').unwrap();
|
let (gid, remain) = line.split_once(':').unwrap();
|
||||||
|
|
||||||
let gid = gid
|
let gid = gid
|
||||||
.chars()
|
.split_once(' ')
|
||||||
.filter(|c| c.is_numeric())
|
.map(|(_, x)| x.parse::<u64>().unwrap())
|
||||||
.collect::<String>()
|
|
||||||
.parse::<u32>()
|
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
let moves = remain.split(';').map(|x| {
|
let moves = remain.split(';').map(|x| {
|
||||||
let mut output = vec![];
|
let mut output = 0u64;
|
||||||
let cubes = x.split(',');
|
let cubes = x.split(',');
|
||||||
|
|
||||||
for cube in cubes {
|
for cube in cubes {
|
||||||
let cube = cube.trim();
|
let cube = cube.trim();
|
||||||
let (count, color) = cube.split_once(' ').unwrap();
|
let (count, color) = cube.split_once(' ').unwrap();
|
||||||
|
|
||||||
let count = count.parse::<u32>().unwrap();
|
let count = count.parse::<u64>().unwrap();
|
||||||
|
|
||||||
match color {
|
match color {
|
||||||
"red" => output.push(Move::Red(count)),
|
"red" => output |= count << 32,
|
||||||
"green" => output.push(Move::Green(count)),
|
"green" => output |= count << 16,
|
||||||
"blue" => output.push(Move::Blue(count)),
|
"blue" => output |= count,
|
||||||
_ => unreachable!(),
|
_ => unreachable!(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -48,24 +47,14 @@ fn process(data: &str) -> u32 {
|
||||||
});
|
});
|
||||||
|
|
||||||
let mut possible = true;
|
let mut possible = true;
|
||||||
'outer: for mmove in moves {
|
for mmove in moves {
|
||||||
for step in mmove {
|
let red = (mmove & (0xffff << 32)) >> 32;
|
||||||
match step {
|
let green = (mmove & (0xffff << 16)) >> 16;
|
||||||
Move::Red(v) if v > 12 => {
|
let blue = mmove & (0xffff);
|
||||||
possible = false;
|
|
||||||
break 'outer;
|
|
||||||
}
|
|
||||||
Move::Green(v) if v > 13 => {
|
|
||||||
possible = false;
|
|
||||||
break 'outer;
|
|
||||||
}
|
|
||||||
Move::Blue(v) if v > 14 => {
|
|
||||||
possible = false;
|
|
||||||
break 'outer;
|
|
||||||
}
|
|
||||||
|
|
||||||
_ => (),
|
if red > 12 || green > 13 || blue > 14 {
|
||||||
}
|
possible = false;
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if possible {
|
if possible {
|
||||||
|
|
|
@ -3,14 +3,7 @@ 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")];
|
||||||
|
|
||||||
#[derive(Debug)]
|
fn process(data: &str) -> u64 {
|
||||||
pub enum Move {
|
|
||||||
Red(u32),
|
|
||||||
Green(u32),
|
|
||||||
Blue(u32),
|
|
||||||
}
|
|
||||||
|
|
||||||
fn process(data: &str) -> u32 {
|
|
||||||
let mut total = 0;
|
let mut total = 0;
|
||||||
|
|
||||||
for line in data.lines() {
|
for line in data.lines() {
|
||||||
|
@ -19,20 +12,20 @@ fn process(data: &str) -> u32 {
|
||||||
}
|
}
|
||||||
let (_, remain) = line.split_once(':').unwrap();
|
let (_, remain) = line.split_once(':').unwrap();
|
||||||
|
|
||||||
let moves = remain.split(';').flat_map(|x| {
|
let moves = remain.split(';').map(|x| {
|
||||||
let mut output = vec![];
|
let mut output = 0u64;
|
||||||
let cubes = x.split(',');
|
let cubes = x.split(',');
|
||||||
|
|
||||||
for cube in cubes {
|
for cube in cubes {
|
||||||
let cube = cube.trim();
|
let cube = cube.trim();
|
||||||
let (count, color) = cube.split_once(' ').unwrap();
|
let (count, color) = cube.split_once(' ').unwrap();
|
||||||
|
|
||||||
let count = count.parse::<u32>().unwrap();
|
let count = count.parse::<u64>().unwrap();
|
||||||
|
|
||||||
match color {
|
match color {
|
||||||
"red" => output.push(Move::Red(count)),
|
"red" => output |= count << 32,
|
||||||
"green" => output.push(Move::Green(count)),
|
"green" => output |= count << 16,
|
||||||
"blue" => output.push(Move::Blue(count)),
|
"blue" => output |= count,
|
||||||
_ => unreachable!(),
|
_ => unreachable!(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -45,11 +38,13 @@ fn process(data: &str) -> u32 {
|
||||||
let mut min_blue = 0;
|
let mut min_blue = 0;
|
||||||
|
|
||||||
for mmove in moves {
|
for mmove in moves {
|
||||||
match mmove {
|
let red = (mmove & (0xffff << 32)) >> 32;
|
||||||
Move::Red(v) => min_red = std::cmp::max(min_red, v),
|
let green = (mmove & (0xffff << 16)) >> 16;
|
||||||
Move::Green(v) => min_green = std::cmp::max(min_green, v),
|
let blue = mmove & 0xffff;
|
||||||
Move::Blue(v) => min_blue = std::cmp::max(min_blue, v),
|
|
||||||
}
|
min_red = min_red.max(red);
|
||||||
|
min_green = min_green.max(green);
|
||||||
|
min_blue = min_blue.max(blue);
|
||||||
}
|
}
|
||||||
|
|
||||||
total += min_red * min_green * min_blue;
|
total += min_red * min_green * min_blue;
|
||||||
|
|
Loading…
Reference in New Issue
Block a user