Skip to content

Commit 9ec5264

Browse files
committed
2024 day 1
1 parent 37ea318 commit 9ec5264

File tree

7 files changed

+94
-2
lines changed

7 files changed

+94
-2
lines changed

Cargo.lock

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/aoc/Cargo.toml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,13 @@ utils = { path = "../utils" }
1313
year2015 = { path = "../year2015", optional = true }
1414
year2016 = { path = "../year2016", optional = true }
1515
year2017 = { path = "../year2017", optional = true }
16+
year2024 = { path = "../year2024", optional = true }
1617

1718
[features]
1819
default = ["all-years", "unsafe"]
1920
# xtask update features
20-
all-years = ["year2015", "year2016", "year2017"]
21-
unsafe = ["year2015?/unsafe", "year2016?/unsafe", "year2017?/unsafe", "utils/unsafe"]
21+
all-years = ["year2015", "year2016", "year2017", "year2024"]
22+
unsafe = ["year2015?/unsafe", "year2016?/unsafe", "year2017?/unsafe", "year2024?/unsafe", "utils/unsafe"]
2223

2324
[lints]
2425
workspace = true

crates/aoc/src/years.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,18 @@ pub mod year2016 {
2323
pub mod year2017 {
2424
pub use ::utils::puzzles_noop as puzzles;
2525
}
26+
#[cfg(not(feature = "year2024"))]
27+
pub mod year2024 {
28+
pub use ::utils::puzzles_noop as puzzles;
29+
}
2630
#[cfg(feature = "year2015")]
2731
pub use ::year2015;
2832
#[cfg(feature = "year2016")]
2933
pub use ::year2016;
3034
#[cfg(feature = "year2017")]
3135
pub use ::year2017;
36+
#[cfg(feature = "year2024")]
37+
pub use ::year2024;
3238

3339
/// Macro which invokes a callback macro with a list of all implemented puzzle solutions.
3440
///
@@ -98,6 +104,7 @@ macro_rules! all_puzzles {
98104
$crate::year2015::puzzles,
99105
$crate::year2016::puzzles,
100106
$crate::year2017::puzzles,
107+
$crate::year2024::puzzles,
101108

102109
$callback
103110
]

crates/year2024/Cargo.toml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
[package]
2+
name = "year2024"
3+
authors = { workspace = true }
4+
edition = { workspace = true }
5+
license = { workspace = true }
6+
publish = { workspace = true }
7+
repository = { workspace = true }
8+
rust-version = { workspace = true }
9+
10+
[dependencies]
11+
utils = { path = "../utils" }
12+
13+
[features]
14+
unsafe = ["utils/unsafe"]

crates/year2024/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Solutions for [Advent of Code 2024](https://adventofcode.com/2024)

crates/year2024/src/day01.rs

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
use utils::prelude::*;
2+
3+
/// Comparing two lists of numbers.
4+
#[derive(Clone, Debug)]
5+
pub struct Day01 {
6+
left: Vec<u32>,
7+
right: Vec<u32>,
8+
}
9+
10+
impl Day01 {
11+
pub fn new(input: &str, _: InputType) -> Result<Self, InputError> {
12+
let pairs = parser::u32()
13+
.then(parser::u32().with_prefix(" "))
14+
.parse_lines(input)?;
15+
16+
let mut left = pairs.iter().map(|x| x.0).collect::<Vec<_>>();
17+
left.sort_unstable();
18+
19+
let mut right = pairs.into_iter().map(|x| x.1).collect::<Vec<_>>();
20+
right.sort_unstable();
21+
22+
Ok(Self { left, right })
23+
}
24+
25+
#[must_use]
26+
pub fn part1(&self) -> u32 {
27+
self.left
28+
.iter()
29+
.zip(&self.right)
30+
.map(|(&l, &r)| l.abs_diff(r))
31+
.sum()
32+
}
33+
34+
#[must_use]
35+
pub fn part2(&self) -> u32 {
36+
let mut score = 0;
37+
let mut i = 0;
38+
for &l in &self.left {
39+
while i < self.right.len() && self.right[i] < l {
40+
i += 1;
41+
}
42+
43+
let mut j = i;
44+
while j < self.right.len() && self.right[j] == l {
45+
score += l;
46+
j += 1;
47+
}
48+
}
49+
score
50+
}
51+
}
52+
53+
examples!(Day01 -> (u32, u32) [
54+
{input: "3 4\n4 3\n2 5\n1 3\n3 9\n3 3", part1: 11, part2: 31},
55+
]);

crates/year2024/src/lib.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#![doc = include_str!("../README.md")]
2+
#![cfg_attr(not(feature = "unsafe"), forbid(unsafe_code))]
3+
4+
utils::year!(2024 => year2024, ${
5+
1 => day01::Day01,
6+
});

0 commit comments

Comments
 (0)