summaryrefslogtreecommitdiff
path: root/2023/day06.rs
blob: 839c798b4f9de459476abf30f509679acf12ac96 (plain)
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#![allow(non_snake_case)]
use std::fs::File;
use std::io::Read;

fn get_opts(t: u64, d: u64) -> u64 {
    let t = t as f64;
    let d = d as f64;
    let D = f64::sqrt((t * t - 4. * d) as f64);
    let x1 = ((-t - D) / 2.).floor();
    let x2 = ((-t + D) / 2.).floor();
    (x2 - x1) as u64
}

fn main() {
    // let filename = "in/day06.ref";
    let filename = "in/day06.pzl";

    let mut f = File::open(filename).expect("cannot open file");
    let mut content = String::new();
    f.read_to_string(&mut content).expect("cannot read file");

    let mut lines = content.trim_end().split('\n');
    let parse1 = |l: &str| {
        l.split_once(":")
            .unwrap()
            .1
            .split(" ")
            .filter_map(|x| x.parse::<u64>().ok())
            .collect::<Vec<u64>>()
    };
    let time = parse1(lines.next().unwrap());
    let dist = parse1(lines.next().unwrap());
    let res1 = time
        .into_iter()
        .zip(dist.into_iter())
        .map(|(t, d)| get_opts(t, d))
        .fold(1, |acc, x| acc * x);

    let mut lines = content.trim_end().split('\n');
    let parse2 = |l: &str| {
        l.split_once(":")
            .unwrap()
            .1
            .split_whitespace()
            .collect::<Vec<_>>()
            .join("")
            .parse()
            .unwrap()
    };
    let time = parse2(lines.next().unwrap());
    let dist = parse2(lines.next().unwrap());
    let res2 = get_opts(time, dist);

    println!("res1: {}", res1);
    println!("res2: {}", res2);
    assert_eq!(res1, 220320);
    assert_eq!(res2, 34454850);
}