diff --git a/2016/day01/src/main.rs b/2016/day01/src/main.rs index f92f6cb..27266be 100644 --- a/2016/day01/src/main.rs +++ b/2016/day01/src/main.rs @@ -20,6 +20,10 @@ fn step(state: &(i32, i32), pos: &(i32, i32), dist: i32) -> (i32, i32) { (pos.0 + state.0 * dist, pos.1 + state.1 * dist) } +fn tiny_step(state: &(i32, i32), pos: &(usize, usize)) -> (usize, usize) { + ((pos.0 as i32 + state.0) as usize, (pos.1 as i32 + state.1) as usize) +} + fn part_1(input: &String) { let mut state = (1, 0); let mut pos = (0, 0); @@ -37,26 +41,29 @@ fn part_1(input: &String) { } fn part_2(input: &String) { - let mut state = (1, 0); - let mut positions: Vec<(i32, i32)> = vec![(0, 0)]; + const OFFSET: usize = 500; //we'll make a grid 1000x1000 + let mut grid = [[false; 2*OFFSET]; 2*OFFSET]; - for item in input.trim().split(", ") { + let mut state = (1, 0); + let mut pos = (OFFSET, OFFSET); + grid[OFFSET][OFFSET] = true; //we are already at the axis + + 'outer: for item in input.trim().split(", ") { let (dir, dist) = item.split_at(1); let dir: String = dir.to_string(); - let dist: i32 = dist.parse::().unwrap(); + let mut dist: i32 = dist.parse::().unwrap(); state = turn(&state, &dir); - let pos = step(&state, positions.last().unwrap(), dist); - print!("{}: {}{} -> ({}, {}), ", positions.len(), dist, dir, pos.0, pos.1); - match positions.iter().position(|&p| p == pos) { - Some(v) => { - println!("Part 2: Stopped twice at ({}, {}) on element {}, {} away from the start", pos.0, pos.1, v, pos.0.abs() + pos.1.abs()); - break; - }, - None => { - //println!("Did not match {} locations", positions.len()); - positions.push(pos); - }, + while dist > 0 { //take a step + pos = tiny_step(&state, &pos); + match grid[pos.0][pos.1] { + true => { + let real_pos = ((pos.0 as i32 - OFFSET as i32), (pos.1 as i32 - OFFSET as i32)); + println!("Part 2: Returned to ({}, {}), {} away from the start", real_pos.0, real_pos.1, real_pos.0.abs() + real_pos.1.abs()); + break 'outer; + }, + false => { grid[pos.0][pos.1] = true; dist = dist - 1 }, + } } } } @@ -66,6 +73,6 @@ fn main() { let mut input = String::new(); file.read_to_string(&mut input).expect("could not read file"); - //part_1(&input); + part_1(&input); part_2(&input); } \ No newline at end of file diff --git a/2016/day03/src/main.rs b/2016/day03/src/main.rs index a18ce91..17d8f73 100644 --- a/2016/day03/src/main.rs +++ b/2016/day03/src/main.rs @@ -15,7 +15,34 @@ fn part_1(input: &String) { } fn part_2(input: &String) { - ; + //I am not skill enough with iterator adaptors to use .as_slice().chunks(3) + //so we will do things the lazy way. sad + let mut triangles = vec![vec![0; 3]; 3]; + let mut read_lines = 3; + let mut total = 0; //total good triangles + for line in input.lines().map(|l| l.to_string()).collect::>().iter() { + let values = line.trim().split_whitespace().map(|v| v.parse::().unwrap()).collect::>(); + //println!("Values are {:?}", values); + triangles[0][3 - read_lines] = values[0]; + triangles[1][3 - read_lines] = values[1]; + triangles[2][3 - read_lines] = values[2]; + read_lines = read_lines - 1; + if read_lines == 0 { + //process our triangles + let good = triangles + .iter() + .filter(|v| { + //println!("filtering {:?}", v); + v.iter().fold(0, |sum, x| sum + x) > 2 * v.iter().max().unwrap() } + ) + .collect::>() + .len(); + //println!("Found {} good ones from this set", good); + total = total + good; + read_lines = 3; //reset + } + } + println!("Part 2: there are {} good triangles", total); } fn main() { @@ -24,4 +51,5 @@ fn main() { file.read_to_string(&mut input).expect("could not read file"); part_1(&input); + part_2(&input); }