From dafb8e35b418228da6f6ec343122bde1719e9303 Mon Sep 17 00:00:00 2001 From: Eryn Wells Date: Fri, 16 Dec 2022 00:34:33 +0000 Subject: [PATCH] Move Day 9 to its own crate --- 2022/day9/Cargo.lock | 14 ++++++++++++++ 2022/day9/Cargo.toml | 9 +++++++++ .../data/day9-input.txt => day9/input.txt} | 0 2022/{aoc2022/src/day9.rs => day9/src/main.rs} | 18 ++++++++++-------- 4 files changed, 33 insertions(+), 8 deletions(-) create mode 100644 2022/day9/Cargo.lock create mode 100644 2022/day9/Cargo.toml rename 2022/{aoc2022/data/day9-input.txt => day9/input.txt} (100%) rename 2022/{aoc2022/src/day9.rs => day9/src/main.rs} (90%) diff --git a/2022/day9/Cargo.lock b/2022/day9/Cargo.lock new file mode 100644 index 0000000..d4bc651 --- /dev/null +++ b/2022/day9/Cargo.lock @@ -0,0 +1,14 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "day9" +version = "0.1.0" +dependencies = [ + "geometry", +] + +[[package]] +name = "geometry" +version = "0.1.0" diff --git a/2022/day9/Cargo.toml b/2022/day9/Cargo.toml new file mode 100644 index 0000000..0ae8e12 --- /dev/null +++ b/2022/day9/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "day9" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +geometry = { path = "../geometry" } diff --git a/2022/aoc2022/data/day9-input.txt b/2022/day9/input.txt similarity index 100% rename from 2022/aoc2022/data/day9-input.txt rename to 2022/day9/input.txt diff --git a/2022/aoc2022/src/day9.rs b/2022/day9/src/main.rs similarity index 90% rename from 2022/aoc2022/src/day9.rs rename to 2022/day9/src/main.rs index ac399a8..7108e6f 100644 --- a/2022/aoc2022/src/day9.rs +++ b/2022/day9/src/main.rs @@ -1,7 +1,6 @@ -use crate::grid::{Direction, Point}; +use geometry::{Direction, Point}; use std::collections::HashSet; - -const INPUT: &'static str = include_str!("../../Data/day9-input.txt"); +use std::{env, fs}; type SignedPoint = Point; @@ -19,7 +18,6 @@ impl Rope { fn move_head(&mut self, direction: Direction) { if let Some(head) = self.nodes.first_mut() { head.move_by_one_in(direction); - println!("{}", &head); for i in 1..self.nodes.len() { let first = self.nodes[i - 1].clone(); let mut second = self.nodes.get_mut(i).unwrap(); @@ -59,14 +57,20 @@ impl Rope { } } -pub fn main(_filename: &str) -> std::io::Result<()> { +pub fn main() { + let args: Vec = env::args().collect(); + + let filename = args.get(1).expect("Missing filename argument"); + + let file_contents = fs::read_to_string(&filename).expect("Unable to read file"); + let mut visited_points: HashSet = HashSet::new(); let mut long_rope_visited_points: HashSet = HashSet::new(); let mut rope = Rope::with_length(2); let mut long_rope = Rope::with_length(10); - for line in INPUT.lines() { + for line in file_contents.lines() { let mut split_line = line.split(" "); let direction = Direction::from_relative_direction(split_line.next().unwrap()).unwrap(); @@ -93,6 +97,4 @@ pub fn main(_filename: &str) -> std::io::Result<()> { "Part 2: number of points tail node of long rope visited: {}", &long_rope_visited_points.len() ); - - Ok(()) }