summaryrefslogtreecommitdiff
path: root/2020/rust/day10/src/main.rs
blob: 905c525b3e3cb76b357e314046d1cc13f8a05ece (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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
use anyhow::Result;
use std::collections::HashMap;
use std::env::args;
use std::fs::File;
use std::io::{BufRead, BufReader};
use std::path::Path;

fn read_input<T: AsRef<Path>>(filename: T) -> Result<Vec<usize>> {
    let f = File::open(filename)?;
    let reader = BufReader::new(f);

    let values = reader.lines()
        .map(|v| v?.parse::<usize>().map_err(anyhow::Error::new)).collect();

    values
}

fn part1(input: &[usize]) -> Option<usize> {
    let mut sorted = input.to_vec();
    sorted.push(0);
    sorted.sort();
    let max = sorted[sorted.len()-1];
    sorted.push(max+3);
    let (_prev, count) = sorted.iter().fold((None, vec![0, 0, 0, 0]), |acc, jolt| {
        let mut res = acc.1.clone();
        if let Some(prev) = acc.0 {
            let diff = jolt - prev;
            res[diff] += 1;
        }
        (Some(jolt), res)
    });

    Some(count[1] * count[3])
}

fn part2(input: &[usize]) -> Option<u128> {
    let mut sorted = input.to_vec();
    sorted.push(0);
    sorted.sort();
    let max = sorted[sorted.len()-1];
    sorted.push(max+3);
    let mut paths: HashMap<usize, u128> = HashMap::new();
    paths.insert(max+3, 1);
    sorted.iter().rev().fold(0, |_, elem| {
        let count = (1..=3).map(|jump| {
            let pos = *elem + jump;
            if sorted.contains(&pos) {
                paths.get(&pos).copied().unwrap_or(0)
            } else {
                0
            }
        }).sum();
        let lm = paths.entry(*elem).or_insert(0); 
        *lm += count;
        count
    });

    Some(paths[&0])
}

fn main() {
    let ( do_part_1, do_part_2 ) = aoc::do_parts();

    let filename = match args().nth(1) {
        Some(f) => f,
        None => {
            eprintln!("Missing input filename");
            std::process::exit(1);
        },
    };
    match read_input(filename) {
        Ok(input) => {
            if do_part_1 {
                match part1(&input) {
                    Some(solution) => println!("Part1: {}", solution),
                    None => {
                        eprintln!("Part1, no solution found");
                        std::process::exit(1);
                    }
                };
            }
            if do_part_2 {
                match part2(&input) {
                    Some(solution) => println!("Part2: {}", solution),
                    None => {
                        eprintln!("Part2, no solution found");
                        std::process::exit(1);
                    }
                };
            }
        },
        Err(err) => eprintln!("Could not read input: {}", err),
    }
}