Day 2
authorGreg Burri <greg.burri@gmail.com>
Fri, 2 Dec 2022 20:32:15 +0000 (21:32 +0100)
committerGreg Burri <greg.burri@gmail.com>
Fri, 2 Dec 2022 20:32:15 +0000 (21:32 +0100)
Cargo.toml
src/day01.rs
src/day02.rs [new file with mode: 0644]
src/day03.rs [new file with mode: 0644]
src/main.rs

index bf82e1f..0d0f199 100644 (file)
@@ -11,4 +11,9 @@ itertools = "0.10"
 threadpool = "1.8"
 regex = "1"
 num = "0.4"
-num_enum = "0.5"
\ No newline at end of file
+num_enum = "0.5"
+
+[profile.release]
+opt-level = 3
+lto = true
+codegen-units = 1
\ No newline at end of file
index d88d000..0a397c0 100644 (file)
@@ -24,10 +24,9 @@ pub fn get_most_calories(calories: &[i64]) -> i64 {
 }
 
 pub fn get_sum_most_three_calories(calories: &[i64]) -> i64 {
-    calories.iter().map(Reverse).k_smallest(3).map(|n| n.0).sum()
+    calories.iter().map(Reverse).k_smallest(3).fold(0, |s, v| s + v.0)
 }
 
-
 #[cfg(test)]
 mod tests {
     use super::*;
diff --git a/src/day02.rs b/src/day02.rs
new file mode 100644 (file)
index 0000000..cebdb21
--- /dev/null
@@ -0,0 +1,89 @@
+use std::io::BufRead;
+
+#[derive(PartialEq, Clone, Copy)]
+pub enum Shape {
+    Rock,
+    Paper,
+    Scissors,
+}
+
+impl Shape {
+    pub fn battle(&self, other: &Shape) -> i32 {
+        (match (self, other) {
+            (Shape::Rock, Shape::Scissors) |
+            (Shape::Paper, Shape::Rock) |
+            (Shape::Scissors, Shape::Paper) => 6,
+            (a, b) if a == b => 3,
+            _ => 0,
+        }) +
+        match self { Shape::Rock => 1, Shape::Paper => 2, Shape::Scissors => 3, }
+    }
+
+    pub fn parse(str: &str) -> Self {
+        match str {
+            "A" | "X" => Shape::Rock,
+            "B" | "Y" => Shape::Paper,
+            "C" | "Z" => Shape::Scissors,
+            _ => panic!("Unknown letter: {}", str)
+        }
+    }
+}
+
+pub fn read_shapes<R>(reader: R) -> Vec<(Shape, Shape)>
+where
+    R: BufRead
+{
+    let mut shapes: Vec<(Shape, Shape)> = Vec::new();
+    for l in reader.lines() {
+        let s: Vec<Shape> = l.unwrap().trim().split(' ').map(Shape::parse).collect();
+        shapes.push((s[0], s[1]));
+    }
+    shapes
+}
+
+pub fn read_shapes_2<R>(reader: R) -> Vec<(Shape, Shape)>
+where
+    R: BufRead
+{
+    let mut shapes: Vec<(Shape, Shape)> = Vec::new();
+    for l in reader.lines() {
+        let l = l.unwrap();
+        let l: Vec<&str> = l.trim().split(' ').collect();
+        let s1: Shape = Shape::parse(l[0]);
+        let s2 = match l[1] {
+            "X" => match s1 { Shape::Rock => Shape::Scissors, Shape::Paper => Shape::Rock, Shape::Scissors => Shape::Paper, }, // Need to lose.
+            "Z" => match s1 { Shape::Rock => Shape::Paper, Shape::Paper => Shape::Scissors, Shape::Scissors => Shape::Rock, }, // Need to win
+            _ => s1, // Draw.
+        };
+
+        shapes.push((s1, s2));
+    }
+    shapes
+}
+
+pub fn get_score(shapes: &[(Shape, Shape)]) -> i32 {
+    shapes.iter().fold(0, |sum, (s1, s2)| sum + s2.battle(s1))
+}
+
+#[cfg(test)]
+mod tests {
+    use super::*;
+
+    #[test]
+    fn part1() {
+        let stategy_guide =
+            "A Y
+             B X
+             C Z";
+        assert_eq!(get_score(&read_shapes(stategy_guide.as_bytes())), 15);
+    }
+
+    #[test]
+    fn part2() {
+        let stategy_guide =
+            "A Y
+             B X
+             C Z";
+        assert_eq!(get_score(&read_shapes_2(stategy_guide.as_bytes())), 12);
+    }
+}
\ No newline at end of file
diff --git a/src/day03.rs b/src/day03.rs
new file mode 100644 (file)
index 0000000..fb593c2
--- /dev/null
@@ -0,0 +1,31 @@
+use std::io::BufRead;
+
+pub fn read_<R>(reader: R) -> Vec<i32>
+where
+    R: BufRead
+{
+    /*
+    let mut shapes: Vec<(Shape, Shape)> = Vec::new();
+    for l in reader.lines() {
+        let s: Vec<Shape> = l.unwrap().trim().split(' ').map(Shape::parse).collect();
+        shapes.push((s[0], s[1]));
+    }
+    shapes
+     */
+    Vec::new()
+}
+
+#[cfg(test)]
+mod tests {
+    use super::*;
+
+    #[test]
+    fn part1() {
+        assert_eq!(1, 1);
+    }
+
+    #[test]
+    fn part2() {
+        assert_eq!(2, 2);
+    }
+}
\ No newline at end of file
index a236cec..6a78796 100644 (file)
@@ -1,15 +1,28 @@
-use std::{env, fs, io, time::Instant};
+use std::{env, fs, time::Instant, io::{BufReader, Seek, SeekFrom}};
 
 mod common;
 mod day01;
+mod day02;
+mod day03;
 
 fn day01() -> String {
-    //let report = common::read_list_of_numbers("data/day01.input", "\n");
     let f = fs::File::open("data/day01.input").unwrap();
-    let calories = day01::read_calories(std::io::BufReader::new(f));
+    let calories = day01::read_calories(BufReader::new(f));
     format!("part1: {}, part2: {}", day01::get_most_calories(&calories), day01::get_sum_most_three_calories(&calories))
 }
 
+fn day02() -> String {
+    let mut f = fs::File::open("data/day02.input").unwrap();
+    let shapes = day02::read_shapes(BufReader::new(f.try_clone().unwrap()));
+    let _ = f.seek(SeekFrom::Start(0));
+    let shapes_2 = day02::read_shapes_2(BufReader::new(f));
+    format!("part1: {}, part2: {}", day02::get_score(&shapes), day02::get_score(&shapes_2))
+}
+
+fn day03() -> String {
+    format!("part1: {}, part2: {}", 0, 0)
+}
+
 fn format_micros(t: u128) -> String {
     if t < 10_000 {
         format!("{} μs", t)
@@ -30,6 +43,8 @@ fn main() {
 
     let days: Vec<fn() -> String> = vec!(
         day01,
+        day02,
+        day03,
     );
 
     let args: Vec<String> = env::args().skip(1).collect();