Dag 8 aoc voltooid, opeens heel eenvoudig
This commit is contained in:
7
advent_of_code/2024/8/Cargo.toml
Normal file
7
advent_of_code/2024/8/Cargo.toml
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
[package]
|
||||||
|
name = "main"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2021"
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
nom = "7.1.3"
|
||||||
50
advent_of_code/2024/8/input.txt
Normal file
50
advent_of_code/2024/8/input.txt
Normal file
@@ -0,0 +1,50 @@
|
|||||||
|
..........1.............TE........................
|
||||||
|
....................................R.............
|
||||||
|
..................................................
|
||||||
|
.......................j.....Q....................
|
||||||
|
...................A................8.............
|
||||||
|
...........................s.......9...........k..
|
||||||
|
q.E..............6...............1R.w.........k...
|
||||||
|
..6...E..............1.........R...............t..
|
||||||
|
.....r.Q......6........Re..T..............9.......
|
||||||
|
.............................T........9...........
|
||||||
|
...............................................wv.
|
||||||
|
.P............A..................8.v....s.k.......
|
||||||
|
.q..................A......k.........8............
|
||||||
|
..........o.....1.....W..H............8.......w...
|
||||||
|
..Q........P.........O.........e...N.W............
|
||||||
|
P................z.........o.............N.......w
|
||||||
|
..............o.....p..........Z.s..........N.....
|
||||||
|
.....O.x......K.....................v..aN.........
|
||||||
|
..O...............U.....H.......t.................
|
||||||
|
.E.......q...6.....i..............................
|
||||||
|
..............z..........o...i...........aW.......
|
||||||
|
....O........r.............e.....Wt...............
|
||||||
|
...............U.7i........H......h........t......
|
||||||
|
......Q.......n..2...I...A....i.p.................
|
||||||
|
...........2...9n.................s........j......
|
||||||
|
..q................Ur..........p..................
|
||||||
|
.............n.................K..................
|
||||||
|
.....S....z.........I.....H.............e.j.......
|
||||||
|
..................7..prD..K...d...................
|
||||||
|
S.........V.....7....K............................
|
||||||
|
......................................0...........
|
||||||
|
..................................................
|
||||||
|
..................2..........I....j.Z.............
|
||||||
|
....................X.............J..Z....a.......
|
||||||
|
........SX............................x......0J...
|
||||||
|
................U....n........x...............0...
|
||||||
|
.........S......X................x....a...........
|
||||||
|
...5.......X.......................02.............
|
||||||
|
...............V.........................d...J....
|
||||||
|
.............................u.......4............
|
||||||
|
.....5...........................u.4..............
|
||||||
|
....5.............................................
|
||||||
|
......V................................3..........
|
||||||
|
......D..........................................d
|
||||||
|
....D.................................4...........
|
||||||
|
.....h....................................d7......
|
||||||
|
..............................P...................
|
||||||
|
.........D......h........3................u...4...
|
||||||
|
.............h..5.....3...........u.....I.........
|
||||||
|
..........3......V.............................J..
|
||||||
131
advent_of_code/2024/8/src/main.rs
Normal file
131
advent_of_code/2024/8/src/main.rs
Normal file
@@ -0,0 +1,131 @@
|
|||||||
|
use std::collections::{HashMap, HashSet};
|
||||||
|
|
||||||
|
use nom::{
|
||||||
|
character::complete::{multispace1, not_line_ending},
|
||||||
|
error::VerboseError,
|
||||||
|
multi::separated_list1,
|
||||||
|
};
|
||||||
|
|
||||||
|
fn parse(input: &str) -> ((i32, i32), HashMap<char, Vec<(i32, i32)>>) {
|
||||||
|
let (input, parse_result) =
|
||||||
|
separated_list1(multispace1::<&str, VerboseError<_>>, not_line_ending)(input).unwrap();
|
||||||
|
|
||||||
|
if !input.is_empty() {
|
||||||
|
panic!("Parsing yields additional input");
|
||||||
|
}
|
||||||
|
|
||||||
|
let mut result: HashMap<char, Vec<(i32, i32)>> = HashMap::new();
|
||||||
|
let dim = (parse_result[0].len() as i32, parse_result.len() as i32 - 1);
|
||||||
|
|
||||||
|
for (y, line) in parse_result.into_iter().enumerate() {
|
||||||
|
for (x, c) in line.chars().enumerate() {
|
||||||
|
if c != '.' && c != '\n' {
|
||||||
|
match result.get_mut(&c) {
|
||||||
|
None => {
|
||||||
|
result.insert(c, vec![(x as i32, y as i32)]);
|
||||||
|
}
|
||||||
|
Some(v) => v.push((x as i32, y as i32)),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
(dim, result)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn check_in_rectangle(point: (i32, i32), h: i32, w: i32) -> bool {
|
||||||
|
point.0 >= 0 && point.1 >= 0 && point.0 < w && point.1 < h
|
||||||
|
}
|
||||||
|
|
||||||
|
fn solve_1(input: &str) -> usize {
|
||||||
|
let (dim, antennae) = parse(input);
|
||||||
|
let dim = (dim.0 as i32, dim.1 as i32);
|
||||||
|
let mut result: HashSet<(i32, i32)> = HashSet::new();
|
||||||
|
|
||||||
|
for (_, v) in antennae.into_iter() {
|
||||||
|
let l = v.len();
|
||||||
|
for i in 0..l {
|
||||||
|
let a = v[i];
|
||||||
|
for j in i + 1..l {
|
||||||
|
let b = v[j];
|
||||||
|
|
||||||
|
let dif = (a.0 - b.0, a.1 - b.1);
|
||||||
|
|
||||||
|
if check_in_rectangle((a.0 + dif.0, a.1 + dif.1), dim.1, dim.0) {
|
||||||
|
result.insert((a.0 + dif.0, a.1 + dif.1));
|
||||||
|
}
|
||||||
|
if check_in_rectangle((b.0 - dif.0, b.1 - dif.1), dim.1, dim.0) {
|
||||||
|
result.insert((b.0 - dif.0, b.1 - dif.1));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
result.len()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn solve_2(input: &str) -> usize {
|
||||||
|
let (dim, antennae) = parse(input);
|
||||||
|
let dim = (dim.0 as i32, dim.1 as i32);
|
||||||
|
|
||||||
|
let mut result: HashSet<(i32, i32)> = HashSet::new();
|
||||||
|
|
||||||
|
for (_, v) in antennae.into_iter() {
|
||||||
|
let l = v.len();
|
||||||
|
for i in 0..l {
|
||||||
|
for j in i + 1..l {
|
||||||
|
let mut a = v[i];
|
||||||
|
let mut b = v[j];
|
||||||
|
|
||||||
|
let dif = (a.0 - b.0, a.1 - b.1);
|
||||||
|
|
||||||
|
while check_in_rectangle(a, dim.1, dim.0) {
|
||||||
|
result.insert(a);
|
||||||
|
a = (a.0 + dif.0, a.1 + dif.1);
|
||||||
|
}
|
||||||
|
while check_in_rectangle(b, dim.1, dim.0) {
|
||||||
|
result.insert(b);
|
||||||
|
b = (b.0 - dif.0, b.1 - dif.1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
result.len()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
println!("Hello, this is Patrick!");
|
||||||
|
|
||||||
|
let input = include_str!("../input.txt");
|
||||||
|
|
||||||
|
let result_1 = solve_1(input);
|
||||||
|
println!(
|
||||||
|
"The number of unique locations with an antinode are {}",
|
||||||
|
result_1
|
||||||
|
);
|
||||||
|
|
||||||
|
let result_2 = solve_2(input);
|
||||||
|
println!(
|
||||||
|
"The new number of unique locations with an antinode are {}",
|
||||||
|
result_2
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
|
||||||
|
mod tests {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_1() {
|
||||||
|
let test_input = include_str!("../test.txt");
|
||||||
|
assert_eq!(solve_1(test_input), 14);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_2() {
|
||||||
|
let test_input = include_str!("../test.txt");
|
||||||
|
assert_eq!(solve_2(test_input), 34);
|
||||||
|
}
|
||||||
|
}
|
||||||
12
advent_of_code/2024/8/test.txt
Normal file
12
advent_of_code/2024/8/test.txt
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
............
|
||||||
|
........0...
|
||||||
|
.....0......
|
||||||
|
.......0....
|
||||||
|
....0.......
|
||||||
|
......A.....
|
||||||
|
............
|
||||||
|
............
|
||||||
|
........A...
|
||||||
|
.........A..
|
||||||
|
............
|
||||||
|
............
|
||||||
Reference in New Issue
Block a user