summaryrefslogtreecommitdiff
path: root/2023
diff options
context:
space:
mode:
authornekineki <nekineki@nekineki.net>2023-12-13 20:17:16 +0100
committernekineki <nekineki@nekineki.net>2023-12-13 20:18:58 +0100
commite2d2c3cedf0cd4af66f413f920e9181f4b0bb710 (patch)
tree9963231676f547f402e45cfd9327cf6b5422f51a /2023
parent62283ad92238c33e4ac1707b3467dfe0b9ebf79a (diff)
day11 part2
Diffstat (limited to '2023')
-rw-r--r--2023/day11.rs71
1 files changed, 57 insertions, 14 deletions
diff --git a/2023/day11.rs b/2023/day11.rs
index f78f45d..95e422c 100644
--- a/2023/day11.rs
+++ b/2023/day11.rs
@@ -1,6 +1,8 @@
#![allow(dead_code)]
#![allow(unused_variables)]
#![allow(unused_mut)]
+use std::cmp::max;
+use std::cmp::min;
use std::fs::File;
use std::io::Read;
@@ -44,6 +46,18 @@ fn print_m(m: &Vec<Vec<bool>>) {
println!();
}
+fn get_pos(m: &Vec<Vec<bool>>) -> Vec<(i32, i32)> {
+ let mut p = Vec::new();
+ for y in 0..m.len() {
+ for x in 0..m[0].len() {
+ if m[y][x] {
+ p.push((x as i32, y as i32));
+ }
+ }
+ }
+ p
+}
+
fn main() {
// let filename = "in/day11.ref";
let filename = "in/day11.pzl";
@@ -62,35 +76,64 @@ fn main() {
m.push(r);
}
- m = insert_rows(m);
+ let m2 = m.clone();
+ let m2t = transpose(m.clone());
+ let p2 = get_pos(&m2);
+ m = insert_rows(m);
m = transpose(m);
m = insert_rows(m);
m = transpose(m);
+ let p1 = get_pos(&m);
- let mut p: Vec<(i32, i32)> = Vec::new();
- for y in 0..m.len() {
- for x in 0..m[0].len() {
- if m[y][x] {
- p.push((x as i32, y as i32));
+ let mut res1 = 0;
+ let mut res2: i64 = 0;
+
+ for i in 0..p1.len() {
+ for j in i..p1.len() {
+ if i != j {
+ let (x1, y1) = p1[i];
+ let (x2, y2) = p1[j];
+ res1 += (x2 - x1).abs() + (y2 - y1).abs();
}
}
}
- println!("{:?}", p);
- let mut res1 = 0;
- let mut res2 = 0;
+ let add = 1000000;
- for i in 0..p.len() {
- for j in i..p.len() {
+ for i in 0..p2.len() {
+ for j in i..p2.len() {
if i != j {
- let (x1, y1) = p[i];
- let (x2, y2) = p[j];
- res1 += (x2 - x1).abs() + (y2 - y1).abs();
+ let (x1, y1) = p2[i];
+ let (x2, y2) = p2[j];
+
+ let x_min = min(x1, x2);
+ let x_max = max(x1, x2);
+
+ let y_min = min(y1, y2);
+ let y_max = max(y1, y2);
+
+ for y in y_min..y_max {
+ if m2[y as usize].iter().any(|a| *a) {
+ res2 += 1;
+ } else {
+ res2 += add;
+ }
+ }
+
+ for x in x_min..x_max {
+ if m2t[x as usize].iter().any(|a| *a) {
+ res2 += 1;
+ } else {
+ res2 += add;
+ }
+ }
}
}
}
println!("res1: {}", res1);
println!("res2: {}", res2);
+ assert_eq!(res1, 9681886);
+ assert_eq!(res2, 791134099634);
}