summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/2020/mod.rs33
-rw-r--r--src/main.rs24
2 files changed, 55 insertions, 2 deletions
diff --git a/src/2020/mod.rs b/src/2020/mod.rs
new file mode 100644
index 0000000..87c213b
--- /dev/null
+++ b/src/2020/mod.rs
@@ -0,0 +1,33 @@
+use anyhow::Context as _;
+use std::io::BufRead as _;
+
+pub fn run(day: u8, puzzle: u8) -> anyhow::Result<()> {
+ match (day, puzzle) {
+ (1, 1) => report_repair(),
+ _ => Err(anyhow::anyhow!("unknown puzzle {}-{}", day, puzzle)),
+ }
+}
+
+fn report_repair() -> anyhow::Result<()> {
+ let f = std::fs::File::open("data/1-1.txt")
+ .context("couldn't find data file")?;
+ let f = std::io::BufReader::new(f);
+ let ints: anyhow::Result<Vec<i32>> = f
+ .lines()
+ .map(|l| {
+ l.context("failed to read a line")?
+ .parse()
+ .context("failed to parse line into an integer")
+ })
+ .collect();
+ let ints = ints?;
+ for i in &ints {
+ for j in &ints {
+ if i + j == 2020 {
+ println!("{} + {} = {}", i, j, i * j);
+ return Ok(());
+ }
+ }
+ }
+ Err(anyhow::anyhow!("no numbers summing to 2020 found"))
+}
diff --git a/src/main.rs b/src/main.rs
index e7a11a9..b8677cb 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -1,3 +1,23 @@
-fn main() {
- println!("Hello, world!");
+#[path = "2020/mod.rs"]
+mod year2020;
+
+#[derive(Debug, structopt::StructOpt)]
+#[structopt(about = "Advent of Code")]
+enum Opt {
+ #[structopt(name = "2020")]
+ Year2020 { day: u8, puzzle: u8 },
+}
+
+#[paw::main]
+fn main(opt: Opt) {
+ let res = match opt {
+ Opt::Year2020 { day, puzzle } => crate::year2020::run(day, puzzle),
+ };
+ match res {
+ Ok(()) => {}
+ Err(e) => {
+ eprintln!("{}", e);
+ std::process::exit(1);
+ }
+ }
}