summaryrefslogtreecommitdiff
path: root/2023/day06.rs
blob: 92a631a4e0eff17ed1e6b483f1e2613c732081de (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
59
60
61
#![allow(dead_code)]
#![allow(unused_variables)]
#![allow(unused_mut)]
use std::fs::File;
use std::io::Read;

fn get_opts(t: u64, d: u64) -> u64 {
    let mut opts = 0;
    for wait in 0..t {
        if wait * (t - wait) > d {
            opts += 1;
        }
    }
    return opts;
}

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);
}