summaryrefslogtreecommitdiff
path: root/bin/init_rust
blob: 6425d638a9c32fd844f2c0dad509a1572748c0cb (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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#!/bin/sh -eu

usage() {
  print "%s\n\n%s\n%s\n%s\n" "$0 [-h] [-y <year>] [-d <day>]" \
      '-h | --help   Output this help.' \
      '-y | --help   Use another year than the current one.' \
      '-d | --help   Use another day than the current one.'
}

_opts=$( getopt --options "y:d:h" --long 'year:,day:,help' -- "$@"  )

eval set -- "${_opts}"
unset _opts

while [ $1 != '--' ]; do
  case "$1" in
    '-d' | '--day')
      shift
      _day="$1"
      ;;
    '-y' | '--year')
      shift
      _year="$1"
      ;;
    '-h' | '--help')
      usage
      exit 0
      ;;
  esac
  shift
done

_year=${_year:-$(date +%Y)}
[ "${_day:-}" ] || _day=${_day:-$(TZ=EST date '+%d')}

[ "${#_day}" != 1 ] || _day="0${_day}"

_cargo_contents=$( sed "s/^#\(.*${_day}\)/\1/" <"${_year}/rust/Cargo.toml")
echo "${_cargo_contents}" >"${_year}/rust/Cargo.toml"

cd "${_year}/rust"
mkdir "day${_day}" || :
cd "day${_day}"

cargo init --bin

cat > 'Cargo.toml' << END_OF_TOML
[package]
name = "day${_day}"
version = "0.1.0"
edition = "2021"

[dependencies]
aoc = { path = "../../../common/rust/aoc" }
anyhow = "1.0"
END_OF_TOML

cat > 'src/main.rs' << END_OF_SRC
use {
    anyhow::{
        anyhow,
        Context,
        Result,
    },
    std::{
        env::args,
        fs::File,
        io::{
            BufRead,
            BufReader,
        },
        path::Path,
    },
};

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

    reader.lines().map(
        |v| {
            let s = v?;
            let n = s.parse()?;
            Ok(n)
        }
    ).collect()
}

fn part1<'a, I: IntoIterator<Item = &'a usize>>(_input: I) -> Result<usize> {
    Ok(0)
}

fn part2<'a, I: IntoIterator<Item = &'a usize>>(_input: I) -> Result<usize> {
    Ok(0)
}

fn main() -> Result<()> {
    let ( do_part_1, do_part_2 ) = aoc::do_parts();

    let filename = args().nth(1).ok_or(anyhow!("Missing input filename"))?;
    let input = read_input(filename).context("Could not read input")?;
    if do_part_1 {
        let solution = part1(&input).context("No solution for part 1")?;
        println!("Part1, solution found to be: {}", solution);
    }
    if do_part_2 {
        let solution = part2(&input).context("No solution for part 2")?;
        println!("Part2, solution found to be: {}", solution);
    }
    Ok(())
}
END_OF_SRC

unset _cargo_contents _day _year