use std::fs::File; use std::io::{self, BufRead}; use std::path::Path; use std::collections::HashMap; fn main() { let file_path = String::from("input"); let mut positions = HashMap::<(i32, i32), u32>::new(); let mut tail: (i32, i32) = (0, 0); let mut head: (i32, i32) = (0, 0); *positions.entry(tail).or_insert(0) += 1; println!("In file {}", file_path); if let Ok(lines) = read_lines(file_path) { // Consumes the iterator, returns an (Optional) String for line in lines { if let Ok(ip) = line { let split: Vec<&str> = ip.split(" ").collect(); let dir = split[0]; let steps: usize = split[1].parse().unwrap(); if dir == "U" { for _ in 0..steps { println!("U {}", steps); head.1 += 1; if tail.1 - head.1 == -2 { println!("U move {}", steps); tail.0 = head.0; tail.1 += 1; *positions.entry(tail).or_insert(0) += 1; } } } else if dir == "D" { for _ in 0..steps { println!("D {}", steps); head.1 -= 1; if tail.1 - head.1 == 2 { println!("D move {}", steps); tail.0 = head.0; tail.1 -= 1; *positions.entry(tail).or_insert(0) += 1; } } } else if dir == "R" { for _ in 0..steps { println!("R {}", steps); head.0 += 1; if tail.0 - head.0 == -2 { println!("R move {}", steps); tail.1 = head.1; tail.0 += 1; *positions.entry(tail).or_insert(0) += 1; } } } else if dir == "L" { for _ in 0..steps { println!("L {}", steps); head.0 -= 1; if tail.0 - head.0 == 2 { println!("L move {}", steps); tail.1 = head.1; tail.0 -= 1; *positions.entry(tail).or_insert(0) += 1; } } } } } } println!("tail {:?}", tail); println!("head {:?}", head); println!("positions {:?}", positions); println!("positions {}", positions.len()); } fn read_lines

(filename: P) -> io::Result>> where P: AsRef, { let file = File::open(filename)?; Ok(io::BufReader::new(file).lines()) }