Closed
Description
Code
Code
use std::collections::HashMap;
type Insertions = HashMap<(usize, usize), usize>;
type CountMap = HashMap<(usize, usize, usize), Vec<usize>>;
struct Polymerizer<'a> {
cur_depth: usize,
max_depth: usize,
counts: CountMap,
insertions: &'a Insertions,
}
impl<'a> Polymerizer<'a> {
fn new(max_depth: usize, insertions: &'a Insertions) -> Self {
let mut ret = Polymerizer {
cur_depth: 0,
max_depth,
counts: HashMap::new(),
insertions,
};
for keys in insertions.keys() {
let mut v = vec![0; 128];
v[keys.0] = 1;
v[keys.1] = 1;
ret.counts.insert((keys.0, keys.1, max_depth), v);
}
ret
}
fn polymerize_pairs(&mut self, p1: usize, p2: usize) {
if self.cur_depth == self.max_depth || self.counts.get(&(p1, p2, self.cur_depth)).is_some()
{
return;
}
let ins = *self.insertions.get(&(p1, p2)).unwrap();
self.cur_depth += 1;
self.polymerize_pairs(p1, ins);
self.polymerize_pairs(ins, p2);
self.cur_depth -= 1;
let v1 = self.counts.get(&(p1, ins, self.cur_depth + 1)).unwrap();
let v2 = self.counts.get(&(ins, p2, self.cur_depth + 1)).unwrap();
let mut v: Vec<usize> = v1.iter().zip(v2.iter()).map(|(c1, c2)| c1 + c2).collect();
v[ins] -= 1;
eprintln!("({:2}, {:2}, {:2}) - {:?}", self.cur_depth, p1 as u8 as char, p2 as u8 as char, v);
self.counts.insert((p1, p2, self.cur_depth), v);
}
fn polymerize(mut self, polymer: &str) -> Vec<usize> {
polymer
.as_bytes()
.windows(2)
.for_each(|w| self.polymerize_pairs(w[0].into(), w[1].into()));
polymer
.as_bytes()
.windows(2)
.fold(vec![0_usize; 128], |acc, key| {
let mut vec = self
.counts
.get(&(key[0].into(), key[1].into(), 0_usize))
.unwrap();
vec[key[1] as usize] -= 1;
acc.into_iter()
.zip(vec.iter())
.map(|(a, v)| a + v)
.collect()
})
}
}
pub fn run(input: &'static str) -> (usize, usize) {
let mut input = input.trim().lines();
let template = input.next().unwrap();
let insertions = input
.skip(1)
.filter_map(|line| line.split_once(" -> "))
.map(|(k, v)| {
let k = k.as_bytes();
(
(k[0].into(), k[1].into()),
v.chars().next().unwrap() as usize,
)
})
.collect::<HashMap<_, _>>();
let polymer = Polymerizer::new(1, &insertions).polymerize(template);
eprintln!("{:?}", polymer);
let (min, max) = polymer
.into_iter()
.filter(|c| *c != 0)
.fold((usize::MAX, 0), |(min, max), v| (min.min(v), max.max(v)));
let d14p1 = max - min;
let polymer = Polymerizer::new(4, &insertions).polymerize(template);
eprintln!("{:?}", polymer);
let (min, max) = polymer
.into_iter()
.filter(|c| *c != 0)
.fold((usize::MAX, 0), |(min, max), v| (min.min(v), max.max(v)));
let d14p2 = max - min;
(d14p1, d14p2)
}
#[test]
fn test() {
let input = "
NNCB
CH -> B
HH -> N
CB -> H
NH -> C
HB -> C
HC -> B
HN -> C
NN -> C
BH -> H
NC -> B
NB -> B
BN -> B
BB -> N
BC -> B
CC -> N
CN -> C
";
assert_eq!(run(input), (1588, 2188189693529));
}
Meta
rustc --version --verbose
:
rustc 1.58.0-nightly (936f2600b 2021-11-22)
binary: rustc
commit-hash: 936f2600b6c903b04387f74ed5cbce88bb06d243
commit-date: 2021-11-22
host: x86_64-unknown-linux-gnu
release: 1.58.0-nightly
LLVM version: 13.0.0
Error output
Please see Backtrace
Backtrace
Compiling aoc2021 v0.1.0 (/home/harshasrisri/Code/rust/aoc/2021)
thread 'rustc' panicked at 'index out of bounds: the len is 2 but the index is 2', compiler/rustc_borrowck/src/diagnostics/mutability_errors.rs:450:53
stack backtrace:
0: 0x7f106ebb921c - std::backtrace_rs::backtrace::libunwind::trace::h7630ba4cba718aa0
at /rustc/936f2600b6c903b04387f74ed5cbce88bb06d243/library/std/src/../../backtrace/src/backtrace/libunwind.rs:93:5
1: 0x7f106ebb921c - std::backtrace_rs::backtrace::trace_unsynchronized::he7498e79c157f5ac
at /rustc/936f2600b6c903b04387f74ed5cbce88bb06d243/library/std/src/../../backtrace/src/backtrace/mod.rs:66:5
2: 0x7f106ebb921c - std::sys_common::backtrace::_print_fmt::hdaebadaee17bca49
at /rustc/936f2600b6c903b04387f74ed5cbce88bb06d243/library/std/src/sys_common/backtrace.rs:67:5
3: 0x7f106ebb921c - <std::sys_common::backtrace::_print::DisplayBacktrace as core::fmt::Display>::fmt::h82b0e3aaf8a96140
at /rustc/936f2600b6c903b04387f74ed5cbce88bb06d243/library/std/src/sys_common/backtrace.rs:46:22
4: 0x7f106ec1614c - core::fmt::write::h72801a82c94e6ff1
at /rustc/936f2600b6c903b04387f74ed5cbce88bb06d243/library/core/src/fmt/mod.rs:1149:17
5: 0x7f106eba99c5 - std::io::Write::write_fmt::hc2da38dc44811df8
at /rustc/936f2600b6c903b04387f74ed5cbce88bb06d243/library/std/src/io/mod.rs:1697:15
6: 0x7f106ebbc3e0 - std::sys_common::backtrace::_print::h1c9a1d19c48821c1
at /rustc/936f2600b6c903b04387f74ed5cbce88bb06d243/library/std/src/sys_common/backtrace.rs:49:5
7: 0x7f106ebbc3e0 - std::sys_common::backtrace::print::h7ce8802039fa9d0e
at /rustc/936f2600b6c903b04387f74ed5cbce88bb06d243/library/std/src/sys_common/backtrace.rs:36:9
8: 0x7f106ebbc3e0 - std::panicking::default_hook::{{closure}}::hb2a74a8c1499c326
at /rustc/936f2600b6c903b04387f74ed5cbce88bb06d243/library/std/src/panicking.rs:211:50
9: 0x7f106ebbbf8b - std::panicking::default_hook::hf4f180b00076f2b2
at /rustc/936f2600b6c903b04387f74ed5cbce88bb06d243/library/std/src/panicking.rs:228:9
10: 0x7f106f353871 - rustc_driver[e620c7401644acc4]::DEFAULT_HOOK::{closure#0}::{closure#0}
11: 0x7f106ebbcbf9 - std::panicking::rust_panic_with_hook::he85ce8435493b711
at /rustc/936f2600b6c903b04387f74ed5cbce88bb06d243/library/std/src/panicking.rs:610:17
12: 0x7f106ebbc6b0 - std::panicking::begin_panic_handler::{{closure}}::h31e15f69e6235bd2
at /rustc/936f2600b6c903b04387f74ed5cbce88bb06d243/library/std/src/panicking.rs:502:13
13: 0x7f106ebb96d4 - std::sys_common::backtrace::__rust_end_short_backtrace::hfce2fadb61aaa3ae
at /rustc/936f2600b6c903b04387f74ed5cbce88bb06d243/library/std/src/sys_common/backtrace.rs:139:18
14: 0x7f106ebbc619 - rust_begin_unwind
at /rustc/936f2600b6c903b04387f74ed5cbce88bb06d243/library/std/src/panicking.rs:498:5
15: 0x7f106eb81621 - core::panicking::panic_fmt::h7b8580d81fcbbacd
at /rustc/936f2600b6c903b04387f74ed5cbce88bb06d243/library/core/src/panicking.rs:107:14
16: 0x7f106eb815e2 - core::panicking::panic_bounds_check::h63650a5dfc9aa86f
at /rustc/936f2600b6c903b04387f74ed5cbce88bb06d243/library/core/src/panicking.rs:75:5
17: 0x7f106f940a1d - <core[cc79c391059f8e46]::option::Option<&rustc_middle[93690e4789d7fe1d]::mir::Location>>::map::<rustc_span[2d5555579096f1fe]::span_encoding::Span, <rustc_borrowck[a407a7ce97bd973b]::MirBorrowckCtxt>::report_mutability_error::{closure#2}>
18: 0x7f106f956c6f - <rustc_borrowck[a407a7ce97bd973b]::MirBorrowckCtxt>::report_mutability_error
19: 0x7f1070a84215 - <rustc_borrowck[a407a7ce97bd973b]::MirBorrowckCtxt>::access_place
20: 0x7f106f95cbb9 - <rustc_borrowck[a407a7ce97bd973b]::MirBorrowckCtxt as rustc_mir_dataflow[aac604db03682c84]::framework::visitor::ResultsVisitor>::visit_statement_before_primary_effect
21: 0x7f1070a264ff - <rustc_mir_dataflow[aac604db03682c84]::framework::direction::Forward as rustc_mir_dataflow[aac604db03682c84]::framework::direction::Direction>::visit_results_in_block::<rustc_borrowck[a407a7ce97bd973b]::dataflow::BorrowckAnalyses<rustc_index[1c51b78c9996c7d7]::bit_set::BitSet<rustc_borrowck[a407a7ce97bd973b]::dataflow::BorrowIndex>, rustc_index[1c51b78c9996c7d7]::bit_set::BitSet<rustc_mir_dataflow[aac604db03682c84]::move_paths::MovePathIndex>, rustc_index[1c51b78c9996c7d7]::bit_set::BitSet<rustc_mir_dataflow[aac604db03682c84]::move_paths::InitIndex>>, rustc_borrowck[a407a7ce97bd973b]::dataflow::BorrowckAnalyses<rustc_mir_dataflow[aac604db03682c84]::framework::engine::Results<rustc_borrowck[a407a7ce97bd973b]::dataflow::Borrows>, rustc_mir_dataflow[aac604db03682c84]::framework::engine::Results<rustc_mir_dataflow[aac604db03682c84]::impls::MaybeUninitializedPlaces>, rustc_mir_dataflow[aac604db03682c84]::framework::engine::Results<rustc_mir_dataflow[aac604db03682c84]::impls::EverInitializedPlaces>>, rustc_borrowck[a407a7ce97bd973b]::MirBorrowckCtxt>
22: 0x7f1070a8b950 - rustc_borrowck[a407a7ce97bd973b]::do_mir_borrowck
23: 0x7f1070a1df16 - <rustc_infer[7163d95876195c41]::infer::InferCtxtBuilder>::enter::<rustc_middle[93690e4789d7fe1d]::mir::query::BorrowCheckResult, rustc_borrowck[a407a7ce97bd973b]::mir_borrowck::{closure#0}>
24: 0x7f1070a7fc06 - rustc_borrowck[a407a7ce97bd973b]::mir_borrowck
25: 0x7f1070a7c977 - <rustc_borrowck[a407a7ce97bd973b]::provide::{closure#0} as core[cc79c391059f8e46]::ops::function::FnOnce<(rustc_middle[93690e4789d7fe1d]::ty::context::TyCtxt, rustc_span[2d5555579096f1fe]::def_id::LocalDefId)>>::call_once
26: 0x7f10717709c3 - <rustc_query_system[efb192c4209e4e11]::dep_graph::graph::DepGraph<rustc_middle[93690e4789d7fe1d]::dep_graph::dep_node::DepKind>>::with_task::<rustc_middle[93690e4789d7fe1d]::ty::context::TyCtxt, rustc_span[2d5555579096f1fe]::def_id::LocalDefId, &rustc_middle[93690e4789d7fe1d]::mir::query::BorrowCheckResult>
27: 0x7f107172a70e - rustc_data_structures[8eb9d08fbf3883e5]::stack::ensure_sufficient_stack::<(&rustc_middle[93690e4789d7fe1d]::mir::query::BorrowCheckResult, rustc_query_system[efb192c4209e4e11]::dep_graph::graph::DepNodeIndex), rustc_query_system[efb192c4209e4e11]::query::plumbing::execute_job<rustc_query_impl[e405c240d479aca8]::plumbing::QueryCtxt, rustc_span[2d5555579096f1fe]::def_id::LocalDefId, &rustc_middle[93690e4789d7fe1d]::mir::query::BorrowCheckResult>::{closure#3}>
28: 0x7f1070c002b6 - rustc_query_system[efb192c4209e4e11]::query::plumbing::try_execute_query::<rustc_query_impl[e405c240d479aca8]::plumbing::QueryCtxt, rustc_query_system[efb192c4209e4e11]::query::caches::DefaultCache<rustc_span[2d5555579096f1fe]::def_id::LocalDefId, &rustc_middle[93690e4789d7fe1d]::mir::query::BorrowCheckResult>>
29: 0x7f1070c90e7e - <rustc_query_impl[e405c240d479aca8]::Queries as rustc_middle[93690e4789d7fe1d]::ty::query::QueryEngine>::mir_borrowck
30: 0x7f1070585d1d - <rustc_session[cec017cef00f19c9]::session::Session>::time::<(), rustc_interface[f89f8228a4e35bc7]::passes::analysis::{closure#2}>
31: 0x7f10711f4935 - rustc_interface[f89f8228a4e35bc7]::passes::analysis
32: 0x7f107178d1e0 - <rustc_query_system[efb192c4209e4e11]::dep_graph::graph::DepGraph<rustc_middle[93690e4789d7fe1d]::dep_graph::dep_node::DepKind>>::with_task::<rustc_middle[93690e4789d7fe1d]::ty::context::TyCtxt, (), core[cc79c391059f8e46]::result::Result<(), rustc_errors[c8a333c965fedc03]::ErrorReported>>
33: 0x7f1071725a63 - rustc_data_structures[8eb9d08fbf3883e5]::stack::ensure_sufficient_stack::<(core[cc79c391059f8e46]::result::Result<(), rustc_errors[c8a333c965fedc03]::ErrorReported>, rustc_query_system[efb192c4209e4e11]::dep_graph::graph::DepNodeIndex), rustc_query_system[efb192c4209e4e11]::query::plumbing::execute_job<rustc_query_impl[e405c240d479aca8]::plumbing::QueryCtxt, (), core[cc79c391059f8e46]::result::Result<(), rustc_errors[c8a333c965fedc03]::ErrorReported>>::{closure#3}>
34: 0x7f107168eb16 - rustc_query_system[efb192c4209e4e11]::query::plumbing::try_execute_query::<rustc_query_impl[e405c240d479aca8]::plumbing::QueryCtxt, rustc_query_system[efb192c4209e4e11]::query::caches::DefaultCache<(), core[cc79c391059f8e46]::result::Result<(), rustc_errors[c8a333c965fedc03]::ErrorReported>>>
35: 0x7f10716f7275 - rustc_query_system[efb192c4209e4e11]::query::plumbing::get_query::<rustc_query_impl[e405c240d479aca8]::queries::analysis, rustc_query_impl[e405c240d479aca8]::plumbing::QueryCtxt>
36: 0x7f10711ec149 - <rustc_interface[f89f8228a4e35bc7]::passes::QueryContext>::enter::<rustc_driver[e620c7401644acc4]::run_compiler::{closure#1}::{closure#2}::{closure#3}, core[cc79c391059f8e46]::result::Result<(), rustc_errors[c8a333c965fedc03]::ErrorReported>>
37: 0x7f10711d98b4 - <rustc_interface[f89f8228a4e35bc7]::interface::Compiler>::enter::<rustc_driver[e620c7401644acc4]::run_compiler::{closure#1}::{closure#2}, core[cc79c391059f8e46]::result::Result<core[cc79c391059f8e46]::option::Option<rustc_interface[f89f8228a4e35bc7]::queries::Linker>, rustc_errors[c8a333c965fedc03]::ErrorReported>>
38: 0x7f10711c9ded - rustc_span[2d5555579096f1fe]::with_source_map::<core[cc79c391059f8e46]::result::Result<(), rustc_errors[c8a333c965fedc03]::ErrorReported>, rustc_interface[f89f8228a4e35bc7]::interface::create_compiler_and_run<core[cc79c391059f8e46]::result::Result<(), rustc_errors[c8a333c965fedc03]::ErrorReported>, rustc_driver[e620c7401644acc4]::run_compiler::{closure#1}>::{closure#1}>
39: 0x7f10711eb18a - rustc_interface[f89f8228a4e35bc7]::interface::create_compiler_and_run::<core[cc79c391059f8e46]::result::Result<(), rustc_errors[c8a333c965fedc03]::ErrorReported>, rustc_driver[e620c7401644acc4]::run_compiler::{closure#1}>
40: 0x7f10711cd3ab - <scoped_tls[3fea4c3dcac147b1]::ScopedKey<rustc_span[2d5555579096f1fe]::SessionGlobals>>::set::<rustc_interface[f89f8228a4e35bc7]::util::setup_callbacks_and_run_in_thread_pool_with_globals<rustc_interface[f89f8228a4e35bc7]::interface::run_compiler<core[cc79c391059f8e46]::result::Result<(), rustc_errors[c8a333c965fedc03]::ErrorReported>, rustc_driver[e620c7401644acc4]::run_compiler::{closure#1}>::{closure#0}, core[cc79c391059f8e46]::result::Result<(), rustc_errors[c8a333c965fedc03]::ErrorReported>>::{closure#0}::{closure#0}, core[cc79c391059f8e46]::result::Result<(), rustc_errors[c8a333c965fedc03]::ErrorReported>>
41: 0x7f10711cc485 - std[a3bc703e0d1c3409]::sys_common::backtrace::__rust_begin_short_backtrace::<rustc_interface[f89f8228a4e35bc7]::util::setup_callbacks_and_run_in_thread_pool_with_globals<rustc_interface[f89f8228a4e35bc7]::interface::run_compiler<core[cc79c391059f8e46]::result::Result<(), rustc_errors[c8a333c965fedc03]::ErrorReported>, rustc_driver[e620c7401644acc4]::run_compiler::{closure#1}>::{closure#0}, core[cc79c391059f8e46]::result::Result<(), rustc_errors[c8a333c965fedc03]::ErrorReported>>::{closure#0}, core[cc79c391059f8e46]::result::Result<(), rustc_errors[c8a333c965fedc03]::ErrorReported>>
42: 0x7f10711ec72a - <<std[a3bc703e0d1c3409]::thread::Builder>::spawn_unchecked<rustc_interface[f89f8228a4e35bc7]::util::setup_callbacks_and_run_in_thread_pool_with_globals<rustc_interface[f89f8228a4e35bc7]::interface::run_compiler<core[cc79c391059f8e46]::result::Result<(), rustc_errors[c8a333c965fedc03]::ErrorReported>, rustc_driver[e620c7401644acc4]::run_compiler::{closure#1}>::{closure#0}, core[cc79c391059f8e46]::result::Result<(), rustc_errors[c8a333c965fedc03]::ErrorReported>>::{closure#0}, core[cc79c391059f8e46]::result::Result<(), rustc_errors[c8a333c965fedc03]::ErrorReported>>::{closure#1} as core[cc79c391059f8e46]::ops::function::FnOnce<()>>::call_once::{shim:vtable#0}
43: 0x7f106ebc7ea3 - <alloc::boxed::Box<F,A> as core::ops::function::FnOnce<Args>>::call_once::he162a5c338a10a39
at /rustc/936f2600b6c903b04387f74ed5cbce88bb06d243/library/alloc/src/boxed.rs:1694:9
44: 0x7f106ebc7ea3 - <alloc::boxed::Box<F,A> as core::ops::function::FnOnce<Args>>::call_once::hb27497b21740dd97
at /rustc/936f2600b6c903b04387f74ed5cbce88bb06d243/library/alloc/src/boxed.rs:1694:9
45: 0x7f106ebc7ea3 - std::sys::unix::thread::Thread::new::thread_start::he467e990e49c5136
at /rustc/936f2600b6c903b04387f74ed5cbce88bb06d243/library/std/src/sys/unix/thread.rs:106:17
46: 0x7f106eae3259 - start_thread
47: 0x7f106e9f85e3 - __GI___clone
48: 0x0 - <unknown>
error: internal compiler error: unexpected panic
note: the compiler unexpectedly panicked. this is a bug.
note: we would appreciate a bug report: https://github.com/rust-lang/rust/issues/new?labels=C-bug%2C+I-ICE%2C+T-compiler&template=ice.md
note: rustc 1.58.0-nightly (936f2600b 2021-11-22) running on x86_64-unknown-linux-gnu
note: compiler flags: -C embed-bitcode=no -C debuginfo=2 -C incremental
note: some of the compiler flags provided by cargo are hidden
query stack during panic:
#0 [mir_borrowck] borrow-checking `day14::<impl at src/day14.rs:13:1: 77:2>::polymerize::{closure#1}`
#1 [analysis] running analysis passes on this crate
end of query stack
error: could not compile `aoc2021`