day6: optimized with quadratic inequality
This commit is contained in:
parent
9787958cc4
commit
0171ee6758
|
@ -2,48 +2,65 @@
|
||||||
|
|
||||||
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) -> i64 {
|
fn process(data: &[u8]) -> u64 {
|
||||||
let mut data = data.lines();
|
let mut data = data.split(|&x| x == b'\n');
|
||||||
|
|
||||||
let time = data
|
let time = data
|
||||||
.next()
|
.next()
|
||||||
.map(|line| {
|
.map(|line| {
|
||||||
line.split(' ')
|
line.split(|&x| x == b' ')
|
||||||
.filter(|x| !x.is_empty())
|
|
||||||
.skip(1)
|
.skip(1)
|
||||||
.map(|x| x.parse::<u64>().unwrap())
|
.filter(|c| !c.is_empty())
|
||||||
|
.map(|c| {
|
||||||
|
let mut num = 0;
|
||||||
|
let mut mul = 1;
|
||||||
|
|
||||||
|
for c in c.iter().rev() {
|
||||||
|
num += (c - b'0') as u64 * mul;
|
||||||
|
mul *= 10;
|
||||||
|
}
|
||||||
|
|
||||||
|
num as f32
|
||||||
|
})
|
||||||
})
|
})
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
let distance = data
|
let distance = data
|
||||||
.next()
|
.next()
|
||||||
.map(|line| {
|
.map(|line| {
|
||||||
line.split(' ')
|
line.split(|&x| x == b' ')
|
||||||
.filter(|x| !x.is_empty())
|
|
||||||
.skip(1)
|
.skip(1)
|
||||||
.map(|x| x.parse::<u64>().unwrap())
|
.filter(|c| !c.is_empty())
|
||||||
|
.map(|c| {
|
||||||
|
let mut num = 0;
|
||||||
|
let mut mul = 1;
|
||||||
|
|
||||||
|
for c in c.iter().rev() {
|
||||||
|
num += (c - b'0') as u64 * mul;
|
||||||
|
mul *= 10;
|
||||||
|
}
|
||||||
|
|
||||||
|
num as f32
|
||||||
|
})
|
||||||
})
|
})
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
let mut answer = 1;
|
let mut answer = 1;
|
||||||
|
|
||||||
for (time, dist) in time.zip(distance) {
|
for (time, dist) in time.zip(distance) {
|
||||||
let mut ways = 0;
|
let mut root1 = (time + (time * time - 4.0 * dist).sqrt()) / 2.0;
|
||||||
|
let root2 = (time - (time * time - 4.0 * dist).sqrt()) / 2.0;
|
||||||
|
|
||||||
for t in 0..time {
|
if root1.fract() == 0.0 && root2.fract() == 0.0 {
|
||||||
let time_left = time - t;
|
root1 -= 1.0;
|
||||||
let speed = t;
|
|
||||||
|
|
||||||
let dist_traveled = time_left * speed;
|
|
||||||
|
|
||||||
if dist_traveled > dist {
|
|
||||||
ways += 1;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
answer *= ways;
|
answer *= root1 as u64 - root2 as u64;
|
||||||
}
|
}
|
||||||
|
|
||||||
answer
|
answer
|
||||||
|
|
|
@ -2,42 +2,46 @@
|
||||||
|
|
||||||
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) -> i64 {
|
fn process(data: &[u8]) -> u64 {
|
||||||
let mut data = data.lines();
|
let mut data = data.split(|&x| x == b'\n');
|
||||||
|
|
||||||
let time: u64 = data
|
let time: f64 = data
|
||||||
.next()
|
.next()
|
||||||
.map(|line| {
|
.map(|line| {
|
||||||
let x: String = line.chars().filter(|c| c.is_numeric()).collect();
|
let mut num = 0;
|
||||||
|
let mut mul = 1;
|
||||||
x.parse::<u64>().unwrap()
|
for c in line.iter().filter(|c| c.is_ascii_digit()).rev() {
|
||||||
|
num += (c - b'0') as u64 * mul;
|
||||||
|
mul *= 10;
|
||||||
|
}
|
||||||
|
num as f64
|
||||||
})
|
})
|
||||||
.unwrap_or(0);
|
.unwrap_or(0.0);
|
||||||
let distance: u64 = data
|
let distance: f64 = data
|
||||||
.next()
|
.next()
|
||||||
.map(|line| {
|
.map(|line| {
|
||||||
let x: String = line.chars().filter(|c| c.is_numeric()).collect();
|
let mut num = 0;
|
||||||
|
let mut mul = 1;
|
||||||
x.parse::<u64>().unwrap()
|
for c in line.iter().filter(|c| c.is_ascii_digit()).rev() {
|
||||||
|
num += (c - b'0') as u64 * mul;
|
||||||
|
mul *= 10;
|
||||||
|
}
|
||||||
|
num as f64
|
||||||
})
|
})
|
||||||
.unwrap_or(0);
|
.unwrap_or(0.0);
|
||||||
|
|
||||||
let mut answer = 0;
|
let mut root1 = (time + (time.powi(2) - 4.0 * distance).sqrt()) / 2.0;
|
||||||
|
let root2 = (time - (time.powi(2) - 4.0 * distance).sqrt()) / 2.0;
|
||||||
for t in 0..time {
|
if root1.fract() == 0.0 && root2.fract() == 0.0 {
|
||||||
let time_left = time - t;
|
root1 -= 1.0;
|
||||||
let speed = t;
|
|
||||||
|
|
||||||
let dist_traveled = time_left * speed;
|
|
||||||
|
|
||||||
if dist_traveled > distance {
|
|
||||||
answer += 1;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
answer
|
root1 as u64 - root2 as u64
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
|
|
Loading…
Reference in New Issue
Block a user