fix: day6 tests and rust fmt
This commit is contained in:
		| @@ -4,7 +4,7 @@ use itertools::Itertools; | |||||||
|  |  | ||||||
| pub fn part_one(input: &str) -> Option<u32> { | pub fn part_one(input: &str) -> Option<u32> { | ||||||
|     let mut visited = HashSet::with_capacity(input.len()); |     let mut visited = HashSet::with_capacity(input.len()); | ||||||
|     visited.insert((0,0)); |     visited.insert((0, 0)); | ||||||
|     let mut x = 0; |     let mut x = 0; | ||||||
|     let mut y = 0; |     let mut y = 0; | ||||||
|     for direction in input.trim().chars() { |     for direction in input.trim().chars() { | ||||||
| @@ -13,15 +13,15 @@ pub fn part_one(input: &str) -> Option<u32> { | |||||||
|             '<' => x -= 1, |             '<' => x -= 1, | ||||||
|             '^' => y += 1, |             '^' => y += 1, | ||||||
|             'v' => y -= 1, |             'v' => y -= 1, | ||||||
|             _ => panic!("oops") |             _ => panic!("oops"), | ||||||
|         }; |         }; | ||||||
|         visited.insert((x, y)); |         visited.insert((x, y)); | ||||||
|     }; |     } | ||||||
|     Some(visited.len() as u32) |     Some(visited.len() as u32) | ||||||
| } | } | ||||||
| pub fn part_two(input: &str) -> Option<u32> { | pub fn part_two(input: &str) -> Option<u32> { | ||||||
|     let mut visited = HashSet::with_capacity(input.len()); |     let mut visited = HashSet::with_capacity(input.len()); | ||||||
|     visited.insert((0,0)); |     visited.insert((0, 0)); | ||||||
|     let (mut x1, mut y1, mut x2, mut y2) = (0, 0, 0, 0); |     let (mut x1, mut y1, mut x2, mut y2) = (0, 0, 0, 0); | ||||||
|     for (santa, robot) in input.trim().chars().tuple_windows().step_by(2) { |     for (santa, robot) in input.trim().chars().tuple_windows().step_by(2) { | ||||||
|         match santa { |         match santa { | ||||||
| @@ -29,7 +29,7 @@ pub fn part_two(input: &str) -> Option<u32> { | |||||||
|             '<' => x1 -= 1, |             '<' => x1 -= 1, | ||||||
|             '^' => y1 += 1, |             '^' => y1 += 1, | ||||||
|             'v' => y1 -= 1, |             'v' => y1 -= 1, | ||||||
|             _ => panic!("oops") |             _ => panic!("oops"), | ||||||
|         }; |         }; | ||||||
|         visited.insert((x1, y1)); |         visited.insert((x1, y1)); | ||||||
|         match robot { |         match robot { | ||||||
| @@ -37,10 +37,10 @@ pub fn part_two(input: &str) -> Option<u32> { | |||||||
|             '<' => x2 -= 1, |             '<' => x2 -= 1, | ||||||
|             '^' => y2 += 1, |             '^' => y2 += 1, | ||||||
|             'v' => y2 -= 1, |             'v' => y2 -= 1, | ||||||
|             _ => panic!("oops") |             _ => panic!("oops"), | ||||||
|         }; |         }; | ||||||
|         visited.insert((x2, y2)); |         visited.insert((x2, y2)); | ||||||
|     }; |     } | ||||||
|     Some(visited.len() as u32) |     Some(visited.len() as u32) | ||||||
| } | } | ||||||
| fn main() { | fn main() { | ||||||
|   | |||||||
| @@ -29,7 +29,9 @@ pub fn part_two(input: &str) -> Option<u32> { | |||||||
|         let mut output = [0; 16]; |         let mut output = [0; 16]; | ||||||
|         hasher.result(&mut output); |         hasher.result(&mut output); | ||||||
|  |  | ||||||
|         if output.starts_with(&[0,0,0]) { return Some(x as u32)} |         if output.starts_with(&[0, 0, 0]) { | ||||||
|  |             return Some(x as u32); | ||||||
|  |         } | ||||||
|         hasher.reset(); |         hasher.reset(); | ||||||
|     } |     } | ||||||
|     None |     None | ||||||
|   | |||||||
| @@ -63,19 +63,19 @@ impl From<&str> for Command { | |||||||
| } | } | ||||||
|  |  | ||||||
| pub fn part_one(input: &str) -> Option<u32> { | pub fn part_one(input: &str) -> Option<u32> { | ||||||
|     let mut a = [[0; 1000]; 1000]; |     let mut a = vec![[0; 1000]; 1000]; | ||||||
|     for line in input.trim().split('\n') { |     for line in input.trim().split('\n') { | ||||||
|         let Command { |         let Command { | ||||||
|             instruction, |             instruction, | ||||||
|             from, |             from, | ||||||
|             to, |             to, | ||||||
|         } = Command::from(line); |         } = Command::from(line); | ||||||
|         for x in from.x..=to.x { |         for x in a.iter_mut().take(to.x + 1).skip(from.x) { | ||||||
|             for y in from.y..=to.y { |             for y in x.iter_mut().take(to.y + 1).skip(from.y) { | ||||||
|                 match instruction { |                 match instruction { | ||||||
|                     On => a[x][y] = 1, |                     On => *y = 1, | ||||||
|                     Off => a[x][y] = 0, |                     Off => *y = 0, | ||||||
|                     Toggle => a[x][y] = 1 - a[x][y], |                     Toggle => *y = 1 - *y, | ||||||
|                 } |                 } | ||||||
|             } |             } | ||||||
|         } |         } | ||||||
| @@ -84,20 +84,20 @@ pub fn part_one(input: &str) -> Option<u32> { | |||||||
| } | } | ||||||
|  |  | ||||||
| pub fn part_two(input: &str) -> Option<u32> { | pub fn part_two(input: &str) -> Option<u32> { | ||||||
|     let mut a = [[0; 1000]; 1000]; |     let mut a = vec![[0; 1000]; 1000]; | ||||||
|     for line in input.trim().split('\n') { |     for line in input.trim().split('\n') { | ||||||
|         let Command { |         let Command { | ||||||
|             instruction, |             instruction, | ||||||
|             from, |             from, | ||||||
|             to, |             to, | ||||||
|         } = Command::from(line); |         } = Command::from(line); | ||||||
|         for x in from.x..=to.x { |         for x in a.iter_mut().take(to.x + 1).skip(from.x) { | ||||||
|             for y in from.y..=to.y { |             for y in x.iter_mut().take(to.y + 1).skip(from.y) { | ||||||
|                 match instruction { |                 match instruction { | ||||||
|                     On => a[x][y] = 1, |                     On => *y += 1, | ||||||
|                     Off if a[x][y] > 0 => a[x][y] -= 1, |                     Toggle => *y += 2, | ||||||
|                     Off => (), |                     Off if y > &mut 0 => *y -= 1, | ||||||
|                     Toggle => a[x][y] += 2, |                     _ => (), | ||||||
|                 } |                 } | ||||||
|             } |             } | ||||||
|         } |         } | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user