From 6acbdc53f1429d7fcf7c8e8c2931b19534cee975 Mon Sep 17 00:00:00 2001 From: Vladan Popovic Date: Fri, 13 Dec 2019 13:57:43 +0100 Subject: [PATCH] Partial / not complete impl --- src/day_three.rs | 37 ++++++++++++++++++++++++++----------- 1 file changed, 26 insertions(+), 11 deletions(-) diff --git a/src/day_three.rs b/src/day_three.rs index 30459d0..5b13a59 100644 --- a/src/day_three.rs +++ b/src/day_three.rs @@ -1,6 +1,5 @@ use std::collections::BTreeSet; -use std::fs::File; -use std::io::{self, BufRead, BufReader}; +use std::io; use std::iter::{repeat, successors}; fn next(c: (i32, i32), direction: &str) -> (i32, i32) { @@ -14,8 +13,6 @@ fn next(c: (i32, i32), direction: &str) -> (i32, i32) { } fn direction_length(entry: String) -> (String, usize) { - /// Creates a (direction, length) pair to later produce a string - /// on a direction with the given length. E.g. given "R10" the output of ( entry.chars().nth(0).unwrap().to_string(), entry[1..].parse().unwrap(), @@ -46,19 +43,25 @@ fn distance(position: (i32, i32)) -> i32 { position.0.abs() + position.1.abs() } -pub fn main() -> io::Result<()> { - let f = BufReader::new(File::open("day3-input.txt")?); - let entries: Vec> = f - .lines() - .map(|line| line.unwrap().split(",").map(String::from).collect()) +fn closest(input: String) -> Option { + let entries: Vec> = input + .trim() + .split("\n") + .map(|line| line.split(",").map(String::from).collect()) .map(to_coordinates) .collect(); - let closest: Option = entries[0] + entries[0] .intersection(&entries[1]) .cloned() .map(distance) - .min(); + .min() +} + +fn first(input: String) -> Option {} + +pub fn main() -> io::Result<()> { + let closest = closest(std::include_str!("../day3-input.txt").to_owned()); println!("{:?}", closest); Ok(()) @@ -80,3 +83,15 @@ fn test_coordinates_for_entry() { let input = "R2,U2".to_owned().split(",").map(String::from).collect(); assert_eq!(to_coordinates(input), expected); } + +#[test] +fn test_part_one() { + let input = "R75,D30,R83,U83,L12,D49,R71,U7,L72\n\ + U62,R66,U55,R34,D71,R55,D58,R83" + .to_owned(); + assert_eq!(closest(input), Some(159)); + let input = "R98,U47,R26,D63,R33,U87,L62,D20,R33,U53,R51\n\ + U98,R91,D20,R16,D67,R40,U7,R15,U6,R7" + .to_owned(); + assert_eq!(closest(input), Some(135)); +}