diff --git a/advent_of_code/2022/15/Cargo.lock b/advent_of_code/2022/15/Cargo.lock new file mode 100644 index 0000000..b8ae363 --- /dev/null +++ b/advent_of_code/2022/15/Cargo.lock @@ -0,0 +1,32 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "main" +version = "0.1.0" +dependencies = [ + "nom", +] + +[[package]] +name = "memchr" +version = "2.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2dffe52ecf27772e601905b7522cb4ef790d2cc203488bbd0e2fe85fcb74566d" + +[[package]] +name = "minimal-lexical" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "68354c5c6bd36d73ff3feceb05efa59b6acb7626617f4962be322a825e61f79a" + +[[package]] +name = "nom" +version = "7.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e5507769c4919c998e69e49c839d9dc6e693ede4cc4290d6ad8b41d4f09c548c" +dependencies = [ + "memchr", + "minimal-lexical", +] diff --git a/advent_of_code/2022/15/Cargo.toml b/advent_of_code/2022/15/Cargo.toml new file mode 100644 index 0000000..200567e --- /dev/null +++ b/advent_of_code/2022/15/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "main" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +nom = "7.1.2" diff --git a/advent_of_code/2022/15/input.txt b/advent_of_code/2022/15/input.txt new file mode 100644 index 0000000..7f37eaa --- /dev/null +++ b/advent_of_code/2022/15/input.txt @@ -0,0 +1,33 @@ +Sensor at x=1943362, y=12808: closest beacon is at x=1861152, y=-42022 +Sensor at x=906633, y=3319637: closest beacon is at x=2096195, y=3402757 +Sensor at x=2358896, y=2158796: closest beacon is at x=2331052, y=2934800 +Sensor at x=1787606, y=3963631: closest beacon is at x=2096195, y=3402757 +Sensor at x=2282542, y=3116014: closest beacon is at x=2331052, y=2934800 +Sensor at x=173912, y=1873897: closest beacon is at x=429790, y=2000000 +Sensor at x=3391153, y=3437167: closest beacon is at x=3720655, y=3880705 +Sensor at x=3834843, y=2463103: closest beacon is at x=2971569, y=2563051 +Sensor at x=3917316, y=3981011: closest beacon is at x=3720655, y=3880705 +Sensor at x=1466100, y=1389028: closest beacon is at x=429790, y=2000000 +Sensor at x=226600, y=3967233: closest beacon is at x=85598, y=4102832 +Sensor at x=1757926, y=2834180: closest beacon is at x=2331052, y=2934800 +Sensor at x=2176953, y=3240563: closest beacon is at x=2096195, y=3402757 +Sensor at x=2883909, y=2533883: closest beacon is at x=2971569, y=2563051 +Sensor at x=376161, y=2533578: closest beacon is at x=429790, y=2000000 +Sensor at x=3015271, y=3913673: closest beacon is at x=3720655, y=3880705 +Sensor at x=490678, y=388548: closest beacon is at x=429790, y=2000000 +Sensor at x=2725765, y=2852933: closest beacon is at x=2331052, y=2934800 +Sensor at x=86373, y=2839828: closest beacon is at x=429790, y=2000000 +Sensor at x=1802070, y=14830: closest beacon is at x=1861152, y=-42022 +Sensor at x=19628, y=1589839: closest beacon is at x=429790, y=2000000 +Sensor at x=2713787, y=3381887: closest beacon is at x=2096195, y=3402757 +Sensor at x=2148471, y=3729393: closest beacon is at x=2096195, y=3402757 +Sensor at x=3999318, y=3263346: closest beacon is at x=3720655, y=3880705 +Sensor at x=575700, y=1390576: closest beacon is at x=429790, y=2000000 +Sensor at x=273266, y=2050976: closest beacon is at x=429790, y=2000000 +Sensor at x=3008012, y=993590: closest beacon is at x=2971569, y=2563051 +Sensor at x=3306379, y=2782128: closest beacon is at x=2971569, y=2563051 +Sensor at x=44975, y=3820788: closest beacon is at x=85598, y=4102832 +Sensor at x=2941700, y=2536797: closest beacon is at x=2971569, y=2563051 +Sensor at x=2040164, y=102115: closest beacon is at x=1861152, y=-42022 +Sensor at x=3928008, y=3692684: closest beacon is at x=3720655, y=3880705 +Sensor at x=3905950, y=222812: closest beacon is at x=4759853, y=-796703 \ No newline at end of file diff --git a/advent_of_code/2022/15/src/main.rs b/advent_of_code/2022/15/src/main.rs new file mode 100644 index 0000000..323ddf4 --- /dev/null +++ b/advent_of_code/2022/15/src/main.rs @@ -0,0 +1,98 @@ +use std::{collections::HashSet, hash::Hash}; +use nom::{ + IResult, + character::complete::{self,multispace1}, + multi::separated_list1, + sequence::{preceded, separated_pair}, bytes::streaming::tag, +}; + +fn parse_input(input: &str) -> IResult<&str, Vec<((i32, i32), (i32, i32))>> { + let (input, coords) = separated_list1( + multispace1, + preceded( + tag("Sensor at x="), + separated_pair( + separated_pair( + complete::i32, + tag(", y="), + complete::i32, + ), + tag(": closest beacon is at x="), + separated_pair( + complete::i32, + tag(", y="), + complete::i32, + )) + ) + )(input)?; + + Ok((input, coords)) +} + +fn manhattan_dist(a: (i32, i32), b: (i32, i32)) -> i32 { + return (a.0 - b.0).abs() + (a.1 - b.1).abs(); +} + +fn fill_map(beacon_map: &mut HashSet<(i32, i32)>, sensor_coord: (i32, i32), beacon_coord: (i32, i32)) { + let dist = manhattan_dist(sensor_coord, beacon_coord); + let (sx, sy) = sensor_coord; + + for i in 0..=dist { + for j in 0..=i { + beacon_map.insert((sx + i, sy + j)); + beacon_map.insert((sx - i, sy + j)); + beacon_map.insert((sx + i, sy - j)); + beacon_map.insert((sx - i, sy - j)); + } + } +} + +fn fill_line(beacon_line: &mut HashSet, sensor_coord: (i32, i32), beacon_coord: (i32, i32), test_line: i32) { + let dist = manhattan_dist(sensor_coord, beacon_coord); + let (sx, sy) = sensor_coord; + + let cross_section_len = dist - (test_line - sy).abs(); + if cross_section_len >= 0 { + for i in 0..=cross_section_len { + beacon_line.insert(sx + i); + beacon_line.insert(sx - i); + } + } +} + +fn main() { + let test_y = 2000000; + + let input_text = include_str!("../input.txt"); + let (_rest, coord_list) = parse_input(input_text).unwrap(); + + // Testing the parsing + + // for ((sx, sy), (bx, by)) in coord_list { + // println!("Sensor at x={sx}, y={sy}: closest beacon is at x={bx}, y={by}"); + // } + + + // Naive method + + // let mut beacon_map = HashSet::new(); + + // for (sensor, beacon) in coord_list { + // fill_map(&mut beacon_map, sensor, beacon); + // } + + // let positions = beacon_map.into_iter().filter(|(_, y)| *y == test_y).count(); + + // println!("Number of positions with y={test_y}: {positions}"); + + + // A slightly less naive approach + + let mut beacon_line = HashSet::new(); + + for (sensor, beacon) in coord_list { + fill_line(&mut beacon_line, sensor, beacon, test_y); + } + + println!("Number of positions with y={test_y}: {}", beacon_line.len() - 1); +} diff --git a/advent_of_code/2022/15/test.txt b/advent_of_code/2022/15/test.txt new file mode 100644 index 0000000..652e631 --- /dev/null +++ b/advent_of_code/2022/15/test.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