Partial / not complete impl

This commit is contained in:
Vladan Popovic 2019-12-13 13:57:43 +01:00
parent f22ef7f7ba
commit 6acbdc53f1
1 changed files with 26 additions and 11 deletions

View File

@ -1,6 +1,5 @@
use std::collections::BTreeSet; use std::collections::BTreeSet;
use std::fs::File; use std::io;
use std::io::{self, BufRead, BufReader};
use std::iter::{repeat, successors}; use std::iter::{repeat, successors};
fn next(c: (i32, i32), direction: &str) -> (i32, i32) { 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) { 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.chars().nth(0).unwrap().to_string(),
entry[1..].parse().unwrap(), entry[1..].parse().unwrap(),
@ -46,19 +43,25 @@ fn distance(position: (i32, i32)) -> i32 {
position.0.abs() + position.1.abs() position.0.abs() + position.1.abs()
} }
pub fn main() -> io::Result<()> { fn closest(input: String) -> Option<i32> {
let f = BufReader::new(File::open("day3-input.txt")?); let entries: Vec<BTreeSet<(i32, i32)>> = input
let entries: Vec<BTreeSet<(i32, i32)>> = f .trim()
.lines() .split("\n")
.map(|line| line.unwrap().split(",").map(String::from).collect()) .map(|line| line.split(",").map(String::from).collect())
.map(to_coordinates) .map(to_coordinates)
.collect(); .collect();
let closest: Option<i32> = entries[0] entries[0]
.intersection(&entries[1]) .intersection(&entries[1])
.cloned() .cloned()
.map(distance) .map(distance)
.min(); .min()
}
fn first(input: String) -> Option<i32> {}
pub fn main() -> io::Result<()> {
let closest = closest(std::include_str!("../day3-input.txt").to_owned());
println!("{:?}", closest); println!("{:?}", closest);
Ok(()) Ok(())
@ -80,3 +83,15 @@ fn test_coordinates_for_entry() {
let input = "R2,U2".to_owned().split(",").map(String::from).collect(); let input = "R2,U2".to_owned().split(",").map(String::from).collect();
assert_eq!(to_coordinates(input), expected); 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));
}