Move Day 9 to its own crate

This commit is contained in:
Eryn Wells 2022-12-16 00:34:33 +00:00
parent f6793c685e
commit dafb8e35b4
4 changed files with 33 additions and 8 deletions

14
2022/day9/Cargo.lock generated Normal file
View file

@ -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"

9
2022/day9/Cargo.toml Normal file
View file

@ -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" }

View file

@ -1,7 +1,6 @@
use crate::grid::{Direction, Point}; use geometry::{Direction, Point};
use std::collections::HashSet; use std::collections::HashSet;
use std::{env, fs};
const INPUT: &'static str = include_str!("../../Data/day9-input.txt");
type SignedPoint = Point; type SignedPoint = Point;
@ -19,7 +18,6 @@ impl Rope {
fn move_head(&mut self, direction: Direction) { fn move_head(&mut self, direction: Direction) {
if let Some(head) = self.nodes.first_mut() { if let Some(head) = self.nodes.first_mut() {
head.move_by_one_in(direction); head.move_by_one_in(direction);
println!("{}", &head);
for i in 1..self.nodes.len() { for i in 1..self.nodes.len() {
let first = self.nodes[i - 1].clone(); let first = self.nodes[i - 1].clone();
let mut second = self.nodes.get_mut(i).unwrap(); 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<String> = 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<SignedPoint> = HashSet::new(); let mut visited_points: HashSet<SignedPoint> = HashSet::new();
let mut long_rope_visited_points: HashSet<SignedPoint> = HashSet::new(); let mut long_rope_visited_points: HashSet<SignedPoint> = HashSet::new();
let mut rope = Rope::with_length(2); let mut rope = Rope::with_length(2);
let mut long_rope = Rope::with_length(10); 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 mut split_line = line.split(" ");
let direction = Direction::from_relative_direction(split_line.next().unwrap()).unwrap(); 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: {}", "Part 2: number of points tail node of long rope visited: {}",
&long_rope_visited_points.len() &long_rope_visited_points.len()
); );
Ok(())
} }