Quadratics are fun
This commit is contained in:
parent
81f604249c
commit
c15d9534bd
1 changed files with 23 additions and 14 deletions
|
@ -8,7 +8,7 @@ pub struct Day6 {
|
||||||
impl Day for Day6 {
|
impl Day for Day6 {
|
||||||
fn part1(&mut self) -> String {
|
fn part1(&mut self) -> String {
|
||||||
let lines = self.input.lines().map(|l| l.split_ascii_whitespace().skip(1).collect::<Vec<_>>()).collect::<Vec<_>>();
|
let lines = self.input.lines().map(|l| l.split_ascii_whitespace().skip(1).collect::<Vec<_>>()).collect::<Vec<_>>();
|
||||||
let races = [
|
let races: [(f64, f64); 4] = [
|
||||||
(lines[0][0].parse().unwrap(), lines[1][0].parse().unwrap()),
|
(lines[0][0].parse().unwrap(), lines[1][0].parse().unwrap()),
|
||||||
(lines[0][1].parse().unwrap(), lines[1][1].parse().unwrap()),
|
(lines[0][1].parse().unwrap(), lines[1][1].parse().unwrap()),
|
||||||
(lines[0][2].parse().unwrap(), lines[1][2].parse().unwrap()),
|
(lines[0][2].parse().unwrap(), lines[1][2].parse().unwrap()),
|
||||||
|
@ -18,13 +18,16 @@ impl Day for Day6 {
|
||||||
let mut acc = 1usize;
|
let mut acc = 1usize;
|
||||||
|
|
||||||
for (time, record_distance) in races {
|
for (time, record_distance) in races {
|
||||||
let mut wins = 0usize;
|
// let mut wins = 0usize;
|
||||||
for time_holding_button in 0..time {
|
// for time_holding_button in 0..time {
|
||||||
let distance = (time - time_holding_button) * time_holding_button /* velocity */;
|
// let distance = (time - time_holding_button) * time_holding_button /* velocity */;
|
||||||
if distance > record_distance { wins += 1; }
|
// if distance > record_distance { wins += 1; }
|
||||||
}
|
// }
|
||||||
|
// Mathy solution:
|
||||||
|
let b = ((time+((time*time-4.0*record_distance).sqrt())) / 2.0).ceil();
|
||||||
|
let a = ((time-((time*time-4.0*record_distance).sqrt())) / 2.0).ceil();
|
||||||
|
|
||||||
acc *= wins;
|
acc *= (b - a) as usize;
|
||||||
}
|
}
|
||||||
|
|
||||||
acc.to_string()
|
acc.to_string()
|
||||||
|
@ -33,13 +36,19 @@ impl Day for Day6 {
|
||||||
fn part2(&mut self) -> String {
|
fn part2(&mut self) -> String {
|
||||||
let lines = self.input.lines().map(|l| l[10..].split_ascii_whitespace().collect::<String>()).collect::<Vec<_>>();
|
let lines = self.input.lines().map(|l| l[10..].split_ascii_whitespace().collect::<String>()).collect::<Vec<_>>();
|
||||||
|
|
||||||
let (time, record_distance): (u128, u128) = (lines[0].parse().unwrap(), lines[1].parse().unwrap());
|
let (time, record_distance): (f64, f64) = (lines[0].parse().unwrap(), lines[1].parse().unwrap());
|
||||||
let mut wins = 0u128;
|
// Boring solution:
|
||||||
for time_holding_button in 0..time {
|
// let mut wins = 0u128;
|
||||||
let distance = (time - time_holding_button) * time_holding_button /* velocity */;
|
// for time_holding_button in 0..time {
|
||||||
if distance > record_distance { wins += 1; }
|
// let distance = (time - time_holding_button) * time_holding_button /* velocity */;
|
||||||
}
|
// if distance > record_distance { wins += 1; }
|
||||||
|
// }
|
||||||
|
|
||||||
wins.to_string()
|
// wins.to_string()
|
||||||
|
// Mathy solution:
|
||||||
|
let b = ((time+((time*time-4.0*record_distance).sqrt())) / 2.0).ceil();
|
||||||
|
let a = ((time-((time*time-4.0*record_distance).sqrt())) / 2.0).ceil();
|
||||||
|
|
||||||
|
(b - a).to_string()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue