solution: refactor day6 with 2d array
This commit is contained in:
		| @@ -1,17 +1,15 @@ | |||||||
| use hashbrown::HashMap; |  | ||||||
|  |  | ||||||
| use lazy_static::lazy_static; | use lazy_static::lazy_static; | ||||||
| use regex::Regex; | use regex::Regex; | ||||||
| use Instruction::*; | use Instruction::*; | ||||||
|  |  | ||||||
| #[derive(Debug)] | #[derive(Debug)] | ||||||
| struct Point { | struct Point { | ||||||
|     x: u32, |     x: usize, | ||||||
|     y: u32, |     y: usize, | ||||||
| } | } | ||||||
|  |  | ||||||
| impl FromIterator<u32> for Point { | impl FromIterator<usize> for Point { | ||||||
|     fn from_iter<T: IntoIterator<Item = u32>>(iter: T) -> Self { |     fn from_iter<T: IntoIterator<Item = usize>>(iter: T) -> Self { | ||||||
|         let mut it = iter.into_iter(); |         let mut it = iter.into_iter(); | ||||||
|         Point { |         Point { | ||||||
|             x: it.next().unwrap(), |             x: it.next().unwrap(), | ||||||
| @@ -52,15 +50,9 @@ impl From<&str> for Command { | |||||||
|             _ => unreachable!(), |             _ => unreachable!(), | ||||||
|         }; |         }; | ||||||
|  |  | ||||||
|         let from: Point = capture[2] |         let from: Point = capture[2].split(',').map(|x| x.parse().unwrap()).collect(); | ||||||
|             .split(',') |  | ||||||
|             .map(|x| x.parse::<u32>().unwrap()) |  | ||||||
|             .collect(); |  | ||||||
|  |  | ||||||
|         let to: Point = capture[3] |         let to: Point = capture[3].split(',').map(|x| x.parse().unwrap()).collect(); | ||||||
|             .split(',') |  | ||||||
|             .map(|x| x.parse::<u32>().unwrap()) |  | ||||||
|             .collect(); |  | ||||||
|  |  | ||||||
|         Command { |         Command { | ||||||
|             instruction, |             instruction, | ||||||
| @@ -71,7 +63,7 @@ impl From<&str> for Command { | |||||||
| } | } | ||||||
|  |  | ||||||
| pub fn part_one(input: &str) -> Option<u32> { | pub fn part_one(input: &str) -> Option<u32> { | ||||||
|     let mut map = HashMap::<(u32, u32), bool>::new(); |     let mut a = [[0; 1000]; 1000]; | ||||||
|     for line in input.trim().split('\n') { |     for line in input.trim().split('\n') { | ||||||
|         let Command { |         let Command { | ||||||
|             instruction, |             instruction, | ||||||
| @@ -81,20 +73,18 @@ pub fn part_one(input: &str) -> Option<u32> { | |||||||
|         for x in from.x..=to.x { |         for x in from.x..=to.x { | ||||||
|             for y in from.y..=to.y { |             for y in from.y..=to.y { | ||||||
|                 match instruction { |                 match instruction { | ||||||
|                     On => *map.entry((x, y)).or_default() = true, |                     On => a[x][y] = 1, | ||||||
|                     Off => *map.entry((x, y)).or_default() = false, |                     Off => a[x][y] = 0, | ||||||
|                     Toggle => { |                     Toggle => a[x][y] = 1 - a[x][y], | ||||||
|                         map.entry((x, y)).or_default(); |  | ||||||
|                         map.insert((x, y), !map[&(x, y)]); |  | ||||||
|                     } |  | ||||||
|                 } |                 } | ||||||
|             } |             } | ||||||
|         } |         } | ||||||
|     } |     } | ||||||
|     Some(map.values().filter(|&x| *x).count() as u32) |     Some(a.iter().map(|x| -> u32 { x.iter().sum() }).sum()) | ||||||
| } | } | ||||||
|  |  | ||||||
| pub fn part_two(input: &str) -> Option<u32> { | pub fn part_two(input: &str) -> Option<u32> { | ||||||
|     let mut map = HashMap::<(u32, u32), u32>::new(); |     let mut a = [[0; 1000]; 1000]; | ||||||
|     for line in input.trim().split('\n') { |     for line in input.trim().split('\n') { | ||||||
|         let Command { |         let Command { | ||||||
|             instruction, |             instruction, | ||||||
| @@ -104,19 +94,15 @@ pub fn part_two(input: &str) -> Option<u32> { | |||||||
|         for x in from.x..=to.x { |         for x in from.x..=to.x { | ||||||
|             for y in from.y..=to.y { |             for y in from.y..=to.y { | ||||||
|                 match instruction { |                 match instruction { | ||||||
|                     On => *map.entry((x, y)).or_default() += 1, |                     On => a[x][y] = 1, | ||||||
|                     Toggle => *map.entry((x, y)).or_default() += 2, |                     Off if a[x][y] > 0 => a[x][y] -= 1, | ||||||
|                     Off => { |                     Off => (), | ||||||
|                         map.entry((x, y)).or_default(); |                     Toggle => a[x][y] += 2, | ||||||
|                         if map[&(x, y)] > 0 { |  | ||||||
|                             *map.entry((x, y)).or_default() -= 1; |  | ||||||
|                         } |  | ||||||
|                     } |  | ||||||
|                 } |                 } | ||||||
|             } |             } | ||||||
|         } |         } | ||||||
|     } |     } | ||||||
|     Some(map.values().into_iter().sum()) |     Some(a.iter().map(|x| -> u32 { x.iter().sum() }).sum()) | ||||||
| } | } | ||||||
| fn main() { | fn main() { | ||||||
|     let input = &aoc::read_file("inputs", 6); |     let input = &aoc::read_file("inputs", 6); | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user