From 0000013076edb85263482cd3e64e6b869a85683d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matej=20Jane=C5=BEi=C4=8D?= Date: Sat, 3 Dec 2022 22:01:47 +0100 Subject: [PATCH] solution: refactor day6 with 2d array --- src/bin/06.rs | 50 ++++++++++++++++++-------------------------------- 1 file changed, 18 insertions(+), 32 deletions(-) diff --git a/src/bin/06.rs b/src/bin/06.rs index c84c85e..f3c7bc1 100644 --- a/src/bin/06.rs +++ b/src/bin/06.rs @@ -1,17 +1,15 @@ -use hashbrown::HashMap; - use lazy_static::lazy_static; use regex::Regex; use Instruction::*; #[derive(Debug)] struct Point { - x: u32, - y: u32, + x: usize, + y: usize, } -impl FromIterator for Point { - fn from_iter>(iter: T) -> Self { +impl FromIterator for Point { + fn from_iter>(iter: T) -> Self { let mut it = iter.into_iter(); Point { x: it.next().unwrap(), @@ -52,15 +50,9 @@ impl From<&str> for Command { _ => unreachable!(), }; - let from: Point = capture[2] - .split(',') - .map(|x| x.parse::().unwrap()) - .collect(); + let from: Point = capture[2].split(',').map(|x| x.parse().unwrap()).collect(); - let to: Point = capture[3] - .split(',') - .map(|x| x.parse::().unwrap()) - .collect(); + let to: Point = capture[3].split(',').map(|x| x.parse().unwrap()).collect(); Command { instruction, @@ -71,7 +63,7 @@ impl From<&str> for Command { } pub fn part_one(input: &str) -> Option { - let mut map = HashMap::<(u32, u32), bool>::new(); + let mut a = [[0; 1000]; 1000]; for line in input.trim().split('\n') { let Command { instruction, @@ -81,20 +73,18 @@ pub fn part_one(input: &str) -> Option { for x in from.x..=to.x { for y in from.y..=to.y { match instruction { - On => *map.entry((x, y)).or_default() = true, - Off => *map.entry((x, y)).or_default() = false, - Toggle => { - map.entry((x, y)).or_default(); - map.insert((x, y), !map[&(x, y)]); - } + On => a[x][y] = 1, + Off => a[x][y] = 0, + Toggle => a[x][y] = 1 - a[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 { - let mut map = HashMap::<(u32, u32), u32>::new(); + let mut a = [[0; 1000]; 1000]; for line in input.trim().split('\n') { let Command { instruction, @@ -104,19 +94,15 @@ pub fn part_two(input: &str) -> Option { for x in from.x..=to.x { for y in from.y..=to.y { match instruction { - On => *map.entry((x, y)).or_default() += 1, - Toggle => *map.entry((x, y)).or_default() += 2, - Off => { - map.entry((x, y)).or_default(); - if map[&(x, y)] > 0 { - *map.entry((x, y)).or_default() -= 1; - } - } + On => a[x][y] = 1, + Off if a[x][y] > 0 => a[x][y] -= 1, + Off => (), + Toggle => a[x][y] += 2, } } } } - Some(map.values().into_iter().sum()) + Some(a.iter().map(|x| -> u32 { x.iter().sum() }).sum()) } fn main() { let input = &aoc::read_file("inputs", 6);