Skip to content

Commit

Permalink
Add day 8
Browse files Browse the repository at this point in the history
  • Loading branch information
qselle committed Dec 14, 2024
1 parent edeb213 commit 11b2a0f
Show file tree
Hide file tree
Showing 7 changed files with 227 additions and 7 deletions.
16 changes: 16 additions & 0 deletions 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 Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@ edition = "2021"
[dependencies]
aoc-runner = "0.3.0"
aoc-runner-derive = "0.3.0"
itertools = "0.13.0"
memoize = "0.4.2"
regex = "1.11.1"
50 changes: 50 additions & 0 deletions input/2024/day8.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
...........................A.............W........
.......x..........AZ......P.........m......k..W...
....v.....Z..K...V..........A.......R....f........
.............d..........V.............2...........
.........Z........d...V.....B....C.....e..........
............Z.vA...........m...................s.W
...x..o.....................ek....................
...O.....VK..................R......B.............
.........O..................my....kB..............
.v...................y..........C........B.....z..
xb.v..................................C..z........
.........................2.ey.....................
..K.......................y...................s...
..........................................z.......
.......................2.......R........z......F..
O.....d..................D..k.........F...........
..........O..........D.............s....E.........
..o......9....................D........C..s.......
.....o..r....................c...................m
.......P..............................e...........
..............1......................d............
...................o...............3..............
........................c.......24..........S.....
....x..................c.............F4.........S.
P............N..8.......W.D.......................
.....K............1.8.............................
.........P..............Q...M.....................
9...................R........4...8.....0..........
....n.........................F...................
........Y...n......1......................3..J....
.........................3...8....................
...n...M............................0.......ja...S
....f................................6.....S.....E
....................i.........M...J...............
...r..........q..........1......l..0.....L........
.........f7....i.Nc......................j.......l
..9....Y7......X.........Q...M5....J4...........3.
.Y........NX..I............Q........L.............
......Xw...nb.............0..............l6.......
......b.....f....5..q.....................a..6....
.......5..........iq.9.....p..........a...........
........X........I................p...6...........
..................N.............L.........j.......
...b.7.......................p....Q......E........
....Y....7......................p................j
.......r..........................................
.................i...................a............
..w.........5.....................................
......w........I..............J...................
.w............r.....................lL............
10 changes: 10 additions & 0 deletions src/day12.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// use aoc_runner_derive::{aoc, aoc_generator};

// #[aoc_generator(day8)]
// pub fn input_generator(input: &str) -> (Vec<usize>, Vec<usize>) {

// #[aoc(day8, part1)]
// pub fn part1(input: &[Equation]) -> usize {

// #[aoc(day8, part2)]
// pub fn part2(input: &[Equation]) -> usize {
146 changes: 139 additions & 7 deletions src/day8.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,142 @@
// use aoc_runner_derive::{aoc, aoc_generator};
use aoc_runner_derive::{aoc, aoc_generator};
use std::collections::{HashMap, HashSet};

// #[aoc_generator(day8)]
// pub fn input_generator(input: &str) -> (Vec<usize>, Vec<usize>) {
#[derive(Debug)]
pub struct City {
frequencies: HashMap<char, HashSet<(usize, usize)>>,
y_len: usize,
x_len: usize,
}

// #[aoc(day8, part1)]
// pub fn part1(input: &[Equation]) -> usize {
#[aoc_generator(day8)]
pub fn input_generator(input: &str) -> City {
let mut frequencies: HashMap<char, HashSet<(usize, usize)>> = HashMap::new();
let map: Vec<Vec<char>> = input
.lines()
.enumerate()
.map(|(y, l)| {
l.chars()
.enumerate()
.map(|(x, c)| {
if c != '.' {
frequencies.entry(c).or_default().insert((y, x));
}
c
})
.collect()
})
.collect();
City {
y_len: map.len(),
x_len: map[0].len(),
frequencies,
}
}

// #[aoc(day8, part2)]
// pub fn part2(input: &[Equation]) -> usize {
#[aoc(day8, part1)]
pub fn part1(input: &City) -> usize {
let mut antinodes: HashSet<(usize, usize)> = HashSet::new();
let mut count = 0;
for positions in input.frequencies.values() {
for current_pos in positions {
for around_pos in positions {
if current_pos == around_pos {
continue;
}
let y = (current_pos.0 * 2).wrapping_sub(around_pos.0);
let x = (current_pos.1 * 2).wrapping_sub(around_pos.1);
if y < input.y_len && x < input.x_len && !antinodes.contains(&(y, x)) {
count += 1;
antinodes.insert((y, x));
}
}
}
}
count
}

#[aoc(day8, part2)]
pub fn part2(input: &City) -> usize {
let mut antinodes: HashSet<(usize, usize)> = HashSet::new();
let mut count = 0;
for positions in input.frequencies.values() {
for current_pos in positions {
for around_pos in positions {
if current_pos == around_pos {
continue;
}
let delta_y: isize = current_pos.0 as isize - around_pos.0 as isize;
let delta_x: isize = current_pos.1 as isize - around_pos.1 as isize;

let mut y: isize = around_pos.0 as isize;
let mut x: isize = around_pos.1 as isize;
loop {
y -= delta_y;
x -= delta_x;
if x < 0 || y < 0 || x >= input.x_len as isize || y >= input.y_len as isize {
break;
}
if !antinodes.contains(&(y as usize, x as usize)) {
count += 1;
antinodes.insert((y as usize, x as usize));
}
}

y = around_pos.0 as isize;
x = around_pos.1 as isize;
loop {
y = y.wrapping_add(delta_y);
x = x.wrapping_add(delta_x);
if x < 0 || y < 0 || x >= input.x_len as isize || y >= input.y_len as isize {
break;
}
if !antinodes.contains(&(y as usize, x as usize)) {
count += 1;
antinodes.insert((y as usize, x as usize));
}
}
}
}
}

count
}

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

const INPUT: &str = "............
........0...
.....0......
.......0....
....0.......
......A.....
............
............
........A...
.........A..
............
............
";
// const INPUT: &str = "T.........
// ...T......
// .T........
// ..........
// ..........
// ..........
// ..........
// ..........
// ..........
// ..........";

#[test]
fn test_part1() {
assert_eq!(14, part1(&input_generator(INPUT)))
}

#[test]
fn test_part2() {
assert_eq!(34, part2(&input_generator(INPUT)))
}
}
10 changes: 10 additions & 0 deletions src/day9.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// use aoc_runner_derive::{aoc, aoc_generator};

// #[aoc_generator(day8)]
// pub fn input_generator(input: &str) -> (Vec<usize>, Vec<usize>) {

// #[aoc(day8, part1)]
// pub fn part1(input: &[Equation]) -> usize {

// #[aoc(day8, part2)]
// pub fn part2(input: &[Equation]) -> usize {
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@ mod day4;
mod day5;
mod day6;
mod day7;
mod day8;

aoc_lib! { year = 2024 }

0 comments on commit 11b2a0f

Please sign in to comment.