diff options
-rw-r--r-- | 2023/rs/Cargo.lock | 29 | ||||
-rw-r--r-- | 2023/rs/code/two/Cargo.toml | 9 | ||||
-rw-r--r-- | 2023/rs/code/two/src/input.txt | 100 | ||||
-rw-r--r-- | 2023/rs/code/two/src/main.rs | 61 |
4 files changed, 199 insertions, 0 deletions
diff --git a/2023/rs/Cargo.lock b/2023/rs/Cargo.lock index f655c3c..9999ada 100644 --- a/2023/rs/Cargo.lock +++ b/2023/rs/Cargo.lock @@ -3,5 +3,34 @@ version = 3 [[package]] +name = "memchr" +version = "2.6.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f665ee40bc4a3c5590afb1e9677db74a508659dfd71e126420da8274909a0167" + +[[package]] +name = "minimal-lexical" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "68354c5c6bd36d73ff3feceb05efa59b6acb7626617f4962be322a825e61f79a" + +[[package]] +name = "nom" +version = "7.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d273983c5a657a70a3e8f2a01329822f3b8c8172b73826411a55751e404a0a4a" +dependencies = [ + "memchr", + "minimal-lexical", +] + +[[package]] name = "one" version = "0.1.0" + +[[package]] +name = "two" +version = "0.1.0" +dependencies = [ + "nom", +] diff --git a/2023/rs/code/two/Cargo.toml b/2023/rs/code/two/Cargo.toml new file mode 100644 index 0000000..5d10422 --- /dev/null +++ b/2023/rs/code/two/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "two" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +nom = "7.1.3" diff --git a/2023/rs/code/two/src/input.txt b/2023/rs/code/two/src/input.txt new file mode 100644 index 0000000..61950d0 --- /dev/null +++ b/2023/rs/code/two/src/input.txt @@ -0,0 +1,100 @@ +Game 1: 10 green, 5 blue; 1 red, 9 green, 10 blue; 5 blue, 6 green, 2 red; 7 green, 9 blue, 1 red; 2 red, 10 blue, 10 green; 7 blue, 1 red +Game 2: 7 green, 5 red, 3 blue; 4 blue, 7 green, 8 red; 9 blue, 4 green; 6 green, 3 red, 4 blue +Game 3: 2 green, 4 blue, 13 red; 15 blue, 9 red, 3 green; 3 red, 18 blue, 3 green; 6 red, 4 green, 2 blue; 6 blue, 13 red +Game 4: 9 red, 1 green, 13 blue; 3 red; 2 blue, 6 red, 1 green; 12 blue, 2 red +Game 5: 1 red, 8 green; 2 red, 8 green, 8 blue; 1 red, 11 green; 5 blue, 11 green; 11 blue, 2 green; 10 blue, 2 red, 1 green +Game 6: 1 red, 12 blue; 20 blue, 3 green, 2 red; 4 red, 15 blue +Game 7: 13 red, 9 green, 1 blue; 8 green, 2 red, 6 blue; 4 green, 5 blue; 7 red, 3 green, 7 blue; 19 red, 5 blue, 1 green +Game 8: 11 red, 14 green, 4 blue; 2 blue, 5 green, 16 red; 18 blue, 11 red, 2 green; 2 blue, 15 red; 13 green, 8 blue +Game 9: 7 green, 5 blue, 11 red; 10 red, 7 green, 4 blue; 1 red; 6 green, 2 blue, 9 red; 8 green, 10 red, 6 blue; 5 red, 5 green, 7 blue +Game 10: 4 blue, 2 green, 1 red; 5 green, 2 red, 1 blue; 3 green, 8 blue, 1 red; 2 blue, 6 green, 2 red; 1 red, 4 green, 2 blue +Game 11: 3 red, 4 blue; 8 blue, 7 green, 2 red; 7 blue, 1 red, 6 green; 13 blue, 4 green +Game 12: 2 red, 3 blue, 4 green; 2 blue, 9 red, 8 green; 10 red, 1 blue; 1 green, 7 red, 3 blue; 7 red, 2 blue, 9 green +Game 13: 12 red, 6 green, 2 blue; 15 green, 2 red, 4 blue; 7 green, 1 red, 3 blue +Game 14: 9 green, 4 red; 6 blue, 1 red, 7 green; 3 blue, 5 green +Game 15: 7 red, 3 green, 2 blue; 3 blue, 4 green; 4 blue, 4 green, 9 red +Game 16: 12 blue, 11 green, 4 red; 8 blue, 9 red, 10 green; 9 green, 11 blue, 13 red; 10 red, 5 blue, 6 green; 2 red; 2 blue, 5 green, 5 red +Game 17: 3 red, 2 green, 2 blue; 1 blue, 3 red, 1 green; 10 green +Game 18: 3 green, 1 blue, 4 red; 12 red, 5 green; 3 red, 3 green, 3 blue; 13 red, 2 blue +Game 19: 13 blue, 8 green, 6 red; 10 red, 12 blue; 8 green, 13 red, 9 blue; 13 green, 3 red, 5 blue; 5 green, 1 blue, 2 red +Game 20: 19 red, 13 blue, 4 green; 1 red, 4 green, 8 blue; 14 red, 6 blue, 7 green; 11 red, 13 blue, 8 green +Game 21: 3 green, 13 red, 7 blue; 1 blue, 1 green, 1 red; 3 blue, 15 red, 5 green; 3 blue, 15 red, 2 green; 6 green, 9 red, 14 blue +Game 22: 2 red, 6 green, 4 blue; 6 green, 2 red; 1 blue, 4 red, 3 green; 11 green, 7 blue, 1 red; 4 red, 8 green, 3 blue +Game 23: 14 blue; 3 green, 2 red, 3 blue; 5 blue, 1 red +Game 24: 12 red; 5 blue, 16 red; 2 blue, 1 green, 16 red; 1 green, 11 red; 2 blue, 8 red, 1 green +Game 25: 4 red, 11 blue, 1 green; 7 red, 9 blue; 6 blue, 10 green +Game 26: 3 green, 13 red; 7 blue, 13 red, 5 green; 5 blue, 8 green, 11 red; 7 blue, 18 green, 6 red +Game 27: 6 green, 6 red, 5 blue; 2 blue, 4 green, 11 red; 15 red, 6 green; 4 green, 12 red, 2 blue; 3 blue, 5 red +Game 28: 16 blue, 6 red, 1 green; 7 red, 4 green, 10 blue; 1 red, 4 green +Game 29: 5 blue, 4 red; 6 blue, 3 red, 4 green; 2 green, 4 red, 5 blue; 1 green, 7 blue, 4 red; 3 green, 2 blue, 4 red +Game 30: 2 green; 14 green, 1 blue, 2 red; 5 red, 14 green +Game 31: 9 blue, 6 red, 7 green; 20 red, 1 green, 15 blue; 6 blue, 7 green, 17 red; 2 blue, 3 green, 6 red; 1 red, 3 blue, 2 green; 5 green, 18 red, 6 blue +Game 32: 7 green, 9 blue, 8 red; 8 red, 13 green, 19 blue; 2 red, 9 blue, 3 green; 9 green, 6 blue, 6 red +Game 33: 6 blue, 12 red; 13 blue, 3 green, 15 red; 5 red, 10 blue, 4 green; 11 blue, 6 red +Game 34: 5 green, 16 blue, 6 red; 10 green, 1 blue, 4 red; 2 red, 7 blue, 6 green; 12 green, 4 blue, 4 red +Game 35: 11 green, 3 blue; 1 red, 6 blue, 10 green; 11 green, 3 blue; 1 red, 2 blue; 11 green, 3 blue, 1 red; 3 blue, 2 green +Game 36: 10 green, 6 red, 4 blue; 3 green, 3 blue, 5 red; 6 red, 5 blue, 10 green +Game 37: 8 red, 7 blue, 5 green; 8 blue, 5 green, 14 red; 8 red, 2 blue +Game 38: 4 green, 1 red, 4 blue; 8 green, 11 blue; 7 red, 5 blue +Game 39: 2 blue, 4 red, 4 green; 8 green, 8 red, 1 blue; 3 red +Game 40: 4 green, 3 red, 14 blue; 4 blue, 13 green, 3 red; 12 green, 2 red, 2 blue; 8 green, 1 red, 11 blue; 4 green, 1 red, 1 blue +Game 41: 4 red, 1 green, 2 blue; 4 red; 4 red +Game 42: 12 red, 8 blue, 1 green; 8 green, 6 red, 5 blue; 12 green, 3 red, 13 blue; 1 red, 2 green, 8 blue; 3 green, 5 red, 6 blue +Game 43: 12 green, 3 blue; 13 green, 7 red, 5 blue; 10 green, 4 red, 4 blue +Game 44: 6 green, 2 red, 4 blue; 10 green, 6 red; 5 blue, 15 red, 13 green; 1 blue, 6 red, 3 green; 9 red, 5 green, 3 blue; 6 green, 4 blue, 5 red +Game 45: 10 blue, 14 green; 2 green, 2 red, 12 blue; 7 green, 1 red; 8 blue, 6 green, 1 red +Game 46: 8 red, 10 green, 15 blue; 9 green, 3 red, 17 blue; 2 blue, 10 red, 5 green; 11 blue, 3 green, 9 red; 5 red, 11 blue, 1 green; 7 green, 5 red, 16 blue +Game 47: 10 blue, 1 green, 1 red; 3 red, 8 blue, 7 green; 8 red, 9 blue; 2 green, 8 red, 1 blue +Game 48: 5 blue, 2 green; 2 red, 7 green, 2 blue; 1 blue, 3 green, 1 red +Game 49: 2 green, 6 red, 5 blue; 6 green; 4 blue, 17 red, 5 green +Game 50: 5 blue, 10 green; 6 blue, 4 red, 9 green; 7 red, 4 blue; 7 red, 3 blue, 14 green; 5 blue, 10 green, 9 red; 13 green, 1 blue, 9 red +Game 51: 1 blue, 15 green; 6 green, 2 blue; 5 blue, 1 red, 12 green +Game 52: 3 red, 15 green; 7 blue, 1 red, 14 green; 8 green, 1 red, 12 blue; 1 red, 9 green, 7 blue +Game 53: 2 green, 4 red; 1 red, 1 blue; 3 blue, 1 green; 2 red, 2 blue, 2 green +Game 54: 7 blue, 13 red, 7 green; 1 red, 2 green; 11 red, 10 green, 5 blue; 10 red, 8 green, 5 blue; 8 green, 12 blue, 12 red +Game 55: 18 red, 3 green, 5 blue; 5 green, 3 blue, 7 red; 3 blue, 3 green, 4 red +Game 56: 14 red, 17 green, 2 blue; 5 green, 13 red, 1 blue; 11 red, 20 green +Game 57: 3 red, 6 green, 2 blue; 3 red, 2 green; 2 green, 5 red; 1 blue, 1 green, 2 red +Game 58: 7 blue, 5 green, 9 red; 10 red, 5 green, 9 blue; 2 blue, 3 red, 8 green; 8 blue, 9 red; 7 red, 3 blue, 7 green; 2 green, 7 red, 1 blue +Game 59: 4 green, 3 blue; 10 red, 4 green, 4 blue; 2 green, 14 red, 12 blue; 1 blue, 1 green, 13 red; 10 red, 3 green, 3 blue; 2 green +Game 60: 9 red, 13 blue; 2 green, 5 red, 9 blue; 3 green, 10 blue +Game 61: 2 red, 8 green, 4 blue; 3 green, 2 red; 10 red, 9 green, 12 blue; 11 green, 17 blue, 3 red; 7 green, 1 red, 14 blue +Game 62: 1 green, 5 red, 13 blue; 5 blue, 1 green, 8 red; 2 green, 8 blue, 3 red; 1 green, 8 red +Game 63: 8 green, 15 red, 2 blue; 4 blue, 3 red, 12 green; 4 green, 1 blue, 17 red; 9 green, 18 red, 4 blue +Game 64: 7 blue, 17 red, 17 green; 3 blue, 4 green, 3 red; 4 red, 19 green, 1 blue; 11 blue, 14 red; 4 blue, 19 green, 7 red; 1 red, 10 green, 11 blue +Game 65: 1 blue, 17 red, 5 green; 17 red, 3 blue, 2 green; 10 blue, 9 green +Game 66: 5 blue, 6 red; 8 red, 2 blue, 1 green; 2 green, 3 blue; 8 blue, 10 red; 1 green, 2 red, 5 blue; 1 red, 3 blue +Game 67: 12 green, 16 blue, 12 red; 15 red, 1 blue, 3 green; 10 red, 3 green, 10 blue; 2 blue, 6 green, 6 red; 9 red, 8 blue, 7 green +Game 68: 10 red, 7 blue; 12 blue, 9 red; 12 blue, 9 red, 2 green +Game 69: 14 blue, 3 red, 3 green; 7 green, 7 red, 2 blue; 8 blue, 4 green, 8 red; 6 blue, 14 red, 3 green +Game 70: 7 blue, 6 green, 2 red; 2 red, 4 blue, 4 green; 2 red, 5 blue, 3 green; 6 green, 2 blue; 5 blue, 2 red, 2 green +Game 71: 7 green, 15 blue, 3 red; 15 blue, 15 red, 2 green; 10 red, 9 blue; 6 green, 20 blue, 11 red; 12 blue, 3 green, 7 red; 1 red, 7 blue +Game 72: 2 green, 9 blue, 7 red; 5 green, 3 blue, 5 red; 10 blue, 8 red, 7 green +Game 73: 18 blue, 5 red, 1 green; 18 blue, 3 red, 9 green; 2 red, 4 blue, 9 green; 5 blue, 5 red; 2 blue, 10 green, 6 red +Game 74: 1 blue, 10 green, 5 red; 4 green, 12 blue, 6 red; 7 red, 13 green, 3 blue; 5 blue, 8 green, 4 red +Game 75: 4 red, 2 blue, 5 green; 2 blue, 7 red, 4 green; 2 blue, 4 green, 3 red; 12 green, 2 blue; 10 green, 1 blue, 2 red +Game 76: 8 green, 6 blue, 5 red; 1 red, 2 blue, 9 green; 7 red, 9 green; 5 green, 1 blue, 11 red +Game 77: 3 blue, 10 red, 9 green; 7 blue, 6 red, 4 green; 4 red, 1 green, 8 blue +Game 78: 2 blue, 1 red, 14 green; 11 green, 1 blue; 15 green, 1 red +Game 79: 3 green, 17 blue, 1 red; 3 red, 2 blue, 10 green; 13 blue, 11 green, 5 red; 16 blue, 2 green, 16 red; 11 green, 1 blue, 14 red +Game 80: 7 red, 10 blue, 5 green; 6 blue, 6 green, 8 red; 6 blue, 3 green, 5 red +Game 81: 1 blue, 14 red, 6 green; 1 red, 13 blue, 12 green; 2 green, 15 red, 15 blue +Game 82: 5 blue, 8 red, 6 green; 19 blue, 4 green; 9 green, 15 blue, 3 red +Game 83: 19 red, 15 green, 2 blue; 17 red, 4 green, 1 blue; 13 green, 18 red +Game 84: 9 green, 14 red; 11 green, 14 red, 1 blue; 1 blue, 2 red, 3 green; 13 green, 10 red; 1 green, 5 red +Game 85: 4 red, 2 green, 11 blue; 8 blue, 3 red; 4 red, 1 blue, 5 green; 2 red, 3 green; 1 green, 8 red, 12 blue +Game 86: 5 blue, 1 red; 8 blue; 2 red, 1 green, 12 blue; 12 blue, 2 red +Game 87: 3 red, 10 green, 3 blue; 13 blue, 6 red, 2 green; 1 green, 2 red, 10 blue +Game 88: 10 red, 3 green, 8 blue; 3 red, 18 blue, 2 green; 3 green, 15 blue; 15 green, 16 blue, 8 red +Game 89: 10 blue, 1 red; 4 green, 9 red, 13 blue; 10 red, 3 green, 12 blue; 2 green, 1 red, 16 blue; 10 blue, 1 red, 6 green +Game 90: 4 red, 2 blue, 15 green; 5 red, 1 blue, 12 green; 3 blue, 3 red, 7 green; 4 blue, 3 red, 7 green; 1 red, 2 green, 1 blue; 1 blue, 4 green, 3 red +Game 91: 16 red, 10 blue, 1 green; 13 green, 13 red, 19 blue; 11 blue, 12 green, 2 red +Game 92: 8 blue, 4 green, 5 red; 7 blue, 4 red; 2 green, 15 blue; 16 blue, 4 red; 1 red, 7 green, 16 blue; 11 blue, 1 red, 3 green +Game 93: 12 green, 2 blue, 2 red; 8 red, 16 green, 8 blue; 15 red, 4 blue, 7 green; 1 red, 4 blue, 15 green; 13 green, 5 red, 4 blue; 5 green, 8 blue, 12 red +Game 94: 13 green, 10 red; 11 red, 19 green, 1 blue; 1 blue, 10 red, 12 green; 18 green, 9 red, 1 blue; 8 green, 1 red +Game 95: 3 green, 4 blue; 2 red, 2 green, 2 blue; 7 red, 3 green +Game 96: 5 red, 7 green; 4 blue, 14 green, 10 red; 13 green; 13 green, 3 blue; 13 green, 1 red, 3 blue; 12 red, 1 green +Game 97: 2 green, 1 blue; 9 red; 4 blue, 8 red; 4 green, 1 red, 14 blue; 2 green, 9 blue; 1 red, 6 blue, 2 green +Game 98: 12 green, 9 blue, 13 red; 6 red, 7 blue; 2 blue, 2 green +Game 99: 9 red, 3 green, 10 blue; 10 red, 10 blue, 4 green; 2 green, 15 blue, 3 red; 12 blue, 4 red +Game 100: 15 blue, 6 red; 1 green, 2 red; 12 blue, 8 green, 1 red; 1 red, 7 blue diff --git a/2023/rs/code/two/src/main.rs b/2023/rs/code/two/src/main.rs new file mode 100644 index 0000000..3effd9a --- /dev/null +++ b/2023/rs/code/two/src/main.rs @@ -0,0 +1,61 @@ +use nom::{Parser, IResult}; +use nom::character::complete::*; +use nom::bytes::complete::*; +use nom::sequence::*; +use nom::combinator::*; +use nom::number::complete as number; +use nom::multi::*; + +struct Game { + pub id: u32, + pub rounds: Vec<Round> +} +#[derive(Default)] +struct Round { + pub red: u8, + pub green: u8, + pub blue: u8 +} + +// feck, i think nom may have been a bit too much for this. + +fn parse_game(i: &str) -> IResult<&str, Game> { + separated_pair( + preceded( + tag("Game "), + digit1.map(|s: &str| s.parse().unwrap())), + tag(": "), + separated_list1( + tag("; "), + fold_many0( + terminated( + separated_pair( + digit1.map(|s: &str| s.parse().unwrap()), + tag(" "), + alpha1), + opt(tag(", "))), + || Round::default(), + |mut r, (n, t)| match t { + "red" => {r.red = n; r}, + "green" => {r.green = n; r}, + "blue" => {r.blue = n; r}, + _ => r + }))).map(|(n, r)| Game{id: n, rounds: r}).parse(i) +} + +static INPUT: &str = include_str!("input.txt"); + +fn main() { + let valueified = INPUT.split("\n").filter(|s| !s.is_empty()).map(|g| parse_game(g).unwrap().1); + + let valid_id_sum = valueified.clone().filter(|g| !g.rounds.iter().any(|r| r.red > 12 || r.green > 13 || r.blue > 14)).map(|g| g.id).sum::<u32>(); + println!("Sum of valid IDs: {valid_id_sum}"); + + let game_power_sum = valueified.map(|g| { + let red = g.rounds.iter().map(|r| r.red).max().unwrap_or(0); + let green = g.rounds.iter().map(|r| r.green).max().unwrap_or(0); + let blue = g.rounds.iter().map(|r| r.blue).max().unwrap_or(0); + red as u32 * green as u32 * blue as u32 + }).sum::<u32>(); + println!("Sum of game powers: {game_power_sum}"); +} |