summaryrefslogtreecommitdiff
path: root/2021/rust
diff options
context:
space:
mode:
Diffstat (limited to '2021/rust')
-rw-r--r--2021/rust/Cargo.toml28
-rw-r--r--2021/rust/day01/Cargo.toml9
-rw-r--r--2021/rust/day01/src/main.rs82
3 files changed, 119 insertions, 0 deletions
diff --git a/2021/rust/Cargo.toml b/2021/rust/Cargo.toml
new file mode 100644
index 0000000..9674b94
--- /dev/null
+++ b/2021/rust/Cargo.toml
@@ -0,0 +1,28 @@
+[workspace]
+members = [
+ "day01",
+# "day02",
+# "day03",
+# "day04",
+# "day05",
+# "day06",
+# "day07",
+# "day08",
+# "day09",
+# "day10",
+# "day11",
+# "day12",
+# "day13",
+# "day14",
+# "day15",
+# "day16",
+# "day17",
+# "day18",
+# "day19",
+# "day20",
+# "day21",
+# "day22",
+# "day23",
+# "day24",
+# "day25",
+]
diff --git a/2021/rust/day01/Cargo.toml b/2021/rust/day01/Cargo.toml
new file mode 100644
index 0000000..afa6985
--- /dev/null
+++ b/2021/rust/day01/Cargo.toml
@@ -0,0 +1,9 @@
+[package]
+name = "day01"
+version = "0.1.0"
+authors = ["cos <cos>"]
+edition = "2021"
+
+[dependencies]
+aoc = { path = "../../../common/rust/aoc" }
+anyhow = "1.0"
diff --git a/2021/rust/day01/src/main.rs b/2021/rust/day01/src/main.rs
new file mode 100644
index 0000000..a3a6e73
--- /dev/null
+++ b/2021/rust/day01/src/main.rs
@@ -0,0 +1,82 @@
+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| v?.parse().map_err(|err| anyhow!("Could not parse input: {}", err))
+ ).collect()
+}
+
+#[derive(PartialEq)]
+enum Change {
+ NoPrevVal,
+ Increased,
+ Decreased,
+ Identical,
+}
+
+fn count_increases(values: &[usize]) -> usize {
+ let changes: Vec<Change> = values.iter().scan(None, |state, val| {
+ let next = match state {
+ None => Change::NoPrevVal,
+ Some(prev) if val < prev => Change::Decreased,
+ Some(prev) if val > prev => Change::Increased,
+ Some(prev) if val == prev => Change::Identical,
+ Some(_) => unreachable!(),
+ };
+ *state = Some(*val);
+ Some(next)
+ }).collect();
+
+ changes.iter().fold(0, |count, change|
+ if *change == Change::Increased { count + 1 } else { count }
+ )
+}
+
+fn part1(input: &[usize]) -> Result<usize> {
+ let count = count_increases(input);
+
+ Ok(count)
+}
+
+fn part2(input: &[usize]) -> Result<usize> {
+ let slidesums: Vec<_> = input.iter().zip(input[1..].iter()).zip(input[2..].iter()).map(|n| {
+ let ((first, second), third) = n;
+ first + second + third
+ }).collect();
+ let count = count_increases(&slidesums);
+
+ Ok(count)
+}
+
+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, number of increases found to be: {}", solution);
+ }
+ if do_part_2 {
+ let solution = part2(&input).context("No solution for part 2")?;
+ println!("Part2, number of increases found to be: {}", solution);
+ }
+ Ok(())
+}