Compare commits

..

2 Commits

Author SHA1 Message Date
d3086d1c20 clippies 2025-12-02 21:35:10 -08:00
47f7068705 day3: part 2, add memoize 2025-12-02 21:30:31 -08:00
9 changed files with 113 additions and 28 deletions

Binary file not shown.

Before

Width:  |  Height:  |  Size: 6.6 KiB

After

Width:  |  Height:  |  Size: 4.8 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 7.2 KiB

After

Width:  |  Height:  |  Size: 5.3 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.7 KiB

76
Cargo.lock generated
View File

@@ -2,6 +2,17 @@
# It is not intended for manual editing.
version = 4
[[package]]
name = "ahash"
version = "0.7.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "891477e0c6a8957309ee5c45a6368af3ae14bb510732d2684ffa19af310920f9"
dependencies = [
"getrandom",
"once_cell",
"version_check",
]
[[package]]
name = "ahash"
version = "0.8.12"
@@ -72,6 +83,7 @@ dependencies = [
"indicatif",
"itertools",
"lazy_static",
"memoize",
"misc",
"nom",
"rayon",
@@ -113,10 +125,10 @@ version = "0.54.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9718806c4a2fe9e8a56fd736f97b340dd10ed1be8ed733ed50449f351dc33cae"
dependencies = [
"ahash",
"ahash 0.8.12",
"cached_proc_macro",
"cached_proc_macro_types",
"hashbrown",
"hashbrown 0.14.5",
"once_cell",
"thiserror",
"web-time",
@@ -247,17 +259,37 @@ version = "1.0.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1"
[[package]]
name = "getrandom"
version = "0.2.16"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "335ff9f135e4384c8150d6f27c6daed433577f86b4750418338c01a1a2528592"
dependencies = [
"cfg-if",
"libc",
"wasi",
]
[[package]]
name = "grid"
version = "0.1.0"
[[package]]
name = "hashbrown"
version = "0.12.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8a9ee70c43aaf417c914396645a0fa852624801b24ebb7ae78fe8272889ac888"
dependencies = [
"ahash 0.7.8",
]
[[package]]
name = "hashbrown"
version = "0.14.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e5274423e17b7c9fc20b6e7e208532f9b19825d82dfd615708b70edd83df41f1"
dependencies = [
"ahash",
"ahash 0.8.12",
"allocator-api2",
]
@@ -318,12 +350,44 @@ version = "0.2.177"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2874a2af47a2325c2001a6e6fad9b16a53b802102b528163885171cf92b15976"
[[package]]
name = "lru"
version = "0.7.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e999beba7b6e8345721bd280141ed958096a2e4abdf74f67ff4ce49b4b54e47a"
dependencies = [
"hashbrown 0.12.3",
]
[[package]]
name = "memchr"
version = "2.7.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f52b00d39961fc5b2736ea853c9cc86238e165017a493d1d5c8eac6bdc4cc273"
[[package]]
name = "memoize"
version = "0.5.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f8d1d5792299bab3f8b5d88d1b7a7cb50ad7ef039a8c4d45a6b84880a6526276"
dependencies = [
"lazy_static",
"lru",
"memoize-inner",
]
[[package]]
name = "memoize-inner"
version = "0.5.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6dd8f89255d8ff313afabed9a3c83ef0993cc056679dfd001f5111a026f876f7"
dependencies = [
"lazy_static",
"proc-macro2",
"quote",
"syn 1.0.109",
]
[[package]]
name = "minimal-lexical"
version = "0.2.1"
@@ -576,6 +640,12 @@ version = "0.9.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0b928f33d975fc6ad9f86c8f283853ad26bdd5b10b7f1542aa2fa15e2289105a"
[[package]]
name = "wasi"
version = "0.11.1+wasi-snapshot-preview1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ccf3ec651a847eb01de73ccad15eb7d99f80485de043efb2f370cd654f4ea44b"
[[package]]
name = "wasm-bindgen"
version = "0.2.106"

View File

@@ -14,6 +14,7 @@ grid = {version = "0.1.0", path = "utils/grid"}
indicatif = { version = "0.17.9", features = ["rayon"] }
itertools = "0.13.0"
lazy_static = "1.5.0"
memoize = "0.5.1"
misc = {path = "utils/misc"}
nom = "7.1.3"
rayon = "1.10.0"

View File

@@ -1,6 +1,6 @@
<!-- AOC TILES BEGIN -->
<h1 align="center">
2025 - 4 ⭐ - Rust
2025 - 6 ⭐ - Rust
</h1>
<a href="src/day1.rs">
<img src=".aoc_tiles/tiles/2025/01.png" width="161px">
@@ -8,4 +8,7 @@
<a href="src/day2.rs">
<img src=".aoc_tiles/tiles/2025/02.png" width="161px">
</a>
<a href="src/day3.rs">
<img src=".aoc_tiles/tiles/2025/03.png" width="161px">
</a>
<!-- AOC TILES END -->

View File

@@ -31,7 +31,7 @@ fn part1(input: &[RangeInclusive<u64>]) -> u64 {
for r in input {
for product in r.clone() {
let prod_s = product.to_string();
if prod_s.len() % 2 != 0 {
if !prod_s.len().is_multiple_of(2) {
continue;
}
let (left, right) = prod_s.split_at(prod_s.len() / 2);
@@ -50,7 +50,7 @@ fn part2(input: &[RangeInclusive<u64>]) -> u64 {
for product in r.clone() {
let prod_s = product.to_string();
for n in 1..=prod_s.len() / 2 {
if prod_s.len() % n != 0 {
if !prod_s.len().is_multiple_of(n) {
continue;
}
if prod_s
@@ -76,7 +76,7 @@ fn part1_arithmetic_brute(input: &[RangeInclusive<u64>]) -> u64 {
// println!("Range: {:?}", r);
for product in r.clone() {
let n_digits = (product.ilog10() + 1) as usize;
if n_digits % 2 != 0 {
if !n_digits.is_multiple_of(2) {
continue;
}
@@ -101,7 +101,7 @@ fn part2_arithmetic_brute(input: &[RangeInclusive<u64>]) -> u64 {
for n in 1..=n_digits / 2 {
let repetitions = n_digits / n;
if n_digits % n != 0 {
if !n_digits.is_multiple_of(n) {
continue;
}
let decade = POW10[n_digits - n];
@@ -127,7 +127,7 @@ fn part1_clever(input: &[RangeInclusive<u64>]) -> u64 {
for r in input {
let n_digits = (r.start().ilog10() + 1) as usize;
let mut lhs = if n_digits % 2 != 0 {
let mut lhs = if !n_digits.is_multiple_of(2) {
// If the number of digits is odd, we start our guess at the first possibility in the next decade
POW10[n_digits / 2]
} else {

View File

@@ -1,36 +1,47 @@
use aoc_runner_derive::{aoc, aoc_generator};
type Bank = Vec<u8>;
use memoize::memoize;
#[aoc_generator(day3)]
fn parse(input: &str) -> Vec<Bank> {
fn parse(input: &str) -> Vec<Vec<u8>> {
input
.lines()
.map(|bank| bank.bytes().map(|c| c - b'0').collect())
.collect()
}
fn max_joltage(bank: &Bank, n: usize) -> u64 {
assert_eq!(n, 2, "unimplemented");
let mut max = 0;
for start in 0..bank.len() - 1 {
let cur = bank[start] as u64 * 10u64.pow(n as u32 - 1)
+ *bank[start + 1..].iter().max().unwrap() as u64;
if cur > max {
max = cur
#[memoize]
fn max_joltage(bank: Vec<u8>, n: usize) -> u64 {
if n == 1 {
return *bank.iter().max().unwrap() as u64;
}
}
max
(0..bank.len() - n + 1)
.map(|start| {
bank[start] as u64 * 10u64.pow(n as u32 - 1)
+ max_joltage(bank[start + 1..].to_vec(), n - 1)
})
.max()
.unwrap()
// let mut max = 0;
// for start in 0..bank.len() - n + 1 {
// let cur = bank[start] as u64 * 10u64.pow(n as u32 - 1)
// + *bank[start + 1..].iter().max().unwrap() as u64;
// if cur > max {
// max = cur
// }
// }
// max
}
#[aoc(day3, part1)]
fn part1(input: &Vec<Bank>) -> u64 {
input.iter().map(|bank| max_joltage(bank, 2)).sum()
fn part1(input: &[Vec<u8>]) -> u64 {
input.iter().map(|bank| max_joltage(bank.clone(), 2)).sum()
}
#[aoc(day3, part2)]
fn part2(input: &Vec<Bank>) -> u64 {
todo!()
fn part2(input: &[Vec<u8>]) -> u64 {
input.iter().map(|bank| max_joltage(bank.clone(), 12)).sum()
}
#[cfg(test)]
@@ -49,6 +60,6 @@ mod tests {
#[test]
fn part2_example() {
assert_eq!(part2(&parse(EXAMPLE)), 0);
assert_eq!(part2(&parse(EXAMPLE)), 3121910778619);
}
}

View File

@@ -1,6 +1,6 @@
mod day3;
mod day1;
mod day2;
mod day3;
use aoc_runner_derive::aoc_lib;