Skip to content

Commit

Permalink
Day 1
Browse files Browse the repository at this point in the history
  • Loading branch information
johnhurt committed Dec 1, 2024
1 parent ee8b6e3 commit 07924aa
Show file tree
Hide file tree
Showing 6 changed files with 114 additions and 115 deletions.
124 changes: 27 additions & 97 deletions Cargo.lock

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

16 changes: 7 additions & 9 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,11 @@ test_lib = []

[dependencies]
pico-args = "0.5.0"
lazy_static = "1.4.0"
nom = "7.1.3"
itertools = "0.12.0"
strum = "0.25.0"
strum_macros = "0.25.0"
tinyvec = { version = "1.6.0", features = ["alloc"]}
hashlink = "0.8.4"
rstar = "0.11.0"
num-traits = "0.2.15"
ordered-float = "4.2.0"
itertools = "0.13.0"
tinyvec = { version = "1.8.0", features = ["alloc"]}
hashlink = "0.9.1"
rstar = "0.12.2"
num-traits = "0.2.19"
ordered-float = "4.5.0"
strum = { version = "0.26.3", features = ["derive"] }
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ Solutions for [Advent of Code](https://adventofcode.com/) in [Rust](https://www.
<!--- benchmarking table --->
## Benchmarks

| Day | Part 1 | Part 2 |
| :---: | :----: | :----: |

| Day | Part 1 | Part 2 |
| :---: | :---: | :---: |
| [Day 1](./src/bin/01.rs) | `36.5µs` | `48.1µs` |

**Total: 0ms**
**Total: 0.08ms**
<!--- benchmarking table --->

---
Expand Down
6 changes: 6 additions & 0 deletions data/examples/01.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
3 4
4 3
2 5
1 3
3 9
3 3
67 changes: 67 additions & 0 deletions src/bin/01.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
use std::collections::HashMap;

use advent_of_code::ws;
use nom::{character::complete::u32, sequence::pair, IResult};

advent_of_code::solution!(1);

fn parse_line(line: &str) -> (u32, u32) {
let result: IResult<_, _> = pair(ws(u32), ws(u32))(line);
result.expect("😎").1
}

pub fn part_one(input: &str) -> Option<u32> {
let (mut left, mut right): (Vec<_>, Vec<_>) =
input.lines().map(parse_line).unzip();

left.sort();
right.sort();

Some(
left.into_iter()
.zip(right)
.map(|(l, r)| l.abs_diff(r))
.sum(),
)
}

pub fn part_two(input: &str) -> Option<u32> {
let (left, right): (Vec<_>, Vec<_>) = input.lines().map(parse_line).unzip();

let mut right_counts = HashMap::<u32, u32>::new();

for v in right.into_iter() {
right_counts
.entry(v)
.and_modify(|existing| *existing += 1)
.or_insert(1);
}

let result = left
.into_iter()
.map(|left| {
(right_counts.get(&left).copied().unwrap_or_default(), left)
})
.map(|(left, right)| left * right)
.sum();
Some(result)
}

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

#[test]
fn test_part_one() {
let result =
part_one(&advent_of_code::template::read_file("examples", DAY));
assert_eq!(result, Some(11));
}

#[test]
fn test_part_two() {
let result =
part_two(&advent_of_code::template::read_file("examples", DAY));
assert_eq!(result, Some(31));
}
}
8 changes: 3 additions & 5 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@ use nom::{
sequence::{delimited, preceded},
Parser,
};
use strum::IntoEnumIterator;
use strum_macros::EnumIter;
use strum::{EnumIter, IntoEnumIterator};
use tinyvec::TinyVec;

pub type TV4<K> = TinyVec<[K; 4]>;
Expand Down Expand Up @@ -185,9 +184,8 @@ impl<T> Grid<T> {
}

pub fn escaping(&self, i: usize) -> impl Iterator<Item = Compass> + '_ {
Compass::iter().filter_map(move |dir| {
self.step_from_index(i, dir).is_none().then_some(dir)
})
Compass::iter()
.filter(move |dir| self.step_from_index(i, *dir).is_some())
}

pub fn min_dist(&self, from: usize, to: usize) -> usize {
Expand Down

0 comments on commit 07924aa

Please sign in to comment.