Skip to content

Commit

Permalink
Day 13: Point of Incidence
Browse files Browse the repository at this point in the history
  • Loading branch information
ephemient committed Dec 13, 2023
1 parent 6db57f0 commit f89c83f
Show file tree
Hide file tree
Showing 5 changed files with 102 additions and 3 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@ Development occurs in language-specific directories:
|[Day10.hs](hs/src/Day10.hs)|[Day10.kt](kt/aoc2023-lib/src/commonMain/kotlin/com/github/ephemient/aoc2023/Day10.kt)|[day10.py](py/aoc2023/day10.py)|[day10.rs](rs/src/day10.rs)|
|[Day11.hs](hs/src/Day11.hs)|[Day11.kt](kt/aoc2023-lib/src/commonMain/kotlin/com/github/ephemient/aoc2023/Day11.kt)|[day11.py](py/aoc2023/day11.py)|[day11.rs](rs/src/day11.rs)|
|[Day12.hs](hs/src/Day12.hs)|[Day12.kt](kt/aoc2023-lib/src/commonMain/kotlin/com/github/ephemient/aoc2023/Day12.kt)|[day12.py](py/aoc2023/day12.py)|[day12.rs](rs/src/day12.rs)|
|[Day13.hs](hs/src/Day13.hs)|[Day13.kt](kt/aoc2023-lib/src/commonMain/kotlin/com/github/ephemient/aoc2023/Day13.kt)|[day13.py](py/aoc2023/day13.py)||
|[Day13.hs](hs/src/Day13.hs)|[Day13.kt](kt/aoc2023-lib/src/commonMain/kotlin/com/github/ephemient/aoc2023/Day13.kt)|[day13.py](py/aoc2023/day13.py)|[day13.rs](rs/src/day13.rs)|
8 changes: 7 additions & 1 deletion rs/benches/criterion.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use aoc2023::{day1, day10, day11, day12, day2, day3, day4, day5, day6, day7, day8, day9};
use aoc2023::{day1, day10, day11, day12, day13, day2, day3, day4, day5, day6, day7, day8, day9};
use criterion::{black_box, Criterion};
use std::env;
use std::fs;
Expand Down Expand Up @@ -86,6 +86,12 @@ fn aoc2023_bench(c: &mut Criterion) -> io::Result<()> {
g.bench_function("part 2", |b| b.iter(|| day12::part2(black_box(&data))));
g.finish();

let data = get_day_input(13)?;
let mut g = c.benchmark_group("day 13");
g.bench_function("part 1", |b| b.iter(|| day13::part1(black_box(&data))));
g.bench_function("part 2", |b| b.iter(|| day13::part2(black_box(&data))));
g.finish();

Ok(())
}

Expand Down
84 changes: 84 additions & 0 deletions rs/src/day13.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
fn find_reflection<T: Eq, F: Fn(&[T], &[T]) -> bool>(data: &[T], eq: F) -> usize {
(1..data.len())
.find(|&i| eq(&data[..i], &data[i..]))
.unwrap_or_default()
}

fn solve<F: Fn(&[&str], &[&str]) -> bool>(lines: &[&str], eq: F) -> usize {
let width = lines
.iter()
.map(|line| line.len())
.max()
.unwrap_or_default();
let transpose = (0..width)
.map(|i| {
lines
.iter()
.flat_map(|line| line[i..i + 1].chars())
.collect::<String>()
})
.collect::<Vec<_>>();
let transpose = transpose.iter().map(|line| &line[..]).collect::<Vec<_>>();

100 * find_reflection(lines, &eq) + find_reflection(&transpose, &eq)
}

pub fn part1(data: &str) -> usize {
data.split("\n\n")
.map(|group| {
solve(&group.lines().collect::<Vec<_>>(), |x, y| {
x.iter().rev().zip(y.iter()).all(|(a, b)| a == b)
})
})
.sum()
}

pub fn part2(data: &str) -> usize {
data.split("\n\n")
.map(|group| {
solve(&group.lines().collect::<Vec<_>>(), |x, y| {
let mut iter = x
.iter()
.rev()
.zip(y.iter())
.flat_map(|(a, b)| a.chars().zip(b.chars()).filter(|(c, d)| c != d));
iter.next().is_some() && iter.next().is_none()
})
})
.sum()
}

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

static EXAMPLE: &str = indoc! {"
#.##..##.
..#.##.#.
##......#
##......#
..#.##.#.
..##..##.
#.#.##.#.
#...##..#
#....#..#
..##..###
#####.##.
#####.##.
..##..###
#....#..#
"};

#[test]
fn part1_examples() {
assert_eq!(405, part1(EXAMPLE));
}

#[test]
fn part2_examples() {
assert_eq!(400, part2(EXAMPLE));
}
}
1 change: 1 addition & 0 deletions rs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ pub mod day1;
pub mod day10;
pub mod day11;
pub mod day12;
pub mod day13;
pub mod day2;
pub mod day3;
pub mod day4;
Expand Down
10 changes: 9 additions & 1 deletion rs/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use aoc2023::{day1, day10, day11, day12, day2, day3, day4, day5, day6, day7, day8, day9};
use aoc2023::{day1, day10, day11, day12, day13, day2, day3, day4, day5, day6, day7, day8, day9};
use std::collections::HashSet;
use std::env;
use std::fs;
Expand Down Expand Up @@ -112,5 +112,13 @@ fn main() -> io::Result<()> {
println!();
}

if args.is_empty() || args.contains("13") {
println!("Day 13");
let data = get_day_input(13)?;
println!("{:?}", day13::part1(&data));
println!("{:?}", day13::part2(&data));
println!();
}

Ok(())
}

0 comments on commit f89c83f

Please sign in to comment.