Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions aoc_utils/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions aoc_utils/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ version = "0.1.0"
edition = "2021"

[dependencies]
num-traits = "0.2.19"
15 changes: 15 additions & 0 deletions aoc_utils/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use num_traits::{zero, PrimInt};
use std::collections::{HashMap, HashSet};
use std::fs::read_to_string;
use std::ops::RemAssign;

pub fn read_lines(filename: &str) -> Vec<String> {
read_to_string(filename)
Expand Down Expand Up @@ -62,3 +64,16 @@ pub fn primes(number: &u32) -> impl Iterator<Item = u32> {

(2..n + 1).filter(move |&i| !&sieve.contains(&i))
}

pub fn gcd<T: PrimInt + RemAssign>(mut n: T, mut m: T) -> T {
let zero = T::zero();

assert!(n != zero && m != zero);
while m != zero {
if m < n {
std::mem::swap(&mut m, &mut n);
}
m %= n;
}
n
}
18 changes: 18 additions & 0 deletions year_2023/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 18 additions & 0 deletions year_2024/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

50 changes: 50 additions & 0 deletions year_2024/input/day8.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
..................................................
.r................................................
..........................I.......................
........................I.........................
................................................M.
............h......................A..............
..7....................I.........h................
......7..................................M....9...
.o.....U..........................................
......................................O...........
....c.................J................O...M...A..
..................................................
...R...7..........................................
..............r...................................
...................J..................9...........
...7..K......UJ...................................
......0...U.........................x.............
.......R.......0..B......................x........
.......................k.....Z.......9............
.......L.........I.....J............m.............
.....K.BR........r.0.C............................
.......K.BR......c................................
..................h....m....Al...........H........
..............L..k.......h...m..........x..9......
........................Z.....m........xO.........
..........0................l......................
.6..................b.............................
............k...o..............Z..................
........4.....o...........L.......................
....................Xo............................
...........8..B..L.........i......................
..z...............bX..........A...................
j........z...X......C.......i........5............
.b...H6.......................U.......l...........
..................X...............................
...6......................Z..........a............
....6........c............5.........1.............
.4.......................5........................
..........k.......H1l.............................
2.................C.......i...................u...
.............a....2...............................
.....z....H.......1..8.....................u......
...........j...b..................................
3.........j.........................a.............
...4................a.............................
..M................j.....1..........5.............
............................................u.....
..4..3...........i................................
z3.................2..............................
..........8..................2.C..................
119 changes: 119 additions & 0 deletions year_2024/src/day8.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
use std::collections::{HashMap, HashSet};
use std::ops::{Add, RemAssign};

pub fn execute() -> String {
let data = aoc_utils::read_lines("input/day8.txt");
let city = City::from_lines(data);

let part1 = city.find_antinodes(false).len();
let part2 = city.find_antinodes(true).len();

format!("{} {}", part1, part2)
}

struct City {
size: isize,
antennas: HashMap<char, Vec<(isize, isize)>>,
}

impl City {
fn from_lines(lines: Vec<String>) -> City {
let size = lines.len() as isize;

let mut antennas = HashMap::new();
for (j, line) in lines.iter().enumerate() {
assert_eq!(line.len() as isize, size);
for (i, c) in line.chars().enumerate() {
if c != '.' {
antennas
.entry(c)
.or_insert_with(Vec::new)
.push((i as isize, j as isize));
}
}
}

City { size, antennas }
}

fn find_antinodes(&self, part2: bool) -> HashSet<(isize, isize)> {
let mut result = HashSet::new();
for (_freq, positions) in self.antennas.iter() {
for a in positions {
for b in positions {
if a == b {
continue;
}
let d = (b.0 - a.0, b.1 - a.1);

let (start, step) = if part2 {
let dtemp = d.clone();
let gcd = aoc_utils::gcd(dtemp.0.abs(), dtemp.1.abs());
(a, (d.0 / gcd, d.1 / gcd))
} else {
(b, d)
};

let mut c = start.clone();
loop {
c.0 += step.0;
c.1 += step.1;

if c.0 < 0 || c.0 >= self.size || c.1 < 0 || c.1 >= self.size {
break;
}
result.insert(c.clone());
if !part2 {
break;
}
}
}
}
}
result
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_mine() {
assert_eq!(execute(), String::from("259 927"));
}

#[test]
fn test_from_lines() {
let city = City::from_lines(_example());
assert_eq!(city.size, 12);
assert_eq!(city.antennas.len(), 2);
assert_eq!(city.antennas[&'0'].len(), 4);
assert_eq!(city.antennas[&'A'].len(), 3);
}

#[test]
fn test_find_antinodes() {
let city = City::from_lines(_example());

assert_eq!(city.find_antinodes(false).len(), 14);
assert_eq!(city.find_antinodes(true).len(), 34);
}

fn _example() -> Vec<String> {
vec![
String::from("............"),
String::from("........0..."),
String::from(".....0......"),
String::from(".......0...."),
String::from("....0......."),
String::from("......A....."),
String::from("............"),
String::from("............"),
String::from("........A..."),
String::from(".........A.."),
String::from("............"),
String::from("............"),
]
}
}
2 changes: 2 additions & 0 deletions year_2024/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ mod day4;
mod day5;
mod day6;
mod day7;
mod day8;

fn main() {
println!("Day 1: {}", day1::execute());
Expand All @@ -15,4 +16,5 @@ fn main() {
println!("Day 5: {}", day5::execute());
println!("Day 6: {}", day6::execute());
println!("Day 7: {}", day7::execute());
println!("Day 8: {}", day8::execute());
}
Loading