2022 day 2 part 1 solution

quickdudley
May 2, 2023, 2:22 AM
TFYEI26JCDNNRLJVONAERLSHNDKQ4V5APBGDJJ6GUGKP3KU7Y4WQC

Dependencies

Change contents

  • file addition: 2022 (d--r------)
    [2.1]
  • file addition: day2.rs (----------)
    [0.16]
    use std::{collections::HashMap, fmt::Display, path::PathBuf};
    #[derive(clap::Parser, Debug)]
    struct Args {
    input: PathBuf,
    }
    #[derive(Copy, Clone, Debug)]
    enum Hand {
    Rock,
    Paper,
    Scissors,
    }
    #[derive(Copy, Clone, Debug, Hash)]
    enum Round {
    Loss,
    Draw,
    Win,
    }
    impl Display for Hand {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error> {
    match self {
    Self::Rock => write!(f, "Rock"),
    Self::Paper => write!(f, "Paper"),
    Self::Scissors => write!(f, "Scissors"),
    }
    }
    }
    impl Hand {
    fn round(self, other: Self) -> Round {
    use Round::*;
    match (self, other) {
    (Hand::Rock, Hand::Rock) => Draw,
    (Hand::Rock, Hand::Paper) => Loss,
    (Hand::Rock, Hand::Scissors) => Win,
    (Hand::Paper, Hand::Rock) => Win,
    (Hand::Paper, Hand::Paper) => Draw,
    (Hand::Paper, Hand::Scissors) => Loss,
    (Hand::Scissors, Hand::Rock) => Loss,
    (Hand::Scissors, Hand::Paper) => Win,
    (Hand::Scissors, Hand::Scissors) => Draw,
    }
    }
    }
    #[derive(Debug)]
    enum ParseStreamError {
    UnknownCommand,
    BrokenPair,
    }
    impl std::error::Error for ParseStreamError {}
    impl Display for ParseStreamError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error> {
    <Self as std::fmt::Debug>::fmt(self, f)
    }
    }
    fn parse_stream<'a, I: Iterator<Item = char> + 'a>(
    stream: I,
    mapping: &'a HashMap<char, Hand>,
    ) -> impl Iterator<Item = Result<(Hand, Hand), ParseStreamError>> + 'a {
    struct ParseStream<'a, I> {
    stream: I,
    mapping: &'a HashMap<char, Hand>,
    }
    impl<'a, I: Iterator<Item = char>> Iterator for ParseStream<'a, I> {
    type Item = Result<(Hand, Hand), ParseStreamError>;
    fn next(&mut self) -> Option<Self::Item> {
    let a = loop {
    match self.stream.next() {
    Some(c) => match self.mapping.get(&c) {
    Some(a) => break *a,
    None if c.is_whitespace() => (),
    None => return Some(Err(ParseStreamError::UnknownCommand)),
    },
    None => return None,
    }
    };
    let b = loop {
    match self.stream.next() {
    Some(c) if c == '\n' => return Some(Err(ParseStreamError::BrokenPair)),
    Some(c) => match self.mapping.get(&c) {
    Some(b) => break *b,
    None if c.is_whitespace() => (),
    None => return Some(Err(ParseStreamError::UnknownCommand)),
    },
    None => return Some(Err(ParseStreamError::BrokenPair)),
    }
    };
    Some(Ok((a, b)))
    }
    }
    ParseStream { stream, mapping }
    }
    enum ErrGrabber<J, E, I: Iterator<Item = Result<J, E>>> {
    Running(I),
    Finished,
    Error(E),
    }
    impl<J, E, I: Iterator<Item = Result<J, E>>> Iterator for ErrGrabber<J, E, I> {
    type Item = J;
    fn next(&mut self) -> Option<J> {
    match self {
    Self::Running(inner) => match inner.next() {
    Some(Ok(v)) => Some(v),
    Some(Err(e)) => {
    *self = Self::Error(e);
    None
    }
    None => {
    *self = Self::Finished;
    None
    }
    },
    _ => None,
    }
    }
    }
    impl<J, E, I: Iterator<Item = Result<J, E>>> ErrGrabber<J, E, I> {
    fn new(inner: I) -> Self {
    Self::Running(inner)
    }
    fn check(self) -> Result<(), E> {
    match self {
    Self::Error(e) => Err(e),
    _ => Ok(()),
    }
    }
    }
    fn score_1(a: Hand, b: Hand) -> u32 {
    (match a.round(b) {
    Round::Loss => 0,
    Round::Draw => 3,
    Round::Win => 6,
    }) + match a {
    Hand::Rock => 1,
    Hand::Paper => 2,
    Hand::Scissors => 3,
    }
    }
    fn main() -> Result<(), Box<dyn std::error::Error>> {
    use clap::Parser;
    let args = Args::parse();
    let dict = <[(char, Hand); 6] as IntoIterator>::into_iter([
    ('a', Hand::Rock),
    ('b', Hand::Paper),
    ('c', Hand::Scissors),
    ('x', Hand::Rock),
    ('y', Hand::Paper),
    ('z', Hand::Scissors),
    ])
    .collect();
    let mut buffer = String::new();
    let input = {
    use std::io::Read;
    let mut file = std::fs::File::open(&args.input)?;
    file.read_to_string(&mut buffer)?;
    buffer.chars()
    };
    let mut rounds = ErrGrabber::new(parse_stream(input.flat_map(|c| c.to_lowercase()), &dict));
    let score: u32 = (&mut rounds)
    .map(|(theirs, mine)| score_1(mine, theirs))
    .sum();
    rounds.check()?;
    println!("Total score: {}", score);
    Ok(())
    }
  • file addition: Cargo.toml (----------)
    [0.16]
    [package]
    name = "aoc-2022"
    version = "0.1.0"
    edition = "2018"
    # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
    [dependencies]
    clap = { version = "4.2.5", features = ["derive"] }
    [[bin]]
    name = "day2"
    path = "day2.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 = "anstream"
    version = "0.3.2"
    source = "registry+https://github.com/rust-lang/crates.io-index"
    checksum = "0ca84f3628370c59db74ee214b3263d58f9aadd9b4fe7e711fd87dc452b7f163"
    dependencies = [
    "anstyle",
    "anstyle-parse",
    "anstyle-query",
    "anstyle-wincon",
    "colorchoice",
    "is-terminal",
    "utf8parse",
    ]
    [[package]]
    name = "anstyle"
    version = "1.0.0"
    source = "registry+https://github.com/rust-lang/crates.io-index"
    checksum = "41ed9a86bf92ae6580e0a31281f65a1b1d867c0cc68d5346e2ae128dddfa6a7d"
    [[package]]
    name = "anstyle-parse"
    version = "0.2.0"
    source = "registry+https://github.com/rust-lang/crates.io-index"
    checksum = "e765fd216e48e067936442276d1d57399e37bce53c264d6fefbe298080cb57ee"
    dependencies = [
    "utf8parse",
    ]
    [[package]]
    name = "anstyle-query"
    version = "1.0.0"
    source = "registry+https://github.com/rust-lang/crates.io-index"
    checksum = "5ca11d4be1bab0c8bc8734a9aa7bf4ee8316d462a08c6ac5052f888fef5b494b"
    dependencies = [
    "windows-sys",
    ]
    [[package]]
    name = "anstyle-wincon"
    version = "1.0.1"
    source = "registry+https://github.com/rust-lang/crates.io-index"
    checksum = "180abfa45703aebe0093f79badacc01b8fd4ea2e35118747e5811127f926e188"
    dependencies = [
    "anstyle",
    "windows-sys",
    ]
    [[package]]
    name = "aoc-2022"
    version = "0.1.0"
    dependencies = [
    "clap",
    ]
    [[package]]
    name = "bitflags"
    version = "1.3.2"
    source = "registry+https://github.com/rust-lang/crates.io-index"
    checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a"
    [[package]]
    name = "cc"
    version = "1.0.79"
    source = "registry+https://github.com/rust-lang/crates.io-index"
    checksum = "50d30906286121d95be3d479533b458f87493b30a4b5f79a607db8f5d11aa91f"
    [[package]]
    name = "clap"
    version = "4.2.5"
    source = "registry+https://github.com/rust-lang/crates.io-index"
    checksum = "8a1f23fa97e1d1641371b51f35535cb26959b8e27ab50d167a8b996b5bada819"
    dependencies = [
    "clap_builder",
    "clap_derive",
    "once_cell",
    ]
    [[package]]
    name = "clap_builder"
    version = "4.2.5"
    source = "registry+https://github.com/rust-lang/crates.io-index"
    checksum = "0fdc5d93c358224b4d6867ef1356d740de2303e9892edc06c5340daeccd96bab"
    dependencies = [
    "anstream",
    "anstyle",
    "bitflags",
    "clap_lex",
    "strsim",
    ]
    [[package]]
    name = "clap_derive"
    version = "4.2.0"
    source = "registry+https://github.com/rust-lang/crates.io-index"
    checksum = "3f9644cd56d6b87dbe899ef8b053e331c0637664e9e21a33dfcdc36093f5c5c4"
    dependencies = [
    "heck",
    "proc-macro2",
    "quote",
    "syn",
    ]
    [[package]]
    name = "clap_lex"
    version = "0.4.1"
    source = "registry+https://github.com/rust-lang/crates.io-index"
    checksum = "8a2dd5a6fe8c6e3502f568a6353e5273bbb15193ad9a89e457b9970798efbea1"
    [[package]]
    name = "colorchoice"
    version = "1.0.0"
    source = "registry+https://github.com/rust-lang/crates.io-index"
    checksum = "acbf1af155f9b9ef647e42cdc158db4b64a1b61f743629225fde6f3e0be2a7c7"
    [[package]]
    name = "errno"
    version = "0.3.1"
    source = "registry+https://github.com/rust-lang/crates.io-index"
    checksum = "4bcfec3a70f97c962c307b2d2c56e358cf1d00b558d74262b5f929ee8cc7e73a"
    dependencies = [
    "errno-dragonfly",
    "libc",
    "windows-sys",
    ]
    [[package]]
    name = "errno-dragonfly"
    version = "0.1.2"
    source = "registry+https://github.com/rust-lang/crates.io-index"
    checksum = "aa68f1b12764fab894d2755d2518754e71b4fd80ecfb822714a1206c2aab39bf"
    dependencies = [
    "cc",
    "libc",
    ]
    [[package]]
    name = "heck"
    version = "0.4.1"
    source = "registry+https://github.com/rust-lang/crates.io-index"
    checksum = "95505c38b4572b2d910cecb0281560f54b440a19336cbbcb27bf6ce6adc6f5a8"
    [[package]]
    name = "hermit-abi"
    version = "0.3.1"
    source = "registry+https://github.com/rust-lang/crates.io-index"
    checksum = "fed44880c466736ef9a5c5b5facefb5ed0785676d0c02d612db14e54f0d84286"
    [[package]]
    name = "io-lifetimes"
    version = "1.0.10"
    source = "registry+https://github.com/rust-lang/crates.io-index"
    checksum = "9c66c74d2ae7e79a5a8f7ac924adbe38ee42a859c6539ad869eb51f0b52dc220"
    dependencies = [
    "hermit-abi",
    "libc",
    "windows-sys",
    ]
    [[package]]
    name = "is-terminal"
    version = "0.4.7"
    source = "registry+https://github.com/rust-lang/crates.io-index"
    checksum = "adcf93614601c8129ddf72e2d5633df827ba6551541c6d8c59520a371475be1f"
    dependencies = [
    "hermit-abi",
    "io-lifetimes",
    "rustix",
    "windows-sys",
    ]
    [[package]]
    name = "libc"
    version = "0.2.142"
    source = "registry+https://github.com/rust-lang/crates.io-index"
    checksum = "6a987beff54b60ffa6d51982e1aa1146bc42f19bd26be28b0586f252fccf5317"
    [[package]]
    name = "linux-raw-sys"
    version = "0.3.6"
    source = "registry+https://github.com/rust-lang/crates.io-index"
    checksum = "b64f40e5e03e0d54f03845c8197d0291253cdbedfb1cb46b13c2c117554a9f4c"
    [[package]]
    name = "once_cell"
    version = "1.17.1"
    source = "registry+https://github.com/rust-lang/crates.io-index"
    checksum = "b7e5500299e16ebb147ae15a00a942af264cf3688f47923b8fc2cd5858f23ad3"
    [[package]]
    name = "proc-macro2"
    version = "1.0.56"
    source = "registry+https://github.com/rust-lang/crates.io-index"
    checksum = "2b63bdb0cd06f1f4dedf69b254734f9b45af66e4a031e42a7480257d9898b435"
    dependencies = [
    "unicode-ident",
    ]
    [[package]]
    name = "quote"
    version = "1.0.26"
    source = "registry+https://github.com/rust-lang/crates.io-index"
    checksum = "4424af4bf778aae2051a77b60283332f386554255d722233d09fbfc7e30da2fc"
    dependencies = [
    "proc-macro2",
    ]
    [[package]]
    name = "rustix"
    version = "0.37.18"
    source = "registry+https://github.com/rust-lang/crates.io-index"
    checksum = "8bbfc1d1c7c40c01715f47d71444744a81669ca84e8b63e25a55e169b1f86433"
    dependencies = [
    "bitflags",
    "errno",
    "io-lifetimes",
    "libc",
    "linux-raw-sys",
    "windows-sys",
    ]
    [[package]]
    name = "strsim"
    version = "0.10.0"
    source = "registry+https://github.com/rust-lang/crates.io-index"
    checksum = "73473c0e59e6d5812c5dfe2a064a6444949f089e20eec9a2e5506596494e4623"
    [[package]]
    name = "syn"
    version = "2.0.15"
    source = "registry+https://github.com/rust-lang/crates.io-index"
    checksum = "a34fcf3e8b60f57e6a14301a2e916d323af98b0ea63c599441eec8558660c822"
    dependencies = [
    "proc-macro2",
    "quote",
    "unicode-ident",
    ]
    [[package]]
    name = "unicode-ident"
    version = "1.0.8"
    source = "registry+https://github.com/rust-lang/crates.io-index"
    checksum = "e5464a87b239f13a63a501f2701565754bae92d243d4bb7eb12f6d57d2269bf4"
    [[package]]
    name = "utf8parse"
    version = "0.2.1"
    source = "registry+https://github.com/rust-lang/crates.io-index"
    checksum = "711b9620af191e0cdc7468a8d14e709c3dcdb115b36f838e601583af800a370a"
    [[package]]
    name = "windows-sys"
    version = "0.48.0"
    source = "registry+https://github.com/rust-lang/crates.io-index"
    checksum = "677d2418bec65e3338edb076e806bc1ec15693c5d0104683f2efe857f61056a9"
    dependencies = [
    "windows-targets",
    ]
    [[package]]
    name = "windows-targets"
    version = "0.48.0"
    source = "registry+https://github.com/rust-lang/crates.io-index"
    checksum = "7b1eb6f0cd7c80c79759c929114ef071b87354ce476d9d94271031c0497adfd5"
    dependencies = [
    "windows_aarch64_gnullvm",
    "windows_aarch64_msvc",
    "windows_i686_gnu",
    "windows_i686_msvc",
    "windows_x86_64_gnu",
    "windows_x86_64_gnullvm",
    "windows_x86_64_msvc",
    ]
    [[package]]
    name = "windows_aarch64_gnullvm"
    version = "0.48.0"
    source = "registry+https://github.com/rust-lang/crates.io-index"
    checksum = "91ae572e1b79dba883e0d315474df7305d12f569b400fcf90581b06062f7e1bc"
    [[package]]
    name = "windows_aarch64_msvc"
    version = "0.48.0"
    source = "registry+https://github.com/rust-lang/crates.io-index"
    checksum = "b2ef27e0d7bdfcfc7b868b317c1d32c641a6fe4629c171b8928c7b08d98d7cf3"
    [[package]]
    name = "windows_i686_gnu"
    version = "0.48.0"
    source = "registry+https://github.com/rust-lang/crates.io-index"
    checksum = "622a1962a7db830d6fd0a69683c80a18fda201879f0f447f065a3b7467daa241"
    [[package]]
    name = "windows_i686_msvc"
    version = "0.48.0"
    source = "registry+https://github.com/rust-lang/crates.io-index"
    checksum = "4542c6e364ce21bf45d69fdd2a8e455fa38d316158cfd43b3ac1c5b1b19f8e00"
    [[package]]
    name = "windows_x86_64_gnu"
    version = "0.48.0"
    source = "registry+https://github.com/rust-lang/crates.io-index"
    checksum = "ca2b8a661f7628cbd23440e50b05d705db3686f894fc9580820623656af974b1"
    [[package]]
    name = "windows_x86_64_gnullvm"
    version = "0.48.0"
    source = "registry+https://github.com/rust-lang/crates.io-index"
    checksum = "7896dbc1f41e08872e9d5e8f8baa8fdd2677f29468c4e156210174edc7f7b953"
    [[package]]
    name = "windows_x86_64_msvc"
    version = "0.48.0"
    source = "registry+https://github.com/rust-lang/crates.io-index"
    checksum = "1a515f5799fe4961cb532f983ce2b23082366b898e52ffbce459c86f67c8378a"