day 15
This commit is contained in:
		
							parent
							
								
									6639f35c10
								
							
						
					
					
						commit
						0c745248f0
					
				| 
						 | 
					@ -0,0 +1,12 @@
 | 
				
			||||||
 | 
					# ---> Rust
 | 
				
			||||||
 | 
					# Generated by Cargo
 | 
				
			||||||
 | 
					# will have compiled files and executables
 | 
				
			||||||
 | 
					/target/
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries
 | 
				
			||||||
 | 
					# More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html
 | 
				
			||||||
 | 
					Cargo.lock
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					# These are backup files generated by rustfmt
 | 
				
			||||||
 | 
					**/*.rs.bk
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -0,0 +1,8 @@
 | 
				
			||||||
 | 
					[package]
 | 
				
			||||||
 | 
					name = "aoc_15a"
 | 
				
			||||||
 | 
					version = "0.1.0"
 | 
				
			||||||
 | 
					edition = "2021"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					[dependencies]
 | 
				
			||||||
| 
						 | 
					@ -0,0 +1,38 @@
 | 
				
			||||||
 | 
					Sensor at x=3889276, y=3176133: closest beacon is at x=3738780, y=3090050
 | 
				
			||||||
 | 
					Sensor at x=3545888, y=1389980: closest beacon is at x=3687798, y=2823020
 | 
				
			||||||
 | 
					Sensor at x=2887269, y=2488344: closest beacon is at x=2809378, y=2513386
 | 
				
			||||||
 | 
					Sensor at x=3990278, y=43134: closest beacon is at x=2307159, y=135337
 | 
				
			||||||
 | 
					Sensor at x=3746631, y=2990632: closest beacon is at x=3738780, y=3090050
 | 
				
			||||||
 | 
					Sensor at x=7523, y=59064: closest beacon is at x=278652, y=-182407
 | 
				
			||||||
 | 
					Sensor at x=2662631, y=3349709: closest beacon is at x=2294322, y=3429562
 | 
				
			||||||
 | 
					Sensor at x=3999326, y=3030235: closest beacon is at x=3738780, y=3090050
 | 
				
			||||||
 | 
					Sensor at x=2788203, y=3722031: closest beacon is at x=3009520, y=4176552
 | 
				
			||||||
 | 
					Sensor at x=1872146, y=1228203: closest beacon is at x=1213036, y=1428271
 | 
				
			||||||
 | 
					Sensor at x=231045, y=2977983: closest beacon is at x=-362535, y=2000000
 | 
				
			||||||
 | 
					Sensor at x=2233881, y=421153: closest beacon is at x=2307159, y=135337
 | 
				
			||||||
 | 
					Sensor at x=3915820, y=2609677: closest beacon is at x=3687798, y=2823020
 | 
				
			||||||
 | 
					Sensor at x=2959514, y=2529069: closest beacon is at x=2809378, y=2513386
 | 
				
			||||||
 | 
					Sensor at x=1829825, y=2614275: closest beacon is at x=2809378, y=2513386
 | 
				
			||||||
 | 
					Sensor at x=1031015, y=2036184: closest beacon is at x=1213036, y=1428271
 | 
				
			||||||
 | 
					Sensor at x=3894267, y=3758546: closest beacon is at x=3738780, y=3090050
 | 
				
			||||||
 | 
					Sensor at x=2653530, y=445121: closest beacon is at x=2307159, y=135337
 | 
				
			||||||
 | 
					Sensor at x=1528274, y=1670020: closest beacon is at x=1213036, y=1428271
 | 
				
			||||||
 | 
					Sensor at x=3839068, y=2974837: closest beacon is at x=3738780, y=3090050
 | 
				
			||||||
 | 
					Sensor at x=254225, y=9603: closest beacon is at x=278652, y=-182407
 | 
				
			||||||
 | 
					Sensor at x=2214848, y=3333326: closest beacon is at x=2294322, y=3429562
 | 
				
			||||||
 | 
					Sensor at x=1008775, y=292264: closest beacon is at x=278652, y=-182407
 | 
				
			||||||
 | 
					Sensor at x=2072077, y=6712: closest beacon is at x=2307159, y=135337
 | 
				
			||||||
 | 
					Sensor at x=3344028, y=3459786: closest beacon is at x=3738780, y=3090050
 | 
				
			||||||
 | 
					Sensor at x=984627, y=3991112: closest beacon is at x=2294322, y=3429562
 | 
				
			||||||
 | 
					Sensor at x=198206, y=2034713: closest beacon is at x=-362535, y=2000000
 | 
				
			||||||
 | 
					Sensor at x=460965, y=1150404: closest beacon is at x=1213036, y=1428271
 | 
				
			||||||
 | 
					Sensor at x=2198999, y=3584784: closest beacon is at x=2294322, y=3429562
 | 
				
			||||||
 | 
					Sensor at x=3212614, y=2899682: closest beacon is at x=3687798, y=2823020
 | 
				
			||||||
 | 
					Sensor at x=3797078, y=2864795: closest beacon is at x=3687798, y=2823020
 | 
				
			||||||
 | 
					Sensor at x=2465051, y=2871666: closest beacon is at x=2809378, y=2513386
 | 
				
			||||||
 | 
					Sensor at x=2356218, y=3981953: closest beacon is at x=2294322, y=3429562
 | 
				
			||||||
 | 
					Sensor at x=2389861, y=1856461: closest beacon is at x=2809378, y=2513386
 | 
				
			||||||
 | 
					Sensor at x=2852352, y=2506253: closest beacon is at x=2809378, y=2513386
 | 
				
			||||||
 | 
					Sensor at x=2275278, y=742411: closest beacon is at x=2307159, y=135337
 | 
				
			||||||
 | 
					Sensor at x=1562183, y=3626443: closest beacon is at x=2294322, y=3429562
 | 
				
			||||||
 | 
					Sensor at x=44398, y=534916: closest beacon is at x=278652, y=-182407
 | 
				
			||||||
| 
						 | 
					@ -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
 | 
				
			||||||
| 
						 | 
					@ -0,0 +1,55 @@
 | 
				
			||||||
 | 
					use std::fs::File;
 | 
				
			||||||
 | 
					use std::io::{self, BufRead};
 | 
				
			||||||
 | 
					use std::path::Path;
 | 
				
			||||||
 | 
					use std::collections::HashSet;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					fn main() {
 | 
				
			||||||
 | 
					    let file_path = String::from("input");
 | 
				
			||||||
 | 
					    println!("In file {}", file_path);
 | 
				
			||||||
 | 
					    let mut sensors: Vec<(i32, i32)> = vec![];
 | 
				
			||||||
 | 
					    let mut beacon: Vec<(i32, i32)> = vec![];
 | 
				
			||||||
 | 
					    let mut distances: Vec<i32> = vec![];
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    if let Ok(lines) = read_lines(file_path) {
 | 
				
			||||||
 | 
					        // Consumes the iterator, returns an (Optional) String
 | 
				
			||||||
 | 
					        for line in lines {
 | 
				
			||||||
 | 
					            if let Ok(ip) = line {
 | 
				
			||||||
 | 
					                let split = ip.split(" ")
 | 
				
			||||||
 | 
					                    .enumerate()
 | 
				
			||||||
 | 
					                    .filter(|&(i, _)| i == 2 || i == 3 || i == 8 || i == 9)
 | 
				
			||||||
 | 
					                    .map(|(i, v)| v[2..v.len() - 1 + (i == 9) as usize].parse::<i32>().unwrap())
 | 
				
			||||||
 | 
					                    .collect::<Vec<i32>>();
 | 
				
			||||||
 | 
					                sensors.push((split[0], split[1]));
 | 
				
			||||||
 | 
					                beacon.push((split[2], split[3]));
 | 
				
			||||||
 | 
					                distances.push((split[0] - split[2]).abs() + (split[1] - split[3]).abs());
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    let mut count = HashSet::<i32>::new();
 | 
				
			||||||
 | 
					    for (i, _) in sensors.iter().enumerate() {
 | 
				
			||||||
 | 
					        let range = distances[i] - (sensors[i].1 - 2000000).abs() + 1;
 | 
				
			||||||
 | 
					        println!("i {} range {}", i, range);
 | 
				
			||||||
 | 
					        for j in 0..range {
 | 
				
			||||||
 | 
					            if i == 6 {
 | 
				
			||||||
 | 
					                println!("{}, {}", sensors[i].0 + j, sensors[i].0 - j);
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					            count.insert(sensors[i].0 + j);
 | 
				
			||||||
 | 
					            count.insert(sensors[i].0 - j);
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					    println!("{:#?}", distances);
 | 
				
			||||||
 | 
					    if count.contains(&-362535) {
 | 
				
			||||||
 | 
					        println!("Contains key ! {} is answer", count.len() - 1);
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					    if count.contains(&2) {
 | 
				
			||||||
 | 
					        println!("EXAMPLE Contains key ! {} is answer", count.len() - 1);
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					    println!("{}", count.len());
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					fn read_lines<P>(filename: P) -> io::Result<io::Lines<io::BufReader<File>>>
 | 
				
			||||||
 | 
					where P: AsRef<Path>, {
 | 
				
			||||||
 | 
					    let file = File::open(filename)?;
 | 
				
			||||||
 | 
					    Ok(io::BufReader::new(file).lines())
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
| 
						 | 
					@ -0,0 +1,12 @@
 | 
				
			||||||
 | 
					# ---> Rust
 | 
				
			||||||
 | 
					# Generated by Cargo
 | 
				
			||||||
 | 
					# will have compiled files and executables
 | 
				
			||||||
 | 
					/target/
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries
 | 
				
			||||||
 | 
					# More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html
 | 
				
			||||||
 | 
					Cargo.lock
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					# These are backup files generated by rustfmt
 | 
				
			||||||
 | 
					**/*.rs.bk
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -0,0 +1,8 @@
 | 
				
			||||||
 | 
					[package]
 | 
				
			||||||
 | 
					name = "aoc_15b"
 | 
				
			||||||
 | 
					version = "0.1.0"
 | 
				
			||||||
 | 
					edition = "2021"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					[dependencies]
 | 
				
			||||||
