summaryrefslogtreecommitdiff
path: root/2023
diff options
context:
space:
mode:
authornekineki <nekineki@nekineki.net>2023-12-03 13:33:07 +0100
committernekineki <nekineki@nekineki.net>2023-12-03 13:33:07 +0100
commit0c4585fa8507873f13969b74ab410bb3d440ac04 (patch)
treee2d4e7f2bed85888d1fa4b095b992c1d50b90642 /2023
parente687c9347ea3e018d41f9c45e8bb52791b2267db (diff)
improve day03
Diffstat (limited to '2023')
-rw-r--r--2023/day03.rs121
1 files changed, 37 insertions, 84 deletions
diff --git a/2023/day03.rs b/2023/day03.rs
index be02594..16c735a 100644
--- a/2023/day03.rs
+++ b/2023/day03.rs
@@ -4,8 +4,6 @@
use std::collections::HashMap;
use std::fs::File;
use std::io::Read;
-// use std::cmp::max;
-// use std::cmp::min;
fn is_symbol(c: char) -> bool {
if c.is_digit(10) || c == '.' {
@@ -15,35 +13,14 @@ fn is_symbol(c: char) -> bool {
}
}
-fn is_valid(x: usize, y: usize, m: &Vec<Vec<char>>) -> bool {
- for dy in -1i32..2 {
- for dx in -1i32..2 {
- let mut xx: usize;
- let mut yy: usize;
- if x == 0 && dx == -1 {
- xx = x;
- } else if x == m[0].len() - 1 && dx == 1 {
- xx = x;
- } else {
- xx = ((x as i32) + dx) as usize;
- }
-
- if y == 0 && dy == -1 {
- yy = y;
- } else if y == m.len() - 1 && dy == 1 {
- yy = y;
- } else {
- yy = ((y as i32) + dy) as usize;
- }
- // println!("{} {} {} {} {} {}", x, y, dx, dy, xx, yy);
- // print!("{}", m[y][x]);
-
- if is_symbol(m[yy][xx]) {
- return true;
- }
+fn dydx() -> Vec<(i32, i32)> {
+ let mut ret = Vec::new();
+ for y in -1i32..=1 {
+ for x in -1i32..=1 {
+ ret.push((y, x));
}
}
- return false;
+ ret
}
fn main() {
@@ -60,34 +37,28 @@ fn main() {
let mut res2 = 0;
let mut m: Vec<Vec<char>> = Vec::new();
+ let pad: Vec<_> = (0..lines[0].bytes().len() + 2).map(|_| '.').collect();
+ m.push(pad.clone());
for line in &lines {
let mut l: Vec<char> = Vec::new();
+ l.push('.');
for c in line.bytes() {
l.push(c as char)
}
+ l.push('.');
m.push(l)
}
+ m.push(pad.clone());
let mut hm: HashMap<(usize, usize), (u32, u32)> = HashMap::new();
let mut id: u32 = 0;
- for y in 0..m.len() {
+ for y in 1..m.len() - 1 {
let mut have_num = false;
let mut num_valid = false;
let mut start_x = 0;
- for x in 0..m[0].len() {
+ for x in 1..m[0].len() {
if have_num {
- if m[y][x].is_digit(10) == true && x == (m[0].len() - 1) {
- if num_valid {
- let num: u32 = String::from_iter(m[y][start_x..=x].iter()).parse().unwrap();
- for xx in start_x..=x {
- hm.insert((xx, y), (id, num));
- }
- id += 1;
- res1 += num;
- }
- have_num = false;
- num_valid = false;
- } else if m[y][x].is_digit(10) == false {
+ if m[y][x].is_digit(10) == false {
if num_valid {
let num: u32 = String::from_iter(m[y][start_x..x].iter()).parse().unwrap();
for xx in start_x..x {
@@ -104,65 +75,47 @@ fn main() {
start_x = x;
}
+ if x == m[0].len() - 1 {
+ continue;
+ }
+
if have_num {
- num_valid = num_valid || is_valid(x, y, &m);
+ num_valid = num_valid
+ || dydx()
+ .into_iter()
+ .map(|(dy, dx)| {
+ is_symbol(m[((y as i32) + dy) as usize][((x as i32) + dx) as usize])
+ })
+ .any(|x| x);
}
}
}
- println!("{:?}", hm);
-
for y in 0..m.len() {
for x in 0..m[0].len() {
if m[y][x] == '*' {
- // println!("{} {}", y+1, x+1);
let mut neighbour: HashMap<u32, u32> = HashMap::new();
- for dy in -1i32..2 {
- for dx in -1i32..2 {
- let mut xx: usize;
- let mut yy: usize;
- if x == 0 && dx == -1 {
- xx = x;
- } else if x == m[0].len() - 1 && dx == 1 {
- xx = x;
- } else {
- xx = ((x as i32) + dx) as usize;
- }
-
- if y == 0 && dy == -1 {
- yy = y;
- } else if y == m.len() - 1 && dy == 1 {
- yy = y;
- } else {
- yy = ((y as i32) + dy) as usize;
- }
- if let Some((id, val)) = hm.get(&(xx, yy)) {
- neighbour.insert(*id, *val);
- }
+ for (dy, dx) in dydx().into_iter() {
+ let xx = ((x as i32) + dx) as usize;
+ let yy = ((y as i32) + dy) as usize;
+ if let Some((id, val)) = hm.get(&(xx, yy)) {
+ neighbour.insert(*id, *val);
}
}
if neighbour.len() == 2 {
- let mut mul: u32 = 1;
- for (key, val) in neighbour.iter() {
- mul *= val;
- }
- // res2 += neighbour.iter().map(|(_, val)| val).reduce(|a,b| a*b).unwrap();
- println!("{}", mul);
- res2 += mul;
+ res2 += neighbour
+ .iter()
+ .map(|(_, val)| *val)
+ .reduce(|a, b| a * b)
+ .unwrap();
}
- // let a: u32 = neighbour.iter()
- // .inspect(|x| println!("asdf {:?}", x))
- // .map(|(_, val)| val)
- // .reduce(|a,b| a*b);
- // println!("{:?}", a);
-
- // println!("{:?}", neighbour);
- // asdf
}
}
}
println!("res1: {}", res1);
println!("res2: {}", res2);
+ assert_eq!(res1, 532445);
+ assert_eq!(res2, 79842967);
}