This commit is contained in:
JonOfUs 2024-12-08 17:50:46 +01:00
parent f6ebab690b
commit 6206cc2e72
4 changed files with 206 additions and 5 deletions

12
res/08/example Normal file
View file

@ -0,0 +1,12 @@
............
........0...
.....0......
.......0....
....0.......
......A.....
............
............
........A...
.........A..
............
............

50
res/08/input Normal file
View file

@ -0,0 +1,50 @@
.E..........m..0N.........f.......................
........N........P0...............................
.......j..................................F.......
........1j............P........................C..
...........................3..K......f..........E.
...........V...y...0.....................F........
1.......j.....P....y.N.......................F....
....................m...................C.........
..L......P....p..................w.m..............
............E......p..AU........8......f..........
..............C...............w....d..............
j1...............E..........3.........f........w..
.................p...A..........3.................
.................3..p........KU...w..r..F.........
7.........y........8.......................r......
........y..u......K...............................
...1..................8....C...K..................
...........h.......................6..............
......................U.........A.r..t........6...
...........5.........8..c.........................
.................U................t...............
.....L...O...................t.............d......
.........7........................................
......L..H...c.....9....t.................6.......
...........................c.M..................4.
.....R..7...O.....................................
.......................9......................d...
..................................................
.........L..9...R..........................6c.....
..M.....T.5.................................d.....
.......5OR...................T....................
.......D......o.........v...................r.....
...u....o.........5...............................
.......WR.....Y...........................e...4...
T............O......M..................4..a.......
.Y...................M............................
........W..D...............oh............e........
.......7......Do...................A...e.......4..
.W...Y..D........................h...v..........e.
..........V.....9.l.......h.......a.........n..v..
.......................H.....a2...................
..................................................
...V............Y....J..H2................vn......
..............................H2.................n
................V..........l...........k..........
.T..u........................J...ak...............
..................J.....l.........................
.................l................................
......u.........................................n.
......................J..k............2...........

106
src/days/d08.rs Normal file
View file

@ -0,0 +1,106 @@
use std::collections::{HashMap, HashSet};
use std::fs;
pub fn solve() {
let path = "res/08/input";
let contents = fs::read_to_string(path).expect("Something went wrong reading the file");
let mut antennas: HashMap<char, Vec<(isize, isize)>> = HashMap::new();
let rows = contents.lines().count();
let cols = contents.lines().next().unwrap().chars().count();
for (i, line) in contents.lines().enumerate() {
for (j, c) in line.chars().enumerate() {
match c {
'.' => {}
_ => {
let entry = antennas.entry(c).or_insert(Vec::new());
entry.push((i as isize, j as isize));
}
}
}
}
let mut antinodes: HashSet<(isize, isize)> = HashSet::new();
antennas.iter().for_each(|(_, v)| {
v.iter().enumerate().for_each(|(i, (x1, y1))| {
v.iter().skip(i + 1).for_each(|(x2, y2)| {
if x1 == x2 && y1 == y2 {
return;
}
let (a_x1, a_y1) = (x1 + (x1 - x2), y1 + (y1 - y2));
let (a_x2, a_y2) = (x2 + (x2 - x1), y2 + (y2 - y1));
if a_x1 >= 0 && a_y1 >= 0 && a_x1 < rows as isize && a_y1 < cols as isize {
antinodes.insert((a_x1, a_y1));
}
if a_x2 >= 0 && a_y2 >= 0 && a_x2 < rows as isize && a_y2 < cols as isize {
antinodes.insert((a_x2, a_y2));
}
})
})
});
let result = antinodes.len();
println!("Result 1: {}", result);
let mut antinodes: HashSet<(isize, isize)> = HashSet::new();
antennas.iter().for_each(|(_, v)| {
v.iter().enumerate().for_each(|(i, (x1, y1))| {
v.iter().skip(i + 1).for_each(|(x2, y2)| {
if x1 == x2 && y1 == y2 {
return;
}
let ax1_diff = x1 - x2;
let ay1_diff = y1 - y2;
let a1_gcd = gcd(ax1_diff, ay1_diff);
let mut cnt = 0;
loop {
let (ax, ay) = (x1 + ax1_diff * cnt / a1_gcd, y1 + ay1_diff * cnt / a1_gcd);
if ax < 0 || ay < 0 || ax >= rows as isize || ay >= cols as isize {
break;
}
antinodes.insert((ax, ay));
cnt += 1;
}
let ax2_diff = x2 - x1;
let ay2_diff = y2 - y1;
let a2_gcd = gcd(ax2_diff, ay2_diff);
cnt = 0;
loop {
let (ax, ay) = (x2 + ax2_diff * cnt / a2_gcd, y2 + ay2_diff * cnt / a2_gcd);
if ax < 0 || ay < 0 || ax >= rows as isize || ay >= cols as isize {
break;
}
antinodes.insert((ax, ay));
cnt += 1;
}
})
})
});
let result = antinodes.len();
println!("Result 2: {}", result);
}
fn gcd(mut a: isize, mut b: isize) -> isize {
a = a.abs();
b = b.abs();
while b != 0 {
let temp = b;
b = a % b;
a = temp;
}
a
}

View file

@ -4,12 +4,45 @@ extern crate scan_fmt;
use std::time::Instant; use std::time::Instant;
fn main() { fn main() {
//days::d07::solve() if std::env::args().len() > 1 {
_all_days() if let Some(day) = std::env::args().nth(1) {
match day.as_str() {
"all" => all_days(),
"1" => days::d01::solve(),
"2" => days::d02::solve(),
"3" => days::d03::solve(),
"4" => days::d04::solve(),
"5" => days::d05::solve(),
"6" => days::d06::solve(),
"7" => days::d07::solve(),
"8" => days::d08::solve(),
//"9" => days::d09::solve(),
//"10" => days::d10::solve(),
//"11" => days::d11::solve(),
//"12" => days::d12::solve(),
//"13" => days::d13::solve(),
//"14" => days::d14::solve(),
//"15" => days::d15::solve(),
//"16" => days::d16::solve(),
//"17" => days::d17::solve(),
//"18" => days::d18::solve(),
//"19" => days::d19::solve(),
//"20" => days::d20::solve(),
//"21" => days::d21::solve(),
//"22" => days::d22::solve(),
//"23" => days::d23::solve(),
//"24" => days::d24::solve(),
//"25" => days::d25::solve(),
_ => println!("Day not implemented"),
}
}
} else {
all_days();
}
} }
#[allow(unreachable_code, unused)] #[allow(unused)]
fn _all_days() { fn all_days() {
let start = Instant::now(); let start = Instant::now();
let mut time = start.clone(); let mut time = start.clone();
@ -41,11 +74,11 @@ fn _all_days() {
days::d07::solve(); days::d07::solve();
time = _print_elapsed(time); time = _print_elapsed(time);
/*
println!("\nDay 8"); println!("\nDay 8");
days::d08::solve(); days::d08::solve();
time = _print_elapsed(time); time = _print_elapsed(time);
/*
println!("\nDay 9"); println!("\nDay 9");
days::d09::solve(); days::d09::solve();
time = _print_elapsed(time); time = _print_elapsed(time);