Skip to content

Commit

Permalink
Clean up some extraneous idents that could easily be closures
Browse files Browse the repository at this point in the history
  • Loading branch information
caass committed Sep 16, 2024
1 parent 037c2a8 commit 1a7aae3
Show file tree
Hide file tree
Showing 9 changed files with 121 additions and 53 deletions.
46 changes: 46 additions & 0 deletions Cargo.lock

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

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ fnv = "1.0.7"
clap = { version = "4.5.17", features = ["derive"] }
petgraph = { version = "0.6.5", features = ["rayon"] }
itertools = "0.13.0"
serde_json = "1.0.128"
void = "1.0.2"

[dev-dependencies]
pretty_assertions = "1.4.0"
39 changes: 17 additions & 22 deletions src/2015/09.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use eyre::{eyre, Result};
use eyre::{eyre, Report, Result};
use itertools::Itertools;
use petgraph::{graph::NodeIndex, Graph, Undirected};
use rayon::prelude::*;
Expand All @@ -10,32 +10,27 @@ use winnow::prelude::*;

use crate::types::{problem, Problem};

pub const ALL_IN_A_SINGLE_NIGHT: Problem = problem!(part1, part2);

fn part1(input: &str) -> Result<usize> {
let stops = input
.lines()
.map(Leg::try_from)
.collect::<Result<Locations, _>>()
.map_err(|e| eyre!("{e}"))?;

Ok(stops.shortest_distance())
}

fn part2(input: &str) -> Result<usize> {
let stops = input
.lines()
.map(Leg::try_from)
.collect::<Result<Locations, _>>()
.map_err(|e| eyre!("{e}"))?;

Ok(stops.longest_distance())
}
pub const ALL_IN_A_SINGLE_NIGHT: Problem = problem!(
|input| Locations::try_from(input).map(|locs| locs.shortest_distance()),
|input| Locations::try_from(input).map(|locs| locs.longest_distance())
);

/// A list of locations Santa has to visit and how far apart they are from each other.
#[derive(Debug)]
struct Locations<'s>(Graph<&'s str, usize, Undirected, u8>);

impl<'s> TryFrom<&'s str> for Locations<'s> {
type Error = Report;

fn try_from(input: &'s str) -> std::result::Result<Self, Self::Error> {
input
.lines()
.map(Leg::try_from)
.collect::<Result<Locations, _>>()
.map_err(|e| eyre!("{e}"))
}
}

impl<'s> Locations<'s> {
/// Construct a new list of stops.
fn new(graph: Graph<&'s str, usize, Undirected, u8>) -> Self {
Expand Down
20 changes: 5 additions & 15 deletions src/2015/10.rs
Original file line number Diff line number Diff line change
@@ -1,24 +1,14 @@
use eyre::Result;
use itertools::Itertools;
use void::Void;

use crate::types::{problem, Problem};

pub const ELVES_LOOK_ELVES_SAY: Problem = problem!(part1, part2);
pub const ELVES_LOOK_ELVES_SAY: Problem = problem!(look_and_say_n::<40>, look_and_say_n::<50>);

fn part1(input: &str) -> Result<usize> {
let mut n = input.to_string();
dbg!(&n);
for _ in 0..40 {
n = look_and_say(&n);
dbg!(&n);
}

Ok(n.len())
}

fn part2(input: &str) -> Result<usize> {
let mut n = input.to_string();
for _ in 0..50 {
fn look_and_say_n<const N: usize>(n: &str) -> Result<usize, Void> {
let mut n = n.to_string();
for _ in 0..N {
n = look_and_say(&n);
}

Expand Down
18 changes: 6 additions & 12 deletions src/2015/11.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,12 @@ use rayon::prelude::*;

use crate::types::{problem, Problem};

pub const CORPORATE_POLICY: Problem = problem!(part1, part2);
const ASCII_LETTER_OFFSET: u8 = b'a';

fn part1(input: &str) -> Result<Password> {
input.parse::<Password>().map(Password::next_valid)
}
pub const CORPORATE_POLICY: Problem = problem!(
|input: &str| input.parse::<Password>().map(Password::next),
|input: &str| input.parse::<Password>().map(|pwd| pwd.next().next())
);

fn part2(input: &str) -> Result<Password> {
input
.parse::<Password>()
.map(|pwd| pwd.next_valid().next_valid())
}
const ASCII_LETTER_OFFSET: u8 = b'a';

#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Clone, Copy)]
struct Password([Letter; 8]);
Expand Down Expand Up @@ -249,7 +243,7 @@ impl Password {
}

/// Returns the next password that passes validation, if one exists.
fn next_valid(mut self) -> Password {
fn next(mut self) -> Password {
self.increment();
self.into_par_iter()
.find_first(Password::is_valid)
Expand Down
28 changes: 28 additions & 0 deletions src/2015/12.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
use rayon::prelude::*;
use serde_json::Value;

use crate::types::{problem, Problem};

pub const JS_ABACUS_FRAMEWORK_DOT_IO: Problem = problem!(
|input| serde_json::from_str(input).map(|root| sum_numbers(root, false)),
|input| serde_json::from_str(input).map(|root| sum_numbers(root, true))
);

fn sum_numbers(value: Value, ignore_red: bool) -> i64 {
match value {
Value::Null => 0,
Value::Bool(_) => 0,
Value::Number(number) => number.as_i64().expect("number to be an integer"),
Value::String(_) => 0,
Value::Array(vec) => vec
.into_par_iter()
.map(|val| sum_numbers(val, ignore_red))
.sum(),
Value::Object(map) if ignore_red && map.values().any(|val| val == "red") => 0,
Value::Object(map) => map
.into_iter()
.par_bridge()
.map(|(_, value)| sum_numbers(value, ignore_red))
.sum(),
}
}
5 changes: 3 additions & 2 deletions src/2015/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use phf::phf_map;
use crate::types::ProblemSet;
use crate::util::mod_days;

mod_days!(01, 02, 03, 04, 05, 06, 07, 08, 09, 10, 11);
mod_days!(01, 02, 03, 04, 05, 06, 07, 08, 09, 10, 11, 12);

pub const PROBLEMS: ProblemSet = ProblemSet(phf_map! {
1u8 => NOT_QUITE_LISP,
Expand All @@ -16,5 +16,6 @@ pub const PROBLEMS: ProblemSet = ProblemSet(phf_map! {
8u8 => MATCHSTICKS,
9u8 => ALL_IN_A_SINGLE_NIGHT,
10u8 => ELVES_LOOK_ELVES_SAY,
11u8 => CORPORATE_POLICY
11u8 => CORPORATE_POLICY,
12u8 => JS_ABACUS_FRAMEWORK_DOT_IO,
});
4 changes: 2 additions & 2 deletions src/types/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ macro_rules! problem {
Problem::new(None, None)
};

($part1:ident) => {
($part1:expr) => {
Problem::new(
Some(|input| {
let output = $part1(input)?;
Expand All @@ -65,7 +65,7 @@ macro_rules! problem {
)
};

($part1:ident, $part2:ident) => {
($part1:expr, $part2:expr) => {
Problem::new(
Some(|input| {
let output = $part1(input)?;
Expand Down
12 changes: 12 additions & 0 deletions tests/2015.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,3 +131,15 @@ mod day11 {
crate::util::aoc!(2015/11-2: "vzcaabcc");
}
}

mod day12 {
#[test]
fn part1() {
crate::util::aoc!(2015/12-1: 111754);
}

#[test]
fn part2() {
crate::util::aoc!(2015/12-2: 65402);
}
}

0 comments on commit 1a7aae3

Please sign in to comment.