From ec42c60bef576a514522575fdff926b423ffb5a5 Mon Sep 17 00:00:00 2001
From: Evy Garden <evysgarden@protonmail.com>
Date: Mon, 3 Jul 2023 19:00:59 +0200
Subject: [PATCH] newline

---
 day08/src/main.rs  |  2 +-
 day15/Cargo.lock   | 42 ++++++++++++++++++++++++++++++++++++
 day15/Cargo.toml   |  9 ++++++++
 day15/data.txt     | 25 ++++++++++++++++++++++
 day15/example.txt  | 14 ++++++++++++
 day15/src/main.rs  | 31 +++++++++++++++++++++++++++
 day15/src/types.rs | 53 ++++++++++++++++++++++++++++++++++++++++++++++
 7 files changed, 175 insertions(+), 1 deletion(-)
 create mode 100644 day15/Cargo.lock
 create mode 100644 day15/Cargo.toml
 create mode 100644 day15/data.txt
 create mode 100644 day15/example.txt
 create mode 100644 day15/src/main.rs
 create mode 100644 day15/src/types.rs

diff --git a/day08/src/main.rs b/day08/src/main.rs
index 6aba465..7a8c5af 100644
--- a/day08/src/main.rs
+++ b/day08/src/main.rs
@@ -59,4 +59,4 @@ fn main() {
 
     let visible = datamat.elements.iter().filter(|v| v.1).count();
     println!("Visible trees: {visible}");
-}
+}
\ No newline at end of file
diff --git a/day15/Cargo.lock b/day15/Cargo.lock
new file mode 100644
index 0000000..c327390
--- /dev/null
+++ b/day15/Cargo.lock
@@ -0,0 +1,42 @@
+# This file is automatically @generated by Cargo.
+# It is not intended for manual editing.
+version = 3
+
+[[package]]
+name = "aho-corasick"
+version = "0.7.20"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "cc936419f96fa211c1b9166887b38e5e40b19958e5b895be7c1f93adec7071ac"
+dependencies = [
+ "memchr",
+]
+
+[[package]]
+name = "day15"
+version = "0.1.0"
+dependencies = [
+ "regex",
+]
+
+[[package]]
+name = "memchr"
+version = "2.5.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "2dffe52ecf27772e601905b7522cb4ef790d2cc203488bbd0e2fe85fcb74566d"
+
+[[package]]
+name = "regex"
+version = "1.7.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "e076559ef8e241f2ae3479e36f97bd5741c0330689e217ad51ce2c76808b868a"
+dependencies = [
+ "aho-corasick",
+ "memchr",
+ "regex-syntax",
+]
+
+[[package]]
+name = "regex-syntax"
+version = "0.6.28"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "456c603be3e8d448b072f410900c09faf164fbce2d480456f50eea6e25f9c848"
diff --git a/day15/Cargo.toml b/day15/Cargo.toml
new file mode 100644
index 0000000..4ccb6f6
--- /dev/null
+++ b/day15/Cargo.toml
@@ -0,0 +1,9 @@
+[package]
+name = "day15"
+version = "0.1.0"
+edition = "2021"
+
+# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
+
+[dependencies]
+regex = "1.7.0"
diff --git a/day15/data.txt b/day15/data.txt
new file mode 100644
index 0000000..d4c562b
--- /dev/null
+++ b/day15/data.txt
@@ -0,0 +1,25 @@
+Sensor at x=251234, y=759482: closest beacon is at x=-282270, y=572396
+Sensor at x=2866161, y=3374117: closest beacon is at x=2729330, y=3697325
+Sensor at x=3999996, y=3520742: closest beacon is at x=3980421, y=3524442
+Sensor at x=3988282, y=3516584: closest beacon is at x=3980421, y=3524442
+Sensor at x=3005586, y=3018139: closest beacon is at x=2727127, y=2959718
+Sensor at x=3413653, y=3519082: closest beacon is at x=3980421, y=3524442
+Sensor at x=2900403, y=187208: closest beacon is at x=2732772, y=2000000
+Sensor at x=1112429, y=3561166: closest beacon is at x=2729330, y=3697325
+Sensor at x=3789925, y=3283328: closest beacon is at x=3980421, y=3524442
+Sensor at x=3991533, y=3529053: closest beacon is at x=3980421, y=3524442
+Sensor at x=3368119, y=2189371: closest beacon is at x=2732772, y=2000000
+Sensor at x=2351157, y=2587083: closest beacon is at x=2727127, y=2959718
+Sensor at x=3326196, y=2929990: closest beacon is at x=3707954, y=2867627
+Sensor at x=3839244, y=1342691: closest beacon is at x=3707954, y=2867627
+Sensor at x=2880363, y=3875503: closest beacon is at x=2729330, y=3697325
+Sensor at x=1142859, y=1691416: closest beacon is at x=2732772, y=2000000
+Sensor at x=3052449, y=2711719: closest beacon is at x=2727127, y=2959718
+Sensor at x=629398, y=214610: closest beacon is at x=-282270, y=572396
+Sensor at x=3614706, y=3924106: closest beacon is at x=3980421, y=3524442
+Sensor at x=3999246, y=2876762: closest beacon is at x=3707954, y=2867627
+Sensor at x=3848935, y=3020496: closest beacon is at x=3707954, y=2867627
+Sensor at x=123637, y=2726215: closest beacon is at x=-886690, y=3416197
+Sensor at x=4000000, y=3544014: closest beacon is at x=3980421, y=3524442
+Sensor at x=2524955, y=3861248: closest beacon is at x=2729330, y=3697325
+Sensor at x=2605475, y=3152151: closest beacon is at x=2727127, y=2959718
\ No newline at end of file
diff --git a/day15/example.txt b/day15/example.txt
new file mode 100644
index 0000000..652e631
--- /dev/null
+++ b/day15/example.txt
@@ -0,0 +1,14 @@
+Sensor at x=2, y=18: closest beacon is at x=-2, y=15
+Sensor at x=9, y=16: closest beacon is at x=10, y=16
+Sensor at x=13, y=2: closest beacon is at x=15, y=3
+Sensor at x=12, y=14: closest beacon is at x=10, y=16
+Sensor at x=10, y=20: closest beacon is at x=10, y=16
+Sensor at x=14, y=17: closest beacon is at x=10, y=16
+Sensor at x=8, y=7: closest beacon is at x=2, y=10
+Sensor at x=2, y=0: closest beacon is at x=2, y=10
+Sensor at x=0, y=11: closest beacon is at x=2, y=10
+Sensor at x=20, y=14: closest beacon is at x=25, y=17
+Sensor at x=17, y=20: closest beacon is at x=21, y=22
+Sensor at x=16, y=7: closest beacon is at x=15, y=3
+Sensor at x=14, y=3: closest beacon is at x=15, y=3
+Sensor at x=20, y=1: closest beacon is at x=15, y=3
\ No newline at end of file
diff --git a/day15/src/main.rs b/day15/src/main.rs
new file mode 100644
index 0000000..daa0fc1
--- /dev/null
+++ b/day15/src/main.rs
@@ -0,0 +1,31 @@
+mod types;
+
+use std::collections::HashSet;
+
+use self::types::*;
+
+fn main() {
+    let sdata = parse_input("data.txt");
+    dbg!(&sdata);
+
+    let line = 2000000;
+
+    let mut intersections = HashSet::<Point>::new();
+    sdata.iter().for_each(|v| {
+        // check if interection is even there
+        let radius = v.position.distance(v.beacon);
+        let row_diff = v.position.y.abs_diff(line);
+        if row_diff <= radius {
+            let intersection_num = (radius - row_diff) as i32;
+            println!("{intersection_num} intersections with sensor at {:?} with distance {} to beacon at {:?}", v.position, radius, v.beacon);
+            for x in (v.position.x - intersection_num)..=(v.position.x + intersection_num) {
+                // print!("{} ", x);
+                intersections.insert(Point::new(x, line));
+            }
+            // println!();
+        }
+        
+    });
+    sdata.iter().for_each(|v| { intersections.remove(&v.beacon); });
+    println!("Intersections: {}", intersections.len());
+}
diff --git a/day15/src/types.rs b/day15/src/types.rs
new file mode 100644
index 0000000..a06a7e8
--- /dev/null
+++ b/day15/src/types.rs
@@ -0,0 +1,53 @@
+use std::{fs::read_to_string, path::Path};
+
+use regex::Regex;
+
+#[derive(Debug)]
+pub struct SensorData {
+    pub position: Point,
+    pub beacon: Point,
+}
+
+impl SensorData {
+    pub fn new(position: Point, beacon: Point) -> Self {
+        Self { position, beacon }
+    }
+}
+
+#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
+pub struct Point {
+    pub x: i32,
+    pub y: i32,
+}
+
+impl Point {
+    pub fn new(x: i32, y: i32) -> Self {
+        Self { x, y }
+    }
+
+    pub fn distance(&self, other: Point) -> u32 {
+        self.x.abs_diff(other.x) + self.y.abs_diff(other.y)
+    }
+}
+
+pub fn parse_input(path: impl AsRef<Path>) -> Vec<SensorData> {
+    let data = read_to_string(path).unwrap();
+
+    let mut sensors = Vec::new();
+    let reg =
+        Regex::new(r"x=(?P<s_x>-?\d+), y=(?P<s_y>-?\d+).*x=(?P<b_x>-?\d+), y=(?P<b_y>-?\d+)").unwrap();
+    for cap in reg.captures_iter(&data) {
+        sensors.push(SensorData::new(
+            Point::new(
+                cap["s_x"].parse::<i32>().unwrap(),
+                cap["s_y"].parse::<i32>().unwrap(),
+            ),
+            Point::new(
+                cap["b_x"].parse::<i32>().unwrap(),
+                cap["b_y"].parse::<i32>().unwrap(),
+            ),
+        ));
+    }
+
+    sensors
+}
-- 
GitLab