day3: part 2, add memoize
This commit is contained in:
76
Cargo.lock
generated
76
Cargo.lock
generated
@@ -2,6 +2,17 @@
|
|||||||
# It is not intended for manual editing.
|
# It is not intended for manual editing.
|
||||||
version = 4
|
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]]
|
[[package]]
|
||||||
name = "ahash"
|
name = "ahash"
|
||||||
version = "0.8.12"
|
version = "0.8.12"
|
||||||
@@ -72,6 +83,7 @@ dependencies = [
|
|||||||
"indicatif",
|
"indicatif",
|
||||||
"itertools",
|
"itertools",
|
||||||
"lazy_static",
|
"lazy_static",
|
||||||
|
"memoize",
|
||||||
"misc",
|
"misc",
|
||||||
"nom",
|
"nom",
|
||||||
"rayon",
|
"rayon",
|
||||||
@@ -113,10 +125,10 @@ version = "0.54.0"
|
|||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "9718806c4a2fe9e8a56fd736f97b340dd10ed1be8ed733ed50449f351dc33cae"
|
checksum = "9718806c4a2fe9e8a56fd736f97b340dd10ed1be8ed733ed50449f351dc33cae"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"ahash",
|
"ahash 0.8.12",
|
||||||
"cached_proc_macro",
|
"cached_proc_macro",
|
||||||
"cached_proc_macro_types",
|
"cached_proc_macro_types",
|
||||||
"hashbrown",
|
"hashbrown 0.14.5",
|
||||||
"once_cell",
|
"once_cell",
|
||||||
"thiserror",
|
"thiserror",
|
||||||
"web-time",
|
"web-time",
|
||||||
@@ -247,17 +259,37 @@ version = "1.0.7"
|
|||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1"
|
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]]
|
[[package]]
|
||||||
name = "grid"
|
name = "grid"
|
||||||
version = "0.1.0"
|
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]]
|
[[package]]
|
||||||
name = "hashbrown"
|
name = "hashbrown"
|
||||||
version = "0.14.5"
|
version = "0.14.5"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "e5274423e17b7c9fc20b6e7e208532f9b19825d82dfd615708b70edd83df41f1"
|
checksum = "e5274423e17b7c9fc20b6e7e208532f9b19825d82dfd615708b70edd83df41f1"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"ahash",
|
"ahash 0.8.12",
|
||||||
"allocator-api2",
|
"allocator-api2",
|
||||||
]
|
]
|
||||||
|
|
||||||
@@ -318,12 +350,44 @@ version = "0.2.177"
|
|||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "2874a2af47a2325c2001a6e6fad9b16a53b802102b528163885171cf92b15976"
|
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]]
|
[[package]]
|
||||||
name = "memchr"
|
name = "memchr"
|
||||||
version = "2.7.6"
|
version = "2.7.6"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "f52b00d39961fc5b2736ea853c9cc86238e165017a493d1d5c8eac6bdc4cc273"
|
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]]
|
[[package]]
|
||||||
name = "minimal-lexical"
|
name = "minimal-lexical"
|
||||||
version = "0.2.1"
|
version = "0.2.1"
|
||||||
@@ -576,6 +640,12 @@ version = "0.9.5"
|
|||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "0b928f33d975fc6ad9f86c8f283853ad26bdd5b10b7f1542aa2fa15e2289105a"
|
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]]
|
[[package]]
|
||||||
name = "wasm-bindgen"
|
name = "wasm-bindgen"
|
||||||
version = "0.2.106"
|
version = "0.2.106"
|
||||||
|
|||||||
@@ -14,6 +14,7 @@ grid = {version = "0.1.0", path = "utils/grid"}
|
|||||||
indicatif = { version = "0.17.9", features = ["rayon"] }
|
indicatif = { version = "0.17.9", features = ["rayon"] }
|
||||||
itertools = "0.13.0"
|
itertools = "0.13.0"
|
||||||
lazy_static = "1.5.0"
|
lazy_static = "1.5.0"
|
||||||
|
memoize = "0.5.1"
|
||||||
misc = {path = "utils/misc"}
|
misc = {path = "utils/misc"}
|
||||||
nom = "7.1.3"
|
nom = "7.1.3"
|
||||||
rayon = "1.10.0"
|
rayon = "1.10.0"
|
||||||
|
|||||||
52
src/day3.rs
52
src/day3.rs
@@ -1,36 +1,52 @@
|
|||||||
use aoc_runner_derive::{aoc, aoc_generator};
|
use std::collections::HashMap;
|
||||||
|
|
||||||
type Bank = Vec<u8>;
|
use aoc_runner_derive::{aoc, aoc_generator};
|
||||||
|
use lazy_static::lazy_static;
|
||||||
|
use memoize::memoize;
|
||||||
|
|
||||||
|
type Bank = [u8];
|
||||||
|
|
||||||
#[aoc_generator(day3)]
|
#[aoc_generator(day3)]
|
||||||
fn parse(input: &str) -> Vec<Bank> {
|
fn parse(input: &str) -> Vec<Vec<u8>> {
|
||||||
input
|
input
|
||||||
.lines()
|
.lines()
|
||||||
.map(|bank| bank.bytes().map(|c| c - b'0').collect())
|
.map(|bank| bank.bytes().map(|c| c - b'0').collect())
|
||||||
.collect()
|
.collect()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn max_joltage(bank: &Bank, n: usize) -> u64 {
|
#[memoize]
|
||||||
assert_eq!(n, 2, "unimplemented");
|
fn max_joltage(bank: Vec<u8>, n: usize) -> u64 {
|
||||||
let mut max = 0;
|
if n == 1 {
|
||||||
for start in 0..bank.len() - 1 {
|
return *bank.iter().max().unwrap() as u64;
|
||||||
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
|
|
||||||
|
(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)]
|
#[aoc(day3, part1)]
|
||||||
fn part1(input: &Vec<Bank>) -> u64 {
|
fn part1(input: &Vec<Vec<u8>>) -> u64 {
|
||||||
input.iter().map(|bank| max_joltage(bank, 2)).sum()
|
input.iter().map(|bank| max_joltage(bank.clone(), 2)).sum()
|
||||||
}
|
}
|
||||||
|
|
||||||
#[aoc(day3, part2)]
|
#[aoc(day3, part2)]
|
||||||
fn part2(input: &Vec<Bank>) -> u64 {
|
fn part2(input: &Vec<Vec<u8>>) -> u64 {
|
||||||
todo!()
|
input.iter().map(|bank| max_joltage(bank.clone(), 12)).sum()
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
@@ -49,6 +65,6 @@ mod tests {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn part2_example() {
|
fn part2_example() {
|
||||||
assert_eq!(part2(&parse(EXAMPLE)), 0);
|
assert_eq!(part2(&parse(EXAMPLE)), 3121910778619);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user