2021 day 1

[?]
Dec 3, 2021, 3:08 AM
HUHAFQLN6Q6U7CPR2SPPEPLE5LMCR53UA2GJ7LUVSHFXQPCGIYAQC

Dependencies

Change contents

  • file addition: 2021 (d--r------)
    [1.0]
  • file addition: day1.rs (----------)
    [0.16]
    enum PairsState<N> {
    Fresh,
    Running(N),
    Finished,
    }
    struct Pairs<I, N> {
    inner: I,
    state: PairsState<N>,
    }
    impl<I: Iterator<Item = N>, N> Pairs<I, N> {
    fn new(inner: I) -> Self {
    Self {
    inner: inner,
    state: PairsState::Fresh,
    }
    }
    }
    impl<I: Iterator<Item = N>, N: Copy> Iterator for Pairs<I, N> {
    type Item = (N, N);
    fn next(&mut self) -> Option<Self::Item> {
    match self.state {
    PairsState::Fresh => match self.inner.next() {
    None => {
    self.state = PairsState::Finished;
    None
    }
    Some(x) => {
    self.state = PairsState::Running(x);
    self.next()
    }
    },
    PairsState::Running(last) => match self.inner.next() {
    None => {
    self.state = PairsState::Finished;
    None
    }
    Some(x) => {
    self.state = PairsState::Running(x);
    Some((last, x))
    }
    },
    PairsState::Finished => None,
    }
    }
    }
    struct SlidingWindow<I, N> {
    inner: I,
    size: usize,
    window: std::collections::VecDeque<N>,
    }
    impl<I, N> SlidingWindow<I, N> {
    fn new(inner: I, size: usize) -> Self {
    Self {
    inner: inner,
    size: size,
    window: std::collections::VecDeque::new(),
    }
    }
    }
    impl<I: Iterator<Item = N>, N: Copy> Iterator for SlidingWindow<I, N> {
    type Item = Vec<N>;
    fn next(&mut self) -> Option<Self::Item> {
    match self.inner.next() {
    None => None,
    Some(x) => {
    self.window.push_back(x);
    if self.window.len() < self.size {
    self.next()
    } else {
    let result = self.window.iter().map(|x| *x).collect();
    self.window.pop_front();
    Some(result)
    }
    }
    }
    }
    }
    fn count_increments<I: Iterator<Item = N>, N: Copy + std::cmp::PartialOrd>(source: I) -> usize {
    Pairs::new(source)
    .map(|(last, current)| if current > last { 1 } else { 0 })
    .sum()
    }
    fn main() {
    use std::fs::File;
    use std::io::BufRead;
    use std::io::BufReader;
    use std::path::Path;
    let args: Vec<_> = std::env::args().collect();
    let file = BufReader::new(File::open(<String as AsRef<Path>>::as_ref(&args[1])).unwrap());
    let measurements: Vec<u64> = file
    .lines()
    .flat_map(|line| line.ok().and_then(|line| str::parse(line.as_ref()).ok()))
    .collect();
    println!(
    "Depth measurement increases {} times (measurements); {} times (moving window)",
    count_increments(measurements.iter()),
    count_increments(
    SlidingWindow::new(measurements.iter(), 3).map(|w| w.into_iter().sum::<u64>())
    )
    );
    }
  • file addition: Cargo.toml (----------)
    [0.16]
    [package]
    name = "aoc-2020"
    version = "0.1.0"
    edition = "2018"
    # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
    [dependencies]
    [[bin]]
    name = "day1"
    path = "day1.rs"
  • file addition: Cargo.lock (----------)
    [0.16]
    # This file is automatically @generated by Cargo.
    # It is not intended for manual editing.
    version = 3
    [[package]]
    name = "aoc-2020"
    version = "0.1.0"
  • file addition: day19.rs (----------)
    [2.17]
    #![feature(async_stream)]
    use bytes::Bytes;
    use std::collections::HashSet;
    use std::future::Future;
    use std::marker::Unpin;
    use std::stream::Stream;
    use tokio::io::{self, AsyncBufRead, AsyncBufReadExt, BufReader};
    enum Rule {
    Match(String),
    Branch(Vec<Vec<usize>>),
    }
    fn parse_rule(s: String) -> Option<Rule> {
    let s = s.trim();
    s.split_once(':').and_then(|(_ix, body)| {
    let body = body.trim();
    parse_match_rule(body).or_else(|| parse_branch_rule(body))
    })
    }
    fn parse_match_rule(body: &str) -> Option<Rule> {
    let oqi = body.find('\"')?;
    let cqi = body.rfind('\"')?;
    Some(Rule::Match(String::from(&body[oqi + 1 .. cqi])))
    }
    fn parse_branch_rule(body: &str) -> Option<Rule> {
    use std::str::FromStr;
    let mut result = Vec::new();
    for a in body.split('|') {
    let mut branch = Vec::new();
    for b in a.split_whitespace() {
    branch.push(usize::from_str(b).ok()?);
    }
    result.push(branch);
    }
    Some(Rule::Branch(result))
    }
    async fn read_rules<I: Unpin + AsyncBufRead>(source: &mut I) -> Vec<Rule> {
    let mut result = Vec::new();
    loop {
    let mut buf = String::new();
    match source.read_line(&mut buf).await {
    Ok(0) => break,
    Ok(_) => match parse_rule(buf) {
    None => break,
    Some(rule) => {
    result.push(rule);
    }
    },
    _ => break,
    }
    }
    result
    }
    async fn check_match(rules: Vec<Rule>, rule_index: usize, text: String) -> Option<HashSet<String>> {
    tokio::task::yield_now().await;
    match &rules[rule_index] {
    Rule::Match(lit) => text.strip_prefix(<String as AsRef<str>>::as_ref(lit)).map(|s| {
    let mut r = HashSet::with_capacity(1);
    r.insert(String::from(s));
    r
    }),
    Rule::Branch(branches) => {
    let jhs: Vec<_> = branches.iter().map(|branch| {
    let branch = branch.clone();
    let text = text.clone();
    tokio::spawn(async {
    tokio::task::yield_now().await;
    let mut remainders = HashSet::with_capacity(1);
    remainders.insert(text);
    for child in branch {
    let mut nr: HashSet<String> = HashSet::new();
    for r in remainders {
    let recursive = Box::new(check_match(rules, child, r)) as Box<dyn Future<Output=Option<HashSet<String>>> + Unpin + Send>;
    for r2 in recursive.await.unwrap_or_else(|| HashSet::new()) {
    }
    }
    }
    Some(remainders)
    })
    }).collect();
    todo!()
    }
    }
    }
    async fn write_str<S: io::AsyncWriteExt + Unpin>(sink: &mut S, text: String) -> io::Result<usize> {
    sink.write_buf(&mut Bytes::from(text)).await
    }
    #[tokio::main(flavor = "multi_thread")]
    async fn main() -> io::Result<()> {
    let stdout = &mut io::stdout();
    let args: Vec<_> = std::env::args().collect();
    let mut input = BufReader::new(tokio::fs::File::open(args[1].clone()).await?);
    let rules = read_rules(&mut input).await;
    write_str(stdout, format!("rules.len() = {}\n", rules.len())).await?;
    Ok(())
    }
  • file addition: Cargo.toml (----------)
    [2.17]
    [package]
    name = "aoc-2020"
    version = "0.1.0"
    edition = "2018"
    # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
    [dependencies]
    tokio = { version = "1", features = ["full"] }
    bytes = "1"
    [[bin]]
    name = "day1"
    path = "day1.rs"
    [[bin]]
    name = "day3"
    path = "day3.rs"
    [[bin]]
    name = "day4"
    path = "day4.rs"
    [[bin]]
    name = "day5"
    path = "day5.rs"
    [[bin]]
    name = "day6"
    path = "day6.rs"
    [[bin]]
    name = "day7"
    path = "day7.rs"
    [[bin]]
    name = "day11"
    path = "day11.rs"
    [[bin]]
    name = "day12"
    path = "day12.rs"
    [[bin]]
    name = "day14"
    path = "day14.rs"
    [[bin]]
    name = "day15"
    path = "day15.rs"
    [[bin]]
    name = "day19"
    path = "day19.rs"
  • file addition: Cargo.lock (----------)
    [2.17]
    # This file is automatically @generated by Cargo.
    # It is not intended for manual editing.
    version = 3
    [[package]]
    name = "aoc-2020"
    version = "0.1.0"
    dependencies = [
    "bytes",
    "tokio",
    ]
    [[package]]
    name = "autocfg"
    version = "1.0.1"
    source = "registry+https://github.com/rust-lang/crates.io-index"
    checksum = "cdb031dd78e28731d87d56cc8ffef4a8f36ca26c38fe2de700543e627f8a464a"
    [[package]]
    name = "bitflags"
    version = "1.2.1"
    source = "registry+https://github.com/rust-lang/crates.io-index"
    checksum = "cf1de2fe8c75bc145a2f577add951f8134889b4795d47466a54a5c846d691693"
    [[package]]
    name = "bytes"
    version = "1.0.1"
    source = "registry+https://github.com/rust-lang/crates.io-index"
    checksum = "b700ce4376041dcd0a327fd0097c41095743c4c8af8887265942faf1100bd040"
    [[package]]
    name = "cfg-if"
    version = "1.0.0"
    source = "registry+https://github.com/rust-lang/crates.io-index"
    checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
    [[package]]
    name = "hermit-abi"
    version = "0.1.19"
    source = "registry+https://github.com/rust-lang/crates.io-index"
    checksum = "62b467343b94ba476dcb2500d242dadbb39557df889310ac77c5d99100aaac33"
    dependencies = [
    "libc",
    ]
    [[package]]
    name = "instant"
    version = "0.1.10"
    source = "registry+https://github.com/rust-lang/crates.io-index"
    checksum = "bee0328b1209d157ef001c94dd85b4f8f64139adb0eac2659f4b08382b2f474d"
    dependencies = [
    "cfg-if",
    ]
    [[package]]
    name = "libc"
    version = "0.2.99"
    source = "registry+https://github.com/rust-lang/crates.io-index"
    checksum = "a7f823d141fe0a24df1e23b4af4e3c7ba9e5966ec514ea068c93024aa7deb765"
    [[package]]
    name = "lock_api"
    version = "0.4.4"
    source = "registry+https://github.com/rust-lang/crates.io-index"
    checksum = "0382880606dff6d15c9476c416d18690b72742aa7b605bb6dd6ec9030fbf07eb"
    dependencies = [
    "scopeguard",
    ]
    [[package]]
    name = "log"
    version = "0.4.14"
    source = "registry+https://github.com/rust-lang/crates.io-index"
    checksum = "51b9bbe6c47d51fc3e1a9b945965946b4c44142ab8792c50835a980d362c2710"
    dependencies = [
    "cfg-if",
    ]
    [[package]]
    name = "memchr"
    version = "2.4.0"
    source = "registry+https://github.com/rust-lang/crates.io-index"
    checksum = "b16bd47d9e329435e309c58469fe0791c2d0d1ba96ec0954152a5ae2b04387dc"
    [[package]]
    name = "mio"
    version = "0.7.13"
    source = "registry+https://github.com/rust-lang/crates.io-index"
    checksum = "8c2bdb6314ec10835cd3293dd268473a835c02b7b352e788be788b3c6ca6bb16"
    dependencies = [
    "libc",
    "log",
    "miow",
    "ntapi",
    "winapi",
    ]
    [[package]]
    name = "miow"
    version = "0.3.7"
    source = "registry+https://github.com/rust-lang/crates.io-index"
    checksum = "b9f1c5b025cda876f66ef43a113f91ebc9f4ccef34843000e0adf6ebbab84e21"
    dependencies = [
    "winapi",
    ]
    [[package]]
    name = "ntapi"
    version = "0.3.6"
    source = "registry+https://github.com/rust-lang/crates.io-index"
    checksum = "3f6bb902e437b6d86e03cce10a7e2af662292c5dfef23b65899ea3ac9354ad44"
    dependencies = [
    "winapi",
    ]
    [[package]]
    name = "num_cpus"
    version = "1.13.0"
    source = "registry+https://github.com/rust-lang/crates.io-index"
    checksum = "05499f3756671c15885fee9034446956fff3f243d6077b91e5767df161f766b3"
    dependencies = [
    "hermit-abi",
    "libc",
    ]
    [[package]]
    name = "once_cell"
    version = "1.8.0"
    source = "registry+https://github.com/rust-lang/crates.io-index"
    checksum = "692fcb63b64b1758029e0a96ee63e049ce8c5948587f2f7208df04625e5f6b56"
    [[package]]
    name = "parking_lot"
    version = "0.11.1"
    source = "registry+https://github.com/rust-lang/crates.io-index"
    checksum = "6d7744ac029df22dca6284efe4e898991d28e3085c706c972bcd7da4a27a15eb"
    dependencies = [
    "instant",
    "lock_api",
    "parking_lot_core",
    ]
    [[package]]
    name = "parking_lot_core"
    version = "0.8.3"
    source = "registry+https://github.com/rust-lang/crates.io-index"
    checksum = "fa7a782938e745763fe6907fc6ba86946d72f49fe7e21de074e08128a99fb018"
    dependencies = [
    "cfg-if",
    "instant",
    "libc",
    "redox_syscall",
    "smallvec",
    "winapi",
    ]
    [[package]]
    name = "pin-project-lite"
    version = "0.2.7"
    source = "registry+https://github.com/rust-lang/crates.io-index"
    checksum = "8d31d11c69a6b52a174b42bdc0c30e5e11670f90788b2c471c31c1d17d449443"
    [[package]]
    name = "proc-macro2"
    version = "1.0.28"
    source = "registry+https://github.com/rust-lang/crates.io-index"
    checksum = "5c7ed8b8c7b886ea3ed7dde405212185f423ab44682667c8c6dd14aa1d9f6612"
    dependencies = [
    "unicode-xid",
    ]
    [[package]]
    name = "quote"
    version = "1.0.9"
    source = "registry+https://github.com/rust-lang/crates.io-index"
    checksum = "c3d0b9745dc2debf507c8422de05d7226cc1f0644216dfdfead988f9b1ab32a7"
    dependencies = [
    "proc-macro2",
    ]
    [[package]]
    name = "redox_syscall"
    version = "0.2.10"
    source = "registry+https://github.com/rust-lang/crates.io-index"
    checksum = "8383f39639269cde97d255a32bdb68c047337295414940c68bdd30c2e13203ff"
    dependencies = [
    "bitflags",
    ]
    [[package]]
    name = "scopeguard"
    version = "1.1.0"
    source = "registry+https://github.com/rust-lang/crates.io-index"
    checksum = "d29ab0c6d3fc0ee92fe66e2d99f700eab17a8d57d1c1d3b748380fb20baa78cd"
    [[package]]
    name = "signal-hook-registry"
    version = "1.4.0"
    source = "registry+https://github.com/rust-lang/crates.io-index"
    checksum = "e51e73328dc4ac0c7ccbda3a494dfa03df1de2f46018127f60c693f2648455b0"
    dependencies = [
    "libc",
    ]
    [[package]]
    name = "smallvec"
    version = "1.6.1"
    source = "registry+https://github.com/rust-lang/crates.io-index"
    checksum = "fe0f37c9e8f3c5a4a66ad655a93c74daac4ad00c441533bf5c6e7990bb42604e"
    [[package]]
    name = "syn"
    version = "1.0.74"
    source = "registry+https://github.com/rust-lang/crates.io-index"
    checksum = "1873d832550d4588c3dbc20f01361ab00bfe741048f71e3fecf145a7cc18b29c"
    dependencies = [
    "proc-macro2",
    "quote",
    "unicode-xid",
    ]
    [[package]]
    name = "tokio"
    version = "1.9.0"
    source = "registry+https://github.com/rust-lang/crates.io-index"
    checksum = "4b7b349f11a7047e6d1276853e612d152f5e8a352c61917887cc2169e2366b4c"
    dependencies = [
    "autocfg",
    "bytes",
    "libc",
    "memchr",
    "mio",
    "num_cpus",
    "once_cell",
    "parking_lot",
    "pin-project-lite",
    "signal-hook-registry",
    "tokio-macros",
    "winapi",
    ]
    [[package]]
    name = "tokio-macros"
    version = "1.3.0"
    source = "registry+https://github.com/rust-lang/crates.io-index"
    checksum = "54473be61f4ebe4efd09cec9bd5d16fa51d70ea0192213d754d2d500457db110"
    dependencies = [
    "proc-macro2",
    "quote",
    "syn",
    ]
    [[package]]
    name = "unicode-xid"
    version = "0.2.2"
    source = "registry+https://github.com/rust-lang/crates.io-index"
    checksum = "8ccb82d61f80a663efe1f787a51b16b5a51e3314d6ac365b08639f52387b33f3"
    [[package]]
    name = "winapi"
    version = "0.3.9"
    source = "registry+https://github.com/rust-lang/crates.io-index"
    checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419"
    dependencies = [
    "winapi-i686-pc-windows-gnu",
    "winapi-x86_64-pc-windows-gnu",
    ]
    [[package]]
    name = "winapi-i686-pc-windows-gnu"
    version = "0.4.0"
    source = "registry+https://github.com/rust-lang/crates.io-index"
    checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6"
    [[package]]
    name = "winapi-x86_64-pc-windows-gnu"
    version = "0.4.0"
    source = "registry+https://github.com/rust-lang/crates.io-index"
    checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f"