6

I messed around in desmos for like 20 mins trying to find an algebraic solution to day 6 and made this. I feel as if this is the best way to solve the problem by graphing f(x) = (x-max_time/2)^2. Finding its y-intercept, finding its vertex (max_time/2) and then finding the minimum x needed to get the max distance by solving for max_distance = f(x).

fn main() {
    let mut raw_input = include_str!("input.txt")
        .lines()
        .map(|f| {

        f.split_ascii_whitespace()
        .skip(1)
        .collect::<String>()
        .parse::<usize>()
        .unwrap()
        
        //for part 1, first parse then collect to a Vec<&str>

    }) ;

    let max_time = raw_input.next().unwrap();
    let max_distance = raw_input.next().unwrap();

    print!("{}", determine_range(max_time, max_distance));

    //let max_times = raw_input.next().unwrap();
    //let max_distances = raw_input.next().unwrap();

    // let races = (0..max_times.len()).map(|i| {
    //     determine_range(max_times[i], max_distances[i])
    // });

    // let total = races.reduce(|acc,x| x*acc).unwrap();
}

fn determine_range(max_time: usize, max_dist: usize) -> f64 {
    let vertex = max_time as f64/2.0;
    let min_y = vertex * vertex - max_dist as f64;
    let min_x = vertex - min_y.sqrt()+ 0.001;

    let mut res = 2.0 * ( (vertex-0.001).floor() - min_x.ceil() + 1.0);
    if vertex.fract() == 0.0 {
        res+=1.0;
    }
    res
}
top 1 comments
sorted by: hot top controversial new old
[-] Mikina@programming.dev 1 points 11 months ago

Here's mine, I think its a similar approach, but the math is a little bit different. Ignore the unnecessary loop from the blatant copypast of multiple races parsing from day one, that was rendered unnecessary by the space replace in input :D

The math was figured out by solving a system of inequalities, for hold time, where (h-t) * h > d, and t, h and d > 0

https://github.com/TheMikina/aoc-2023/blob/main/src/bin/06.rs

this post was submitted on 06 Dec 2023
6 points (100.0% liked)

Advent Of Code

736 readers
1 users here now

An unofficial home for the advent of code community on programming.dev!

Advent of Code is an annual Advent calendar of small programming puzzles for a variety of skill sets and skill levels that can be solved in any programming language you like.

AoC 2023

Solution Threads

M T W T F S S
1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25

Rules/Guidelines

Relevant Communities

Relevant Links

Credits

Icon base by Lorc under CC BY 3.0 with modifications to add a gradient

console.log('Hello World')

founded 1 year ago
MODERATORS