y2015d20
This commit is contained in:
parent
261a2e89c9
commit
aa28df24b3
14
y2015/src/bin/d20.rs
Normal file
14
y2015/src/bin/d20.rs
Normal file
@ -0,0 +1,14 @@
|
||||
use y2015::days::d20;
|
||||
|
||||
fn main() {
|
||||
part1();
|
||||
part2();
|
||||
}
|
||||
|
||||
fn part1() {
|
||||
println!("{}", d20::process_part1(36000000));
|
||||
}
|
||||
|
||||
fn part2() {
|
||||
println!("{}", d20::process_part2(36000000));
|
||||
}
|
70
y2015/src/days/d20.rs
Normal file
70
y2015/src/days/d20.rs
Normal file
@ -0,0 +1,70 @@
|
||||
pub fn process_part1(input: u32) -> u32 {
|
||||
// slow
|
||||
//let mut house = 1;
|
||||
//loop {
|
||||
// let mut presents = 0;
|
||||
// for elf in 1..=house {
|
||||
// if house % elf == 0 {
|
||||
// presents += elf * 10;
|
||||
// println!("{house} {presents}");
|
||||
// }
|
||||
// }
|
||||
// if presents >= input {
|
||||
// break;
|
||||
// }
|
||||
// house += 1;
|
||||
//}
|
||||
//house
|
||||
let mut house = 0;
|
||||
loop {
|
||||
house += 1;
|
||||
let mut divisors = get_divisors(house);
|
||||
divisors.push(house);
|
||||
if divisors.iter().sum::<usize>() * 10 >= input as usize {
|
||||
break;
|
||||
}
|
||||
}
|
||||
house as u32
|
||||
}
|
||||
|
||||
fn get_divisors(n: usize) -> Vec<usize> {
|
||||
let mut divisors = vec![1];
|
||||
let mut potential_divisor = 2;
|
||||
while (potential_divisor * potential_divisor) < n {
|
||||
if n % potential_divisor == 0 {
|
||||
divisors.push(potential_divisor);
|
||||
divisors.push(n / potential_divisor);
|
||||
}
|
||||
potential_divisor += 1;
|
||||
}
|
||||
// This almost made me go mad
|
||||
if potential_divisor * potential_divisor == n {
|
||||
divisors.push(potential_divisor)
|
||||
}
|
||||
divisors
|
||||
}
|
||||
|
||||
pub fn process_part2(input: u32) -> u32 {
|
||||
let mut house = 0;
|
||||
loop {
|
||||
house += 1;
|
||||
let mut divisors = get_divisors(house);
|
||||
divisors.push(house);
|
||||
let sum = divisors.iter().filter(|&&d| house / d <= 50).sum::<usize>();
|
||||
if sum * 11 >= input as usize {
|
||||
break;
|
||||
}
|
||||
}
|
||||
house as u32
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn part1() {
|
||||
let result = process_part1(100);
|
||||
assert_eq!(result, 6);
|
||||
}
|
||||
}
|
@ -18,3 +18,5 @@ pub mod d8;
|
||||
pub mod d9;
|
||||
|
||||
pub mod d19;
|
||||
|
||||
pub mod d20;
|
||||
|
Loading…
Reference in New Issue
Block a user