| 
						 | 
					@ -0,0 +1,86 @@
 | 
				
			||||||
 | 
					use std::fs::File;
 | 
				
			||||||
 | 
					use std::io::{self, BufRead};
 | 
				
			||||||
 | 
					use std::path::Path;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					fn get_distance(a: (i64, i64), b: (i64, i64)) -> i64 {
 | 
				
			||||||
 | 
					    (a.0 - b.0).abs() + (a.1 - b.1).abs()
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					fn main() {
 | 
				
			||||||
 | 
					    let file_path = String::from("../aoc_15a/input");
 | 
				
			||||||
 | 
					    println!("In file {}", file_path);
 | 
				
			||||||
 | 
					    let mut sensors: Vec<(i64, i64)> = vec![];
 | 
				
			||||||
 | 
					    let mut beacon: Vec<(i64, i64)> = vec![];
 | 
				
			||||||
 | 
					    let mut distances: Vec<i64> = vec![];
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    if let Ok(lines) = read_lines(file_path) {
 | 
				
			||||||
 | 
					        // Consumes the iterator, returns an (Optional) String
 | 
				
			||||||
 | 
					        for line in lines {
 | 
				
			||||||
 | 
					            if let Ok(ip) = line {
 | 
				
			||||||
 | 
					                let split = ip.split(" ")
 | 
				
			||||||
 | 
					                    .enumerate()
 | 
				
			||||||
 | 
					                    .filter(|&(i, _)| i == 2 || i == 3 || i == 8 || i == 9)
 | 
				
			||||||
 | 
					                    .map(|(i, v)| v[2..v.len() - 1 + (i == 9) as usize].parse::<i64>().unwrap())
 | 
				
			||||||
 | 
					                    .collect::<Vec<i64>>();
 | 
				
			||||||
 | 
					                sensors.push((split[0], split[1]));
 | 
				
			||||||
 | 
					                beacon.push((split[2], split[3]));
 | 
				
			||||||
 | 
					                distances.push((split[0] - split[2]).abs() + (split[1] - split[3]).abs());
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    let mut tool: Vec<(i64, i64, i64)> = vec![];
 | 
				
			||||||
 | 
					    for (i, _) in sensors.iter().enumerate() {
 | 
				
			||||||
 | 
					        for j in i + 1..sensors.len() {
 | 
				
			||||||
 | 
					            let distance = (sensors[i].0 - sensors[j].0).abs() + (sensors[i].1 - sensors[j].1).abs();
 | 
				
			||||||
 | 
					            let simple = (distance - distances[i] - distances[j]).abs();
 | 
				
			||||||
 | 
					            if simple == 2 {
 | 
				
			||||||
 | 
					                println!("Found something between {} and {}, {} {} {} {}", i, j, simple, distance, distances[i], distances[j]);
 | 
				
			||||||
 | 
					                let mut x = 0;
 | 
				
			||||||
 | 
					                let y = (sensors[i].1 + sensors[j].1) / 2;
 | 
				
			||||||
 | 
					                let mut d1 = (get_distance(sensors[i], (x, y)) - distances[i]).abs();
 | 
				
			||||||
 | 
					                let mut d2 = (get_distance(sensors[j], (x, y)) - distances[j]).abs();
 | 
				
			||||||
 | 
					                while d1 + d2 > 2 {
 | 
				
			||||||
 | 
					                    d2 = (get_distance(sensors[j], (x, y)) - distances[j]).abs();
 | 
				
			||||||
 | 
					                    d1 = (get_distance(sensors[i], (x, y)) - distances[i]).abs();
 | 
				
			||||||
 | 
					                    x += 1;
 | 
				
			||||||
 | 
					                }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					                println!("{} {} {} {}", d1, d2, x, y);
 | 
				
			||||||
 | 
					                d2 = (get_distance(sensors[j], (x + 1, y + 1)) - distances[j]).abs();
 | 
				
			||||||
 | 
					                d1 = (get_distance(sensors[i], (x + 1, y + 1)) - distances[i]).abs();
 | 
				
			||||||
 | 
					                if d1 + d2 == 2 {
 | 
				
			||||||
 | 
					                    tool.push((x, y, 1));
 | 
				
			||||||
 | 
					                }
 | 
				
			||||||
 | 
					                else {
 | 
				
			||||||
 | 
					                    tool.push((x, y, -1));
 | 
				
			||||||
 | 
					                }
 | 
				
			||||||
 | 
					                println!("{} {} {} {}", d1, d2, x, y);
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    println!("{:#?}", tool);
 | 
				
			||||||
 | 
					    let dir = tool[0].2;
 | 
				
			||||||
 | 
					    let mut a = (tool[0].0 - 3000000, tool[0].1 - 3000000 * dir);
 | 
				
			||||||
 | 
					    let b = (tool[1].0, tool[1].1);
 | 
				
			||||||
 | 
					    while (a.0 - b.0).abs() != (a.1 - b.1).abs() {
 | 
				
			||||||
 | 
					        a.0 += 1;
 | 
				
			||||||
 | 
					        a.1 += dir;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    println!("{:?}", a);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    println!("{} {}", distances[6], get_distance(sensors[6], a));
 | 
				
			||||||
 | 
					    println!("{} {}", distances[29], get_distance(sensors[29], a));
 | 
				
			||||||
 | 
					    println!("{} {}", distances[24], get_distance(sensors[24], a));
 | 
				
			||||||
 | 
					    println!("{} {}", distances[31], get_distance(sensors[31], a));
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    println!("{}", a.0 * 4000000 + a.1);
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					fn read_lines<P>(filename: P) -> io::Result<io::Lines<io::BufReader<File>>>
 | 
				
			||||||
 | 
					where P: AsRef<Path>, {
 | 
				
			||||||
 | 
					    let file = File::open(filename)?;
 | 
				
			||||||
 | 
					    Ok(io::BufReader::new(file).lines())
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
		Loading…
	
		Reference in New Issue