From f242bf8f282ea85a92b839f3b4e99a57970a5491 Mon Sep 17 00:00:00 2001 From: Thorn Avery Date: Fri, 2 Dec 2022 14:37:27 +1100 Subject: [PATCH 01/10] fixed formatting again --- src/problem02/mod.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/problem02/mod.rs b/src/problem02/mod.rs index 16be535..01a1561 100644 --- a/src/problem02/mod.rs +++ b/src/problem02/mod.rs @@ -1,9 +1,9 @@ static INPUT: &'static str = include_str!("input.txt"); pub fn solutionA() -> i32 { - return 0; + 0; } pub fn solutionB() -> i32 { - return 0; + 0; } From 87f53eb424d648167fef4a129c4657e069cb2b0a Mon Sep 17 00:00:00 2001 From: Thorn Avery Date: Fri, 2 Dec 2022 14:40:20 +1100 Subject: [PATCH 02/10] fixed formatting again --- src/problem02/mod.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/problem02/mod.rs b/src/problem02/mod.rs index 01a1561..3b80ede 100644 --- a/src/problem02/mod.rs +++ b/src/problem02/mod.rs @@ -1,9 +1,9 @@ static INPUT: &'static str = include_str!("input.txt"); pub fn solutionA() -> i32 { - 0; + 0 } pub fn solutionB() -> i32 { - 0; + 0 } From 784cdd62685f2b12d8065e045ac4bd7b9f3d91cd Mon Sep 17 00:00:00 2001 From: Thorn Avery Date: Fri, 2 Dec 2022 16:02:56 +1100 Subject: [PATCH 03/10] merged nix builder into repo --- .gitignore | 3 +- Cargo.lock | 2 +- Cargo.toml | 2 +- flake.lock | 140 ++++++++++++++++++ flake.nix | 98 ++++++++++++ src/lib.rs | 5 +- src/main.rs | 12 +- src/problem02/mod.rs | 9 -- src/{problem01 => problem_01}/description.txt | 0 src/{problem01 => problem_01}/input.txt | 0 src/{problem01 => problem_01}/mod.rs | 18 +-- src/{problem02 => problem_02}/input.txt | 0 src/problem_02/mod.rs | 9 ++ 13 files changed, 267 insertions(+), 31 deletions(-) create mode 100644 flake.lock create mode 100644 flake.nix delete mode 100644 src/problem02/mod.rs rename src/{problem01 => problem_01}/description.txt (100%) rename src/{problem01 => problem_01}/input.txt (100%) rename src/{problem01 => problem_01}/mod.rs (61%) rename src/{problem02 => problem_02}/input.txt (100%) create mode 100644 src/problem_02/mod.rs diff --git a/.gitignore b/.gitignore index ea8c4bf..4075fb2 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ -/target +target +result diff --git a/Cargo.lock b/Cargo.lock index 6966fe4..06f16a2 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -207,7 +207,7 @@ dependencies = [ ] [[package]] -name = "tAoC2022" +name = "taoc2022" version = "0.1.0" dependencies = [ "clap", diff --git a/Cargo.toml b/Cargo.toml index ca3b78f..54d04fd 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,5 +1,5 @@ [package] -name = "tAoC2022" +name = "taoc2022" version = "0.1.0" edition = "2021" diff --git a/flake.lock b/flake.lock new file mode 100644 index 0000000..e9acb03 --- /dev/null +++ b/flake.lock @@ -0,0 +1,140 @@ +{ + "nodes": { + "advisory-db": { + "flake": false, + "locked": { + "lastModified": 1669846878, + "narHash": "sha256-2ks8+eP/MuH07KGUR7zO/cSRPyoYJIPQHDsj+hveN/U=", + "owner": "rustsec", + "repo": "advisory-db", + "rev": "b80f8edaa7e5ce5a6babcd61f14dddee77875c2c", + "type": "github" + }, + "original": { + "owner": "rustsec", + "repo": "advisory-db", + "type": "github" + } + }, + "crane": { + "inputs": { + "flake-compat": "flake-compat", + "flake-utils": "flake-utils", + "nixpkgs": [ + "nixpkgs" + ], + "rust-overlay": "rust-overlay" + }, + "locked": { + "lastModified": 1669605882, + "narHash": "sha256-TiQtL5sUI5rp28S63v+VX25qNjcrc8Xeu+shf3g7Tj4=", + "owner": "ipetkov", + "repo": "crane", + "rev": "24591d5f8cc979f7b243b88a2d39da09976970ad", + "type": "github" + }, + "original": { + "owner": "ipetkov", + "repo": "crane", + "type": "github" + } + }, + "flake-compat": { + "flake": false, + "locked": { + "lastModified": 1650374568, + "narHash": "sha256-Z+s0J8/r907g149rllvwhb4pKi8Wam5ij0st8PwAh+E=", + "owner": "edolstra", + "repo": "flake-compat", + "rev": "b4a34015c698c7793d592d66adbab377907a2be8", + "type": "github" + }, + "original": { + "owner": "edolstra", + "repo": "flake-compat", + "type": "github" + } + }, + "flake-utils": { + "locked": { + "lastModified": 1667395993, + "narHash": "sha256-nuEHfE/LcWyuSWnS8t12N1wc105Qtau+/OdUAjtQ0rA=", + "owner": "numtide", + "repo": "flake-utils", + "rev": "5aed5285a952e0b949eb3ba02c12fa4fcfef535f", + "type": "github" + }, + "original": { + "owner": "numtide", + "repo": "flake-utils", + "type": "github" + } + }, + "flake-utils_2": { + "locked": { + "lastModified": 1667395993, + "narHash": "sha256-nuEHfE/LcWyuSWnS8t12N1wc105Qtau+/OdUAjtQ0rA=", + "owner": "numtide", + "repo": "flake-utils", + "rev": "5aed5285a952e0b949eb3ba02c12fa4fcfef535f", + "type": "github" + }, + "original": { + "owner": "numtide", + "repo": "flake-utils", + "type": "github" + } + }, + "nixpkgs": { + "locked": { + "lastModified": 1669900182, + "narHash": "sha256-EKxjHxRJnP1w+2nnm8pq4Uqkqa5McnfPXcO8cG8Mxzc=", + "owner": "NixOS", + "repo": "nixpkgs", + "rev": "bcb6dbbe30ce7631e5a0865dff1ab9b63d92977d", + "type": "github" + }, + "original": { + "owner": "NixOS", + "ref": "nixpkgs-unstable", + "repo": "nixpkgs", + "type": "github" + } + }, + "root": { + "inputs": { + "advisory-db": "advisory-db", + "crane": "crane", + "flake-utils": "flake-utils_2", + "nixpkgs": "nixpkgs" + } + }, + "rust-overlay": { + "inputs": { + "flake-utils": [ + "crane", + "flake-utils" + ], + "nixpkgs": [ + "crane", + "nixpkgs" + ] + }, + "locked": { + "lastModified": 1667487142, + "narHash": "sha256-bVuzLs1ZVggJAbJmEDVO9G6p8BH3HRaolK70KXvnWnU=", + "owner": "oxalica", + "repo": "rust-overlay", + "rev": "cf668f737ac986c0a89e83b6b2e3c5ddbd8cf33b", + "type": "github" + }, + "original": { + "owner": "oxalica", + "repo": "rust-overlay", + "type": "github" + } + } + }, + "root": "root", + "version": 7 +} diff --git a/flake.nix b/flake.nix new file mode 100644 index 0000000..1fd8b30 --- /dev/null +++ b/flake.nix @@ -0,0 +1,98 @@ +{ + description = "tAoC2022 - Nix Builder"; + + inputs = { + nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable"; + crane = { + url = "github:ipetkov/crane"; + inputs.nixpkgs.follows = "nixpkgs"; + }; + flake-utils.url = "github:numtide/flake-utils"; + advisory-db = { + url = "github:rustsec/advisory-db"; + flake = false; + }; + + # my rust project + # taoc2022 = { + # url = "git+ssh://gitea@git.p7.co.nz/thornAvery/tAoC2022"; + # flake = false; + # }; + + }; + + outputs = { self, nixpkgs, crane, flake-utils, advisory-db, ... }: + flake-utils.lib.eachDefaultSystem (system: + let + pkgs = import nixpkgs { + inherit system; + }; + + inherit (pkgs) lib; + + craneLib = crane.lib.${system}; + src = ./.; + + buildInputs = [ + ] ++ lib.optionals pkgs.stdenv.isDarwin [ + pkgs.libiconv + ]; + + cargoArtifacts = craneLib.buildDepsOnly { + inherit src buildInputs; + }; + + my-crate = craneLib.buildPackage { + inherit cargoArtifacts src buildInputs; + }; + in + { + checks = { + inherit my-crate; + + my-crate-clippy = craneLib.cargoClippy { + inherit cargoArtifacts src buildInputs; + cargoClippyExtraArgs = "--all-targets -- --deny warnings"; + }; + + my-crate-doc = craneLib.cargoDoc { + inherit cargoArtifacts src; + }; + + my-crate-fmt = craneLib.cargoFmt { + inherit src; + }; + + my-crate-audit = craneLib.cargoAudit { + inherit src advisory-db; + }; + + my-crate-nextest = craneLib.cargoNextest { + inherit cargoArtifacts src buildInputs; + partitions = 1; + partitionType = "count"; + }; + } // lib.optionalAttrs (system == "x86_64-linux") { + # cargo-tarpaulin only supports x86_64 systems + my-crate-coverage = craneLib.cargoTarpaulin { + inherit cargoArtifacts src; + }; + }; + + packages.default = my-crate; + + apps.default = flake-utils.lib.mkApp { + drv = my-crate; + }; + + devShells.default = pkgs.mkShell { + inputsFrom = builtins.attrValues self.checks; + RUST_SRC_PATH = "${pkgs.rust.packages.stable.rustPlatform.rustLibSrc}"; + nativeBuildInputs = with pkgs; [ + cargo + rustc + rust-analyzer + ]; + }; + }); +} diff --git a/src/lib.rs b/src/lib.rs index 9a9096e..e62cb92 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,3 +1,2 @@ -#![allow(non_snake_case)] -pub mod problem01; -pub mod problem02; +pub mod problem_01; +pub mod problem_02; diff --git a/src/main.rs b/src/main.rs index 73c6d30..58dc661 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,11 +1,11 @@ -use tAoC2022::problem01; -use tAoC2022::problem02; +use taoc2022::problem_01; +use taoc2022::problem_02; fn main() { println!("== Problem 01 =="); - println!("= a: {}", problem01::solutionA()); - println!("= b: {}", problem01::solutionB()); + println!("= a: {}", problem_01::solution_a()); + println!("= b: {}", problem_01::solution_b()); println!("== Problem 02 =="); - println!("= a: {}", problem02::solutionA()); - println!("= b: {}", problem02::solutionB()); + println!("= a: {}", problem_02::solution_a()); + println!("= b: {}", problem_02::solution_b()); } diff --git a/src/problem02/mod.rs b/src/problem02/mod.rs deleted file mode 100644 index 3b80ede..0000000 --- a/src/problem02/mod.rs +++ /dev/null @@ -1,9 +0,0 @@ -static INPUT: &'static str = include_str!("input.txt"); - -pub fn solutionA() -> i32 { - 0 -} - -pub fn solutionB() -> i32 { - 0 -} diff --git a/src/problem01/description.txt b/src/problem_01/description.txt similarity index 100% rename from src/problem01/description.txt rename to src/problem_01/description.txt diff --git a/src/problem01/input.txt b/src/problem_01/input.txt similarity index 100% rename from src/problem01/input.txt rename to src/problem_01/input.txt diff --git a/src/problem01/mod.rs b/src/problem_01/mod.rs similarity index 61% rename from src/problem01/mod.rs rename to src/problem_01/mod.rs index 52826b8..f15aa26 100644 --- a/src/problem01/mod.rs +++ b/src/problem_01/mod.rs @@ -1,10 +1,10 @@ -static INPUT: &'static str = include_str!("input.txt"); +static INPUT: &str = include_str!("input.txt"); fn solution(x: i32) -> i32 { let mut totals = vec![Default::default(); x.try_into().unwrap()]; let mut total = 0; for line in INPUT.lines() { - if line == "" { + if line.is_empty() { add_to_total(&mut totals, total); total = 0; } else { @@ -15,21 +15,19 @@ fn solution(x: i32) -> i32 { return totals.iter().sum(); } -fn add_to_total(totals: &mut Vec, total: i32) { +fn add_to_total(totals: &mut [i32], total: i32) { let mut i = total; for n in totals.iter_mut() { if i > *n { - let tmp = *n; - *n = i; - i = tmp; + std::mem::swap(&mut (*n), &mut i); }; } } -pub fn solutionA() -> i32 { - return solution(1); +pub fn solution_a() -> i32 { + solution(1) } -pub fn solutionB() -> i32 { - return solution(3); +pub fn solution_b() -> i32 { + solution(3) } diff --git a/src/problem02/input.txt b/src/problem_02/input.txt similarity index 100% rename from src/problem02/input.txt rename to src/problem_02/input.txt diff --git a/src/problem_02/mod.rs b/src/problem_02/mod.rs new file mode 100644 index 0000000..8b43c01 --- /dev/null +++ b/src/problem_02/mod.rs @@ -0,0 +1,9 @@ +// static INPUT: &'static str = include_str!("input.txt"); + +pub fn solution_a() -> i32 { + 0 +} + +pub fn solution_b() -> i32 { + 0 +} From b17fc3d75ccfa6592185f00b4c1078590d267a62 Mon Sep 17 00:00:00 2001 From: Thorn Avery Date: Sat, 3 Dec 2022 14:46:31 +1100 Subject: [PATCH 04/10] day 2 done --- src/problem_02/description.txt | 48 + src/problem_02/input.txt | 2500 ++++++++++++++++++++++++++++++++ src/problem_02/mod.rs | 99 +- src/problem_02/types.rs | 38 + 4 files changed, 2682 insertions(+), 3 deletions(-) create mode 100644 src/problem_02/description.txt create mode 100644 src/problem_02/types.rs diff --git a/src/problem_02/description.txt b/src/problem_02/description.txt new file mode 100644 index 0000000..ff40d71 --- /dev/null +++ b/src/problem_02/description.txt @@ -0,0 +1,48 @@ +--- Day 2: Rock Paper Scissors --- + +The Elves begin to set up camp on the beach. To decide whose tent gets to be closest to the snack storage, a giant Rock Paper Scissors tournament is already in progress. + +Rock Paper Scissors is a game between two players. Each game contains many rounds; in each round, the players each simultaneously choose one of Rock, Paper, or Scissors using a hand shape. Then, a winner for that round is selected: Rock defeats Scissors, Scissors defeats Paper, and Paper defeats Rock. If both players choose the same shape, the round instead ends in a draw. + +Appreciative of your help yesterday, one Elf gives you an encrypted strategy guide (your puzzle input) that they say will be sure to help you win. "The first column is what your opponent is going to play: A for Rock, B for Paper, and C for Scissors. The second column--" Suddenly, the Elf is called away to help with someone's tent. + +The second column, you reason, must be what you should play in response: X for Rock, Y for Paper, and Z for Scissors. Winning every time would be suspicious, so the responses must have been carefully chosen. + +The winner of the whole tournament is the player with the highest score. Your total score is the sum of your scores for each round. The score for a single round is the score for the shape you selected (1 for Rock, 2 for Paper, and 3 for Scissors) plus the score for the outcome of the round (0 if you lost, 3 if the round was a draw, and 6 if you won). + +Since you can't be sure if the Elf is trying to help you or trick you, you should calculate the score you would get if you were to follow the strategy guide. + +For example, suppose you were given the following strategy guide: + +A Y +B X +C Z + +This strategy guide predicts and recommends the following: + + In the first round, your opponent will choose Rock (A), and you should choose Paper (Y). This ends in a win for you with a score of 8 (2 because you chose Paper + 6 because you won). + In the second round, your opponent will choose Paper (B), and you should choose Rock (X). This ends in a loss for you with a score of 1 (1 + 0). + The third round is a draw with both players choosing Scissors, giving you a score of 3 + 3 = 6. + +In this example, if you were to follow the strategy guide, you would get a total score of 15 (8 + 1 + 6). + +What would your total score be if everything goes exactly according to your strategy guide? + +Your puzzle answer was 14069. +--- Part Two --- + +The Elf finishes helping with the tent and sneaks back over to you. "Anyway, the second column says how the round needs to end: X means you need to lose, Y means you need to end the round in a draw, and Z means you need to win. Good luck!" + +The total score is still calculated in the same way, but now you need to figure out what shape to choose so the round ends as indicated. The example above now goes like this: + + In the first round, your opponent will choose Rock (A), and you need the round to end in a draw (Y), so you also choose Rock. This gives you a score of 1 + 3 = 4. + In the second round, your opponent will choose Paper (B), and you choose Rock so you lose (X) with a score of 1 + 0 = 1. + In the third round, you will defeat your opponent's Scissors with Rock for a score of 1 + 6 = 7. + +Now that you're correctly decrypting the ultra top secret strategy guide, you would get a total score of 12. + +Following the Elf's instructions for the second column, what would your total score be if everything goes exactly according to your strategy guide? + +Your puzzle answer was 12411. + +Both parts of this puzzle are complete! They provide two gold stars: ** diff --git a/src/problem_02/input.txt b/src/problem_02/input.txt index e69de29..4473a9d 100644 --- a/src/problem_02/input.txt +++ b/src/problem_02/input.txt @@ -0,0 +1,2500 @@ +C X +C X +C X +A Z +C X +C X +A Y +B X +B Y +B Z +C Y +C X +C X +C Y +B Y +C X +C Y +B Z +C Y +B Z +B Z +A Z +C X +C X +C X +B Z +C Y +C X +C X +B Y +C X +B Z +C X +C X +A Y +C X +A Z +B Z +B Z +B Z +B Y +B Z +C X +B Y +C Y +C Y +B Z +C X +C Y +C X +C X +C X +C Y +A X +A X +C Z +C X +B Z +A Z +C X +B Y +C Y +B X +B Z +C Z +A X +C X +B X +C Y +C Y +B Z +B Z +B Z +C Z +A Z +A Z +C X +C X +C Z +B Z +B Y +C X +A Z +C X +B Y +C Y +B Z +C X +A Y +C Y +A X +B Z +C Y +C Y +C X +C Y +C X +B X +C Y +C X +B Y +A Z +B X +B Y +C Z +C X +C X +C X +B Z +B X +B Z +C X +B Y +B Z +C X +C Y +B Y +C Y +C X +B Z +B Y +B X +A Z +A Z +C X +C X +B Z +B Y +C X +C Y +C Y +A X +C X +C Y +A Y +C X +A Z +C Y +B X +B X +B X +B Y +B Y +C Z +C X +A Y +A Z +C X +B Z +C Y +C Y +A Y +B Z +B Z +C X +C Y +B X +C Y +B X +A Y +C X +B Z +A Z +C Y +A Z +A Y +C Z +B Y +A Z +A Y +C X +B Y +B X +C X +A Y +B Z +B Z +B Y +B Z +C X +C Z +B Y +B Y +A Y +B Z +B X +B Z +A X +C Y +B Z +A X +A X +B Y +C Z +C Z +B Z +B X +B X +A Z +A Y +A Y +C Z +C X +C X +C X +B Z +C Y +C Y +C Y +B X +B Y +A Y +B Z +B X +B Z +C X +B X +C X +C X +B Z +C Z +C Y +C X +C X +B Z +A Y +C X +C X +A Z +B Y +B X +B Y +B Z +C Y +A X +B Y +C X +A Z +B Z +A Y +C X +B Y +B Z +A Z +B Z +C Y +B Z +A X +A Y +B X +A X +B Y +C X +A X +B X +A X +A Z +B Z +C X +C X +A Y +B Z +C X +A Z +B Z +C X +C X +A Z +C Y +B Z +C X +B Y +B Y +A Z +C X +B Z +B Y +A Z +B Y +C X +C Y +A Y +B Y +C Z +A Y +C Z +A Z +B Z +C X +A X +B Z +C X +C Y +A X +C X +A Z +C Z +C X +C Z +B Z +C Y +B Y +B Z +C X +C X +B Y +C X +B Z +C Y +A X +A Z +B X +A Z +C Z +C Y +C Y +B Z +C Y +C X +C Y +B Y +C X +C X +C X +B Z +C X +C X +B Y +C Y +A Z +B Z +C Z +B Z +A Y +B Z +C X +C Z +B X +A Z +C X +C X +C Y +B Z +C Z +B Z +A Y +B Z +C Z +B Z +A Z +C X +A Z +A X +C X +B X +C X +C X +C Y +C X +C X +B Z +C X +A Z +B Z +B Y +C X +A Y +C Y +B Z +B Z +C X +A X +C X +B Z +C X +B X +B X +B Y +B Z +B X +B Y +C X +C X +B Y +C X +C X +A X +B Y +A Y +B Z +B Z +A X +C X +C X +C X +A Z +C X +B Y +A Z +A Y +B Z +B Y +B Z +C X +A Y +C X +C X +B Y +A X +A X +B X +C Y +C X +C X +B Y +B X +B Z +C Y +B X +C X +B Z +B Z +B Z +C X +C Z +A X +C X +C Z +A Y +B X +C Y +C Y +B Z +B Z +C Y +C X +A Z +A Z +C X +C Z +C X +C X +C X +C Y +B Z +B Y +B Z +C Y +A Y +A Z +C Y +C Z +C Z +B Z +B X +C X +C X +C X +B Y +C X +C X +C Z +A Z +C X +B X +B X +C X +C X +C X +C Y +B Z +B X +C Y +C X +B Y +A Y +A Z +C X +C Y +A Y +B Z +C X +B X +C X +C X +B X +B Z +B Y +C X +C Y +A Y +C X +C X +B Z +C Y +B Z +C Y +B Y +C Y +B Z +B Y +B Z +B Z +C X +B Z +A X +C Y +C X +C X +C X +C X +C Z +B Z +C X +C Y +C Z +C X +C Y +B Z +B Z +A Z +B X +A Z +B Y +A Y +C X +C X +C X +C X +C X +C X +A X +C X +C Y +B Z +C Y +C Y +C Y +B Y +C X +B Z +B Z +A X +C Z +C Z +B Y +B Z +C X +A X +C Z +B Z +B Z +C Z +B X +C X +B Z +A Z +B Z +C X +A X +B Y +B X +B Y +B Z +B Z +C Y +B X +B Y +C X +B Z +B Z +B Y +B Z +A Y +A X +C Z +C Y +B Z +B Z +A Y +A X +B Y +C X +B X +A Y +B Y +A Z +A X +B Y +B Z +A Z +C X +C X +C Y +C Y +B Y +C X +A Z +B Z +A X +C X +C X +B X +C Z +B Y +C Z +C Y +C X +B X +C Y +C Z +C X +B Z +C Y +C Y +B X +B Y +C X +B X +B X +A X +A Z +A X +A Y +C Z +B Z +C Y +C X +B X +A X +C Y +C X +C X +B X +B Y +A X +B Y +A Z +C X +B Z +C Y +B Z +B Z +B Z +C X +B Z +B X +C Y +C Y +C Y +B Z +B X +C Y +C Z +A Z +C Y +B Z +B X +B Z +B Y +A X +B Y +B Y +A Z +A X +B Z +C X +C Y +C X +C X +C X +A X +A Z +C Y +B Z +B X +C Z +C Y +C X +A X +C X +B X +C X +A X +B Y +C Y +B Z +C X +B Z +A Y +C X +C Y +A Z +B Z +B Z +C X +A Z +A X +C Y +C Y +A Z +C X +C Z +C Y +C X +C X +A Y +C X +C Y +A Y +C Y +C X +C Z +C X +A Y +C X +B Z +C X +C X +C X +B Y +A Y +A Y +C X +C X +B Z +B Z +C X +B Z +C X +A Y +B Y +C Y +B Z +C X +A X +C X +C X +C X +B X +B Y +A X +A X +B Z +C X +B Z +C X +C X +A Y +A Z +B Z +C X +B Z +B X +C X +B Y +B Z +B Z +C X +B Z +B Z +B Z +C X +B Y +B Z +A Z +C X +C X +B Z +A Y +B Y +C X +B Z +B Z +A Y +B X +B X +A X +A X +B Y +C X +A Z +B Y +B X +C X +B Z +B Y +A Z +C X +C X +B Z +C Y +B Z +C X +C Y +C X +C Z +C X +B Z +B Z +B Z +C X +C X +C X +B Z +C Y +B Y +B Z +C Z +C X +B Y +C X +A X +C Z +C X +C X +B Z +A Z +C X +C Y +C X +B Y +C Y +B X +C Z +C X +C Z +C X +B Z +B Y +B Y +A Z +B Y +C X +A X +C X +C Y +B Z +C X +C Z +C Z +B Y +C Y +B Z +C Y +C Y +C X +C X +A Y +B Y +B Y +C Y +B X +B Z +C X +C X +C Y +C X +B X +B Y +A Z +B Z +B Z +C X +C X +B Y +C X +B X +B Z +B Y +B Y +B Z +B Z +A Y +B Y +B Y +B X +A Y +C X +C Y +A Z +B Z +C X +B Y +C X +A Z +B Y +C X +C X +B Y +B Y +C Y +C X +B Y +C X +C X +B Z +C X +C X +C Y +B Z +C Y +C Y +A Z +C Y +B Y +C X +C Z +A X +C X +B Y +B Z +C Y +A Z +B Y +C Y +C Z +B Y +B Y +C X +C Z +B Z +C Z +C X +C X +C X +B Y +B Y +B Y +A Z +B Y +B Y +B Z +A Y +B Z +C X +C Z +B Z +C Z +B Y +B Z +C X +C X +C Y +C X +B Y +A Z +C Z +B Z +C X +B Y +A X +C X +A X +A Z +C X +B Y +B Z +B Z +A Z +C Z +C Y +C X +C Z +B X +C Y +C Z +B X +B X +A X +A Z +B Z +A Z +A X +C X +C X +A X +B Z +C Z +C Z +A X +B X +C X +B Z +B Y +A Z +A X +B Y +B Z +C X +B X +B Y +C X +C X +B Z +C Z +C X +C X +B Z +C X +B Z +C Y +C Y +C Y +B Z +C X +A X +B Y +B Y +A Y +C X +C X +C X +B Y +A Z +C X +C Y +C X +C X +B X +B Z +C X +C Z +A Z +B Z +C X +A Z +B Z +A X +C X +C Y +B Z +B X +B Y +B Z +C X +C X +C X +C X +B Y +C X +B Y +C X +C Y +B Z +C Y +C X +A X +C Z +C Y +A X +C X +B Z +B Y +B Y +C Y +C X +C Y +C X +B Z +B Z +C Y +B Z +B Z +B Z +B Z +C X +B Z +C X +A Z +B Z +B Y +A X +B Z +C Y +B Y +C Z +C X +A X +C X +A Y +B Z +B Y +B Z +C X +C Y +A Y +C X +C X +B Z +B Z +A Z +C X +A Z +C X +A X +B Y +C X +A Z +B Z +B Z +C X +C X +C Z +B Z +C X +A X +B X +B Y +C X +C X +C X +C Y +B Z +C Y +C X +C X +B Y +C X +A Z +A Z +C Y +B Y +B X +B Z +B Y +C Y +C Y +C X +A Z +B Z +B X +C X +C X +A X +C Z +A Y +C X +C Y +A Z +C Z +B Z +B Z +C X +B X +A Z +B Y +C X +A Z +C X +C Y +B Y +B Y +B X +B Z +C Y +B X +C X +B Y +C X +B Y +C X +B Y +A Y +A Z +C Y +A X +C X +C Z +B X +C Z +C Y +C Y +C X +C X +C X +C Y +C Z +A Z +B Z +C X +A X +A Z +B Y +C X +C X +C Z +C X +B Z +C X +B Y +B Z +B Y +C X +C Y +B Z +C Y +A X +C X +A Z +B Y +B Z +C X +B Z +C X +C Z +B Y +A X +C X +C X +C X +C X +B X +B Z +A Y +B Y +C X +B Z +C Z +C Y +A Z +C Y +A Y +B Z +C X +B Z +B Y +C Y +C Y +C Y +B Z +B Z +C X +C Z +C Z +B Y +B Y +C Z +B Y +C X +C Y +B Z +A Z +C Y +A Y +A X +B Z +B Y +C X +B Z +C X +A Z +C X +B Z +A X +C Y +C X +A Z +B Z +C Y +A Z +C Z +C X +A Y +B Z +C X +C X +B Y +A X +C Y +C X +C X +C Z +C X +A Y +C X +C X +A Y +B Z +B X +C X +B Z +C Z +C X +C Y +C Y +C X +B Z +A Y +C Y +C Y +B X +C X +B Z +B Y +B X +C X +C Z +B Z +B Z +B Z +C X +B Z +A Z +C X +C Y +C X +C Z +A Y +C Y +C Y +B Z +B Z +A Z +C Y +C Y +C X +B X +C X +C X +B X +C Z +A Z +A Y +C X +A Y +B Y +A X +C X +C X +B Z +B Y +C X +A Y +B Z +C Y +C X +C X +C X +B Z +B Y +B Y +C Z +B X +C X +C Z +C Z +C X +C X +C Z +C Z +B Z +C X +B Z +B Z +A X +A Z +B Y +B Y +A Y +A Z +B Z +C X +C Y +C X +A X +C X +A Y +A Z +A X +C Z +B Y +B Y +B X +C Z +B Y +C Y +B Y +C X +C Y +B X +A Y +C X +C X +B Z +C Z +A Z +C X +C Z +C X +A Z +B Z +B Y +C X +C X +B Y +B Y +C X +C Y +C Y +B Z +A Y +A Z +A Z +C Y +C X +C X +C X +C X +A X +A Z +A Z +B Y +B X +C Z +C Y +B Y +B Z +C X +B Y +B Z +B X +A Y +C Y +B Z +B X +C X +B Z +C Y +A X +C Z +C X +C Y +B Y +C X +C Y +C X +C X +B Y +B Y +B Z +C X +B Z +A X +C Y +B Y +C X +C X +B Z +B Y +B Y +C X +C Y +B Y +C Y +C X +C X +A Z +C Z +C Y +B X +B Z +B Z +B Z +A Y +B Y +C X +B Y +B X +C X +C X +A Y +C Z +A X +B Z +C X +C X +B Z +A Z +A Z +B Z +C X +C Y +A Z +B Z +C Z +C Z +C X +C X +C Y +C X +C X +B Z +A X +C Y +C Y +B Z +C Y +B Y +C Y +C X +C Y +B Y +C X +C Z +C X +C Z +C Y +C X +C X +A Z +B Y +C X +C X +B Z +C Z +B Z +C X +C X +C Z +C X +C X +B X +C X +B Z +B X +B Z +B Z +B Z +C X +B Y +B Z +C Y +C X +B Z +B Z +A Z +C X +B Z +A Z +B Z +B Z +B Z +C X +C X +C Y +C X +C X +B Z +B Y +B X +B X +C X +C X +B Z +B Y +C X +C X +B Z +C X +A Y +C X +C X +C X +B Z +C Y +C Y +B Y +B Y +B Y +A Y +B X +B Z +B Y +A Z +B X +C X +B Z +B Z +C Y +B Z +A X +B Z +C Y +A X +B X +A Z +B Y +B Z +C X +B Y +C X +B Z +B Z +A Z +B Y +C Y +C Y +C X +C Z +B Z +C X +C Y +A Z +B X +C X +A Z +C Y +C X +C Y +C X +B Z +B Z +C X +B Y +B Z +C X +C X +B Y +A Y +C X +C Z +C X +C X +C Y +C X +C X +C X +B Z +C X +B Y +C X +C Y +C X +C Y +A Z +B Z +A Z +C Y +C X +C X +B Y +A Z +C X +B X +C X +C Z +A Z +B Z +B Z +C Y +C Y +B Z +C X +C Z +A X +C X +B Y +B Z +B Y +B Y +C Z +A X +C Z +A X +B Z +B X +C X +B Z +B Z +A X +B Y +B Z +A X +C X +B Z +B Y +C X +C X +B Y +C Y +A Y +C Y +B X +B X +C X +C X +C Y +B Z +B Y +B Z +C X +B Y +B Y +B Z +B Y +B Y +C Y +C Z +C X +B Z +C X +B X +A Z +C X +C X +A Y +C Z +B X +C Y +B Y +C Z +B Z +C X +B Z +C X +C X +C Y +C Y +B Z +C X +C Z +C X +B Z +B Z +C X +B Z +C Y +C X +C X +A X +C X +B X +A Y +C Z +C Y +B Y +B X +C X +C Z +B Z +C Y +C Z +C X +C X +B X +A Z +B Y +C Z +A X +C X +C X +A Y +C Z +B X +B Z +C X +B Z +C Y +C Z +B Y +B X +C X +A Y +A X +C X +C X +B Z +C X +C X +C X +B Z +C X +A Z +B Y +C Z +C X +A Z +C X +C Z +B Z +B X +B X +B Y +C Y +A X +A X +A Z +C Z +C Y +A X +C X +A Z +B X +B Z +C X +B Y +B Y +B X +B X +B Y +B Y +B Z +C X +C Z +C Y +B Y +B Z +A Y +A Y +C X +B Z +C X +C X +B X +C X +B Z +B X +C Y +C X +C X +B Z +C Y +B X +A Z +C Y +C Y +C X +C X +B X +B Y +C X +B Z +C Y +B Y +B Z +A Z +C X +B Z +C X +B Z +C X +C X +C X +A Y +B X +A Z +C Y +C Z +B Y +B Y +A Y +B Y +B Z +B X +C X +B Y +B Y +B Z +A X +C Y +B Z +C Y +A Y +C X +B Z +A Z +B Z +B Y +B Y +B Y +B Z +B X +B Z +A Y +C Y +B Y +C X +C Y +B Y +B Z +C Y +C X +B Y +B Z +B Y +C Y +B Y +A X +C X +C X +B Z +B Z +B Y +B X +A X +B Y +A Z +A X +C Z +B Z +C X +C X +A Z +B Z +B Z +A Z +B Y +C X +C X +B X +C X +C Y +C X +B Z +A X +C X +B Y +C Z +A Y +B Z +C Y +A Y +B Y +B Y +B Z +B Z +C X +B Z +A X +C X +B Z +C X +C X +C X +A X +B X +A Z +B Z +A X +B X +C X +B Z +A Z +B Z +A Z +C X +C X +C X +C Y +B X +B Z +B Z +B Y +A Y +C Z +A X +B Y +C Y +B Z +B Z +B Z +B Z +C Y +B Y +B Z +B Z +C X +A Z +B Y +A Y +B Y +C Z +B X +C Y +B Z +C Y +C X +B Y +B Z +C Z +A Z +C Y +B Z +A X +C X +B Y +B Z +B X +C X +B Z +C Y +C X +C Z +B Y +C X +B Y +B Y +A X +C X +B Y +B X +C Z +C X +B Z +B Y +C Y +B Z +C Y +C Y +C Z +B Z +C Z +A Y +B X +B Z +C X +B Z +B X +B X +C Z +C Y +C Z +C X +B Y +C X +A X +B Y +B Y +C Y +C X +A Z +B Z +B Z +A Y +B Z +A X +C X +A Z +C X +B Y +C X +C X +A Y +A Z +B Z +A X +A X +B X +C X +C Z +B Y +A Y +B Y +A Z +B Z +A Y +B Z +C X +B Z +C X +C X +A Y +C X +C X +B Y +A Z +B Y +B Z +B Z +B Z +B Z +B Y +B Z +B Z +C X +B Z +C X +C Y +C Y +B Y +C Y +C X +A Z +B Y +A Z +B Y +C X +B Y +B X +B Z +B Y +B Y +B Z +C Z +B Z +B X +C X +B Z +C X +C X +A X +C X +B Y +B X +C Y +C Y +C Z +B Z +A Z +C X +B Z +B X +A Z +C X +C Y +C X +C X +A X +B Z +B Z +A Y +C X +B Y +B X +A Y +B Y +C X +C Y +C X +B Y +B Z +C X +B Y +C X +C X +C X +C Y +B Z +B Z +A Y +B Y +A Z +B Z +C X +B X +A Y +C Y +C Z +A Z +C Y +C X +B X +A Z +A Z +B Y +C X +B Z +B Z +C Y +B Z +B Z +C X +B Z +C X +A Z +C Y +B X +C Y +A X +B Y +B Z +A X +C Y +C X +C Y +C X +B Z +C X +C X +A Y +B Z +B Y +B Z +C Y +A Z +B Z +C X +B Y +C X +C X +C Y +C Z +B Z +B X +C X +C X +B Y +C X +C Y +A X +C Y +C X +C X +A Y +B Y +B Y +A X +C Y +B Z +B Z +C Z +B Z +B X +B Y +B X +B X +C X +C Y +A Z +C Z +C Y +B Z +B Y +A Y +C X +B Z +A X +C Y +A Z +C X +B Y +C Y +A X +A Z +A Z +B X +B Y +B Y +C X +B X +B Z +B Z +B Y +B X +B X +C Y +C Y +B Y +C Y +C X +B Y +B X +A Z +C Z +C X +B X +B Y +C Y +C Y +A X +C X +A Z +C X +A Y +B Y +C Y +A Z +B Y +B Y +A X +B Z +B Y +B X +A Y +B Z +C Z +C X +A Z +C Y +A X +A Z +C X +C Y +B Z +C X +B X +C Y +C X +B Z +C X +C X +B Y +B Y +C X +C X +B Y +C X +C Y +B Y +B X +C X +B Y +C Y +A Y +B Y +B Y +B Z +B Y +B X +A Y +C Y +C X +C Z +A Z +C X +A Z +C X +A Y +B Z +C Z +C Y +B Z +B Z +A Z +A X +C X +C Y +C X +C Z +B Z +A X +A Y +B Z +B Z +A X +C X +C Y +B X +C X +C Z +A X +B Y +A X +C Z +B Z +C X +C X +C X +B X +C Y +B Z +A X +B Z +A X +C Y +C X +A Y +C X +A Z +B Y +C X +C Y +B Z +C X +B Y +B Z +B Y +B Y +C X +C X +B Y +C X +C Y +C X +C Y +A Z +C X +C Y +B X +A Z +B Y +C X +C X +C Z +B Z +B Z +B X +C Z +B Z +B Z +C X +C X +C X +B X +B Z +B Z +C X +C X +C X +A X +C X +C Y +A Y +B X +A X +A Z +A Y +C X +B X +C Y +C X +C Y +C X +A X +B Z +A X +A X +B Z +C X +B Y +C X +B Y +B Z +C X +C Y +C Z +C X +C X +C X +C X +B Y +C Z +A Z +B Z +B Z +C X +C Z +B X +B Z +A Z +B Z +C Z +C X +C Y +B X +C Y +C Y +C X +B X +B X +C Y +C X +B Y +C Y +C X +B Z +B Z +A Y +A Z +C X +B Z +C X +A Z +B Z +C X +C X +C X +B Z +C X +B Y +B Z +A X +B Y +B Z +B X +C Y +A Z +B Z +C Y +C Y +C X +B Z +C X +C X +C X +C Y +A Z +C Z +B X +B Y +B Y +B Z +C Y +B Z +B Z +C Y +C X +B X +C Y diff --git a/src/problem_02/mod.rs b/src/problem_02/mod.rs index 8b43c01..f695ecb 100644 --- a/src/problem_02/mod.rs +++ b/src/problem_02/mod.rs @@ -1,9 +1,102 @@ -// static INPUT: &'static str = include_str!("input.txt"); +mod types; +use types::*; + +static INPUT: &str = include_str!("input.txt"); + +fn decode_rps(c: char) -> RpsHand { + match c { + 'A' => RpsHand::Rock, + 'X' => RpsHand::Rock, + 'B' => RpsHand::Paper, + 'Y' => RpsHand::Paper, + 'C' => RpsHand::Scissors, + 'Z' => RpsHand::Scissors, + _ => panic!("invalid input"), + } +} + +fn decode_goal(c: char) -> Goal { + match c { + 'X' => Goal::Lose, + 'Y' => Goal::Draw, + 'Z' => Goal::Win, + _ => panic!("invalid input"), + } +} + +fn decode_plan(g: Goal, h: RpsHand) -> RpsHand { + match g { + Goal::Lose => h.wins_against(), + Goal::Draw => h.ties_with(), + Goal::Win => h.loses_to(), + } +} + +fn process_input(input: &str, conv_func: fn(a: char, b: char) -> Game) -> Vec { + let mut games = vec![]; + for line in input.lines() { + if !line.is_empty() { + if line.len() != 3 { + panic!("invalid input") + } else { + let a = line.chars().next().expect("invalid input"); + let b = line.chars().nth(2).expect("invalid input"); + games.push(conv_func(a, b)); + } + } + } + games +} + +fn conv_game(a: char, b: char) -> Game { + Game { + opponent: decode_rps(a), + mine: decode_rps(b), + } +} + +fn conv_plan(a: char, b: char) -> Game { + let opp = decode_rps(a); + let goal = decode_goal(b); + Game { + opponent: opp, + mine: decode_plan(goal, opp), + } +} + +fn score_shape(s: &RpsHand) -> i32 { + match s { + RpsHand::Rock => 1, + RpsHand::Paper => 2, + RpsHand::Scissors => 3, + } +} + +fn score_outcome(g: &Game) -> i32 { + match (&g.mine, &g.opponent) { + (RpsHand::Rock, RpsHand::Scissors) => 6, + (RpsHand::Paper, RpsHand::Rock) => 6, + (RpsHand::Scissors, RpsHand::Paper) => 6, + _ => { + if g.mine == g.opponent { + 3 + } else { + 0 + } + } + } +} + +fn score_game(g: &Game) -> i32 { + score_shape(&g.mine) + score_outcome(g) +} pub fn solution_a() -> i32 { - 0 + let games = process_input(INPUT, conv_game); + games.iter().map(score_game).sum() } pub fn solution_b() -> i32 { - 0 + let games = process_input(INPUT, conv_plan); + games.iter().map(score_game).sum() } diff --git a/src/problem_02/types.rs b/src/problem_02/types.rs new file mode 100644 index 0000000..53d5a09 --- /dev/null +++ b/src/problem_02/types.rs @@ -0,0 +1,38 @@ +#[derive(Eq, PartialEq, Clone, Copy)] +pub enum RpsHand { + Paper, + Scissors, + Rock, +} + +impl RpsHand { + pub fn loses_to(&self) -> RpsHand { + match *self { + RpsHand::Paper => RpsHand::Scissors, + RpsHand::Scissors => RpsHand::Rock, + RpsHand::Rock => RpsHand::Paper, + } + } + pub fn wins_against(&self) -> RpsHand { + match *self { + RpsHand::Paper => RpsHand::Rock, + RpsHand::Scissors => RpsHand::Paper, + RpsHand::Rock => RpsHand::Scissors, + } + } + pub fn ties_with(&self) -> RpsHand { + *self + } +} + +pub enum Goal { + Lose, + Draw, + Win, +} + +#[derive(Eq, PartialEq)] +pub struct Game { + pub opponent: RpsHand, + pub mine: RpsHand, +} From 917b377f98eb9210fee71e8be0d8daecc8d95ccc Mon Sep 17 00:00:00 2001 From: Thorn Avery Date: Tue, 6 Dec 2022 10:38:24 +1100 Subject: [PATCH 05/10] day 3 done --- src/lib.rs | 1 + src/main.rs | 4 + src/problem_03/description.txt | 67 ++++++++ src/problem_03/input.txt | 300 +++++++++++++++++++++++++++++++++ src/problem_03/mod.rs | 91 ++++++++++ src/problem_03/types.rs | 38 +++++ 6 files changed, 501 insertions(+) create mode 100644 src/problem_03/description.txt create mode 100644 src/problem_03/input.txt create mode 100644 src/problem_03/mod.rs create mode 100644 src/problem_03/types.rs diff --git a/src/lib.rs b/src/lib.rs index e62cb92..8c4cf21 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,2 +1,3 @@ pub mod problem_01; pub mod problem_02; +pub mod problem_03; diff --git a/src/main.rs b/src/main.rs index 58dc661..2cc416a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,5 +1,6 @@ use taoc2022::problem_01; use taoc2022::problem_02; +use taoc2022::problem_03; fn main() { println!("== Problem 01 =="); @@ -8,4 +9,7 @@ fn main() { println!("== Problem 02 =="); println!("= a: {}", problem_02::solution_a()); println!("= b: {}", problem_02::solution_b()); + println!("== Problem 03 =="); + println!("= a: {}", problem_03::solution_a()); + println!("= b: {}", problem_03::solution_b()); } diff --git a/src/problem_03/description.txt b/src/problem_03/description.txt new file mode 100644 index 0000000..689febc --- /dev/null +++ b/src/problem_03/description.txt @@ -0,0 +1,67 @@ +--- Day 3: Rucksack Reorganization --- + +One Elf has the important job of loading all of the rucksacks with supplies for the jungle journey. Unfortunately, that Elf didn't quite follow the packing instructions, and so a few items now need to be rearranged. + +Each rucksack has two large compartments. All items of a given type are meant to go into exactly one of the two compartments. The Elf that did the packing failed to follow this rule for exactly one item type per rucksack. + +The Elves have made a list of all of the items currently in each rucksack (your puzzle input), but they need your help finding the errors. Every item type is identified by a single lowercase or uppercase letter (that is, a and A refer to different types of items). + +The list of items for each rucksack is given as characters all on a single line. A given rucksack always has the same number of items in each of its two compartments, so the first half of the characters represent items in the first compartment, while the second half of the characters represent items in the second compartment. + +For example, suppose you have the following list of contents from six rucksacks: + +vJrwpWtwJgWrhcsFMMfFFhFp +jqHRNqRjqzjGDLGLrsFMfFZSrLrFZsSL +PmmdzqPrVvPwwTWBwg +wMqvLMZHhHMvwLHjbvcjnnSBnvTQFn +ttgJtRGJQctTZtZT +CrZsJsPPZsGzwwsLwLmpwMDw + + The first rucksack contains the items vJrwpWtwJgWrhcsFMMfFFhFp, which means its first compartment contains the items vJrwpWtwJgWr, while the second compartment contains the items hcsFMMfFFhFp. The only item type that appears in both compartments is lowercase p. + The second rucksack's compartments contain jqHRNqRjqzjGDLGL and rsFMfFZSrLrFZsSL. The only item type that appears in both compartments is uppercase L. + The third rucksack's compartments contain PmmdzqPrV and vPwwTWBwg; the only common item type is uppercase P. + The fourth rucksack's compartments only share item type v. + The fifth rucksack's compartments only share item type t. + The sixth rucksack's compartments only share item type s. + +To help prioritize item rearrangement, every item type can be converted to a priority: + + Lowercase item types a through z have priorities 1 through 26. + Uppercase item types A through Z have priorities 27 through 52. + +In the above example, the priority of the item type that appears in both compartments of each rucksack is 16 (p), 38 (L), 42 (P), 22 (v), 20 (t), and 19 (s); the sum of these is 157. + +Find the item type that appears in both compartments of each rucksack. What is the sum of the priorities of those item types? + +Your puzzle answer was 8153. +--- Part Two --- + +As you finish identifying the misplaced items, the Elves come to you with another issue. + +For safety, the Elves are divided into groups of three. Every Elf carries a badge that identifies their group. For efficiency, within each group of three Elves, the badge is the only item type carried by all three Elves. That is, if a group's badge is item type B, then all three Elves will have item type B somewhere in their rucksack, and at most two of the Elves will be carrying any other item type. + +The problem is that someone forgot to put this year's updated authenticity sticker on the badges. All of the badges need to be pulled out of the rucksacks so the new authenticity stickers can be attached. + +Additionally, nobody wrote down which item type corresponds to each group's badges. The only way to tell which item type is the right one is by finding the one item type that is common between all three Elves in each group. + +Every set of three lines in your list corresponds to a single group, but each group can have a different badge item type. So, in the above example, the first group's rucksacks are the first three lines: + +vJrwpWtwJgWrhcsFMMfFFhFp +jqHRNqRjqzjGDLGLrsFMfFZSrLrFZsSL +PmmdzqPrVvPwwTWBwg + +And the second group's rucksacks are the next three lines: + +wMqvLMZHhHMvwLHjbvcjnnSBnvTQFn +ttgJtRGJQctTZtZT +CrZsJsPPZsGzwwsLwLmpwMDw + +In the first group, the only item type that appears in all three rucksacks is lowercase r; this must be their badges. In the second group, their badge item type must be Z. + +Priorities for these items must still be found to organize the sticker attachment efforts: here, they are 18 (r) for the first group and 52 (Z) for the second group. The sum of these is 70. + +Find the item type that corresponds to the badges of each three-Elf group. What is the sum of the priorities of those item types? + +Your puzzle answer was 2342. + +Both parts of this puzzle are complete! They provide two gold stars: ** diff --git a/src/problem_03/input.txt b/src/problem_03/input.txt new file mode 100644 index 0000000..0b6d449 --- /dev/null +++ b/src/problem_03/input.txt @@ -0,0 +1,300 @@ +VdzVHmNpdVmBBCpmQLTNfTtMhMJnhFhTTf +FgqsZbqDDFqRrhhJnsnLMTfhJG +bRRRPrRRwSwbDqgjvDZbRbQzpzmQVWCzzBdvQBFCzlWV +GcDdRdvhRssRhGDdShCRtqWjlQzqWgqzNfNjfQWWjt +mwwnnPFwmVrPmJmzfNzqCjQCbgVlgC +nPnHHLrHwmJTrCTJpThBscBSdSLGZvZBvRhZ +RVQQcVlcSRclfZCCCnMJJTSTnC +NdHwjdwjbBBZrrZrbJDZJJ +wmhjGGBGwwmjtjtdPlfRcpVQlhRppVJF +pplbNBPPrppllrFNbpvppSTcwqcWFhTTShhJDTchqd +RGzRfLjjmZmfmwLftTWhStStJWTdWmDm +nfsMjQssnpPvNnrPrw +SjjBgllzlQjBZvlBBgcFbgJHsMhJqbMHPggJbM +hRLRVDdRRWnJqnnHTqMCnH +GRfLddRRpVhNVrWSjwQQzSzcGSBQSc +qMwNqqBdQdnTVBBVVhMVnVFzTHPggTPjGRDzvPTjjmvPDj +sbSrWJpStrtPtRPttzmmDD +pfbJJcbsrcLpWLllsnFmcqcwQncnQQqVNQ +RBTWCMwCwdZThPZcZZ +pVmVpHLFFFHHVgVmvNmHSQNvddlPPzZJMPcdhclhjczLdZMP +vnnNnFStGMRDwWnn +fWDdJTpDJzdBBBdmDSbSRHRwPqbPbHgSbz +slQtQvNsMVvrrgPRgRglnhwWPH +sGMMQFrsjvNMfWmdpfFDFZBf +vnMRMWCMJwWWwwWPjmSdVmLdzvVbLrhL +HsNfDHQlZpNqfQzbLbrqhjLmVdjd +dfDZQsNpstHHHptZDDtZWgngtgBMPMMRwCPtBBGW +HwQwwbwFNWHwHBVFQFLQzRznZnSzcjjjpPbcPpSP +vTfTJsCmsftJZmTSSdPvzdjRSvPdjd +TrGtTJfmGDfD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gSCtC +WmmFBmJrcFRBFrJJBFchzWCStgCTgvhvTlfsNqfTlvTv +sFzzssDLzrBPjDVVddHMQD +fztDZSGrNrlnbnPTgFFpln +jvvQMMcLcjJmQwHdJvjQJnnbTbRFRphnnpsWgmFRPR +vTLHHCQLHBBjJCSZrVCZtSfSfrft +gHfHffHLjwHrRjLrLRZVMnTdTBsNTBwTVBsBnN +DWPhqhhDhvSGvWPzSzMBQBQVMMBBmvssvQvQ +CDGbqCDbChSbWGrHcHRgbcVcfrLJ +frlTLmtllbbbdpJS +qFjhzjThjHTFGHTjqhhjMzBhVpVpdbBnSJQRpBnVVdbRRQJd +vjWPWjWPPPWgwmfCrNvTvZ diff --git a/src/problem_03/mod.rs b/src/problem_03/mod.rs new file mode 100644 index 0000000..f92dfe8 --- /dev/null +++ b/src/problem_03/mod.rs @@ -0,0 +1,91 @@ +use std::cmp::Ordering; + +static INPUT: &str = include_str!("input.txt"); + +fn item_priority(c: char) -> u8 { + if !c.is_ascii_alphabetic() { + panic!("invalid item in rucksack: {}", c); + } + match c { + 'a'..='z' => (c as u8) - b'a' + 1, + _ => (c as u8) - b'A' + 27, + } +} + +fn split_input_line(l: String) -> (Vec, Vec) { + let n = l.len(); + if n % 2 != 0 { + panic!("invalid line length"); + } + let s = n / 2; + let mut c1 = l[0..s].chars().collect::>(); + c1.sort(); + c1.dedup(); + let mut c2 = l[s..n].chars().collect::>(); + c2.sort(); + c2.dedup(); + (c1, c2) +} + +fn parse_input_line(l: String) -> Vec { + let mut c1 = l.chars().collect::>(); + c1.sort(); + c1.dedup(); + c1 +} + +fn find_items(c1: Vec, c2: Vec) -> Vec { + let mut results = vec![]; + let mut i1 = c1.iter().peekable(); + let mut i2 = c2.iter().peekable(); + while let (Some(a), Some(b)) = (i1.peek(), i2.peek()) { + match a.cmp(b) { + Ordering::Equal => { + results.push(**a); + i1.next(); + i2.next(); + } + Ordering::Less => { + i1.next(); + } + Ordering::Greater => { + i2.next(); + } + } + } + results +} + +pub fn solution_a() -> i32 { + let input = INPUT.lines().map(String::from).map(split_input_line); + input + .into_iter() + .map(|(a, b)| find_items(a, b)) + .map(|x| match x.first() { + Some(y) => item_priority(*y) as i32, + _ => panic!("no matches found"), + }) + .sum() +} + +pub fn solution_b() -> i32 { + let mut total = 0; + let mut input = INPUT.lines().map(String::from).map(parse_input_line); + loop { + let l1 = input.next(); + let l2 = input.next(); + let l3 = input.next(); + match (l1, l2, l3) { + (Some(s1), Some(s2), Some(s3)) => { + let intermediate_matches = find_items(s1, s2); + let final_matches = find_items(s3, intermediate_matches); + match final_matches.first() { + Some(x) => total += item_priority(*x) as i32, + _ => panic!("no matches found"), + } + } + _ => break, + } + } + total +} diff --git a/src/problem_03/types.rs b/src/problem_03/types.rs new file mode 100644 index 0000000..53d5a09 --- /dev/null +++ b/src/problem_03/types.rs @@ -0,0 +1,38 @@ +#[derive(Eq, PartialEq, Clone, Copy)] +pub enum RpsHand { + Paper, + Scissors, + Rock, +} + +impl RpsHand { + pub fn loses_to(&self) -> RpsHand { + match *self { + RpsHand::Paper => RpsHand::Scissors, + RpsHand::Scissors => RpsHand::Rock, + RpsHand::Rock => RpsHand::Paper, + } + } + pub fn wins_against(&self) -> RpsHand { + match *self { + RpsHand::Paper => RpsHand::Rock, + RpsHand::Scissors => RpsHand::Paper, + RpsHand::Rock => RpsHand::Scissors, + } + } + pub fn ties_with(&self) -> RpsHand { + *self + } +} + +pub enum Goal { + Lose, + Draw, + Win, +} + +#[derive(Eq, PartialEq)] +pub struct Game { + pub opponent: RpsHand, + pub mine: RpsHand, +} From 52015d047927dbf78027855c38650f83f49a0a63 Mon Sep 17 00:00:00 2001 From: Thorn Avery Date: Tue, 6 Dec 2022 10:38:54 +1100 Subject: [PATCH 06/10] whoops forgot a stray file --- src/problem_03/types.rs | 38 -------------------------------------- 1 file changed, 38 deletions(-) delete mode 100644 src/problem_03/types.rs diff --git a/src/problem_03/types.rs b/src/problem_03/types.rs deleted file mode 100644 index 53d5a09..0000000 --- a/src/problem_03/types.rs +++ /dev/null @@ -1,38 +0,0 @@ -#[derive(Eq, PartialEq, Clone, Copy)] -pub enum RpsHand { - Paper, - Scissors, - Rock, -} - -impl RpsHand { - pub fn loses_to(&self) -> RpsHand { - match *self { - RpsHand::Paper => RpsHand::Scissors, - RpsHand::Scissors => RpsHand::Rock, - RpsHand::Rock => RpsHand::Paper, - } - } - pub fn wins_against(&self) -> RpsHand { - match *self { - RpsHand::Paper => RpsHand::Rock, - RpsHand::Scissors => RpsHand::Paper, - RpsHand::Rock => RpsHand::Scissors, - } - } - pub fn ties_with(&self) -> RpsHand { - *self - } -} - -pub enum Goal { - Lose, - Draw, - Win, -} - -#[derive(Eq, PartialEq)] -pub struct Game { - pub opponent: RpsHand, - pub mine: RpsHand, -} From 083cdabb2f127d4e91c1779ee171bb6b21af92f9 Mon Sep 17 00:00:00 2001 From: Thorn Avery Date: Tue, 6 Dec 2022 11:29:16 +1100 Subject: [PATCH 07/10] day 4 done --- Cargo.lock | 316 ---------- Cargo.toml | 1 - src/lib.rs | 1 + src/main.rs | 4 + src/problem_04/description.txt | 64 ++ src/problem_04/input.txt | 1000 ++++++++++++++++++++++++++++++++ src/problem_04/mod.rs | 60 ++ 7 files changed, 1129 insertions(+), 317 deletions(-) create mode 100644 src/problem_04/description.txt create mode 100644 src/problem_04/input.txt create mode 100644 src/problem_04/mod.rs diff --git a/Cargo.lock b/Cargo.lock index 06f16a2..b8ea136 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2,322 +2,6 @@ # It is not intended for manual editing. version = 3 -[[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.77" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e9f73505338f7d905b19d18738976aae232eb46b8efc15554ffc56deb5d9ebe4" - -[[package]] -name = "clap" -version = "4.0.29" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4d63b9e9c07271b9957ad22c173bae2a4d9a81127680962039296abcd2f8251d" -dependencies = [ - "bitflags", - "clap_derive", - "clap_lex", - "is-terminal", - "once_cell", - "strsim", - "termcolor", -] - -[[package]] -name = "clap_derive" -version = "4.0.21" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0177313f9f02afc995627906bbd8967e2be069f5261954222dac78290c2b9014" -dependencies = [ - "heck", - "proc-macro-error", - "proc-macro2", - "quote", - "syn", -] - -[[package]] -name = "clap_lex" -version = "0.3.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0d4198f73e42b4936b35b5bb248d81d2b595ecb170da0bac7655c54eedfa8da8" -dependencies = [ - "os_str_bytes", -] - -[[package]] -name = "errno" -version = "0.2.8" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f639046355ee4f37944e44f60642c6f3a7efa3cf6b78c78a0d989a8ce6c396a1" -dependencies = [ - "errno-dragonfly", - "libc", - "winapi", -] - -[[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.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2540771e65fc8cb83cd6e8a237f70c319bd5c29f78ed1084ba5d50eeac86f7f9" - -[[package]] -name = "hermit-abi" -version = "0.2.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ee512640fe35acbfb4bb779db6f0d80704c2cacfa2e39b601ef3e3f47d1ae4c7" -dependencies = [ - "libc", -] - -[[package]] -name = "io-lifetimes" -version = "1.0.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "46112a93252b123d31a119a8d1a1ac19deac4fac6e0e8b0df58f0d4e5870e63c" -dependencies = [ - "libc", - "windows-sys", -] - -[[package]] -name = "is-terminal" -version = "0.4.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "927609f78c2913a6f6ac3c27a4fe87f43e2a35367c0c4b0f8265e8f49a104330" -dependencies = [ - "hermit-abi", - "io-lifetimes", - "rustix", - "windows-sys", -] - -[[package]] -name = "libc" -version = "0.2.137" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fc7fcc620a3bff7cdd7a365be3376c97191aeaccc2a603e600951e452615bf89" - -[[package]] -name = "linux-raw-sys" -version = "0.1.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8f9f08d8963a6c613f4b1a78f4f4a4dbfadf8e6545b2d72861731e4858b8b47f" - -[[package]] -name = "once_cell" -version = "1.16.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "86f0b0d4bf799edbc74508c1e8bf170ff5f41238e5f8225603ca7caaae2b7860" - -[[package]] -name = "os_str_bytes" -version = "6.4.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9b7820b9daea5457c9f21c69448905d723fbd21136ccf521748f23fd49e723ee" - -[[package]] -name = "proc-macro-error" -version = "1.0.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "da25490ff9892aab3fcf7c36f08cfb902dd3e71ca0f9f9517bea02a73a5ce38c" -dependencies = [ - "proc-macro-error-attr", - "proc-macro2", - "quote", - "syn", - "version_check", -] - -[[package]] -name = "proc-macro-error-attr" -version = "1.0.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a1be40180e52ecc98ad80b184934baf3d0d29f979574e439af5a55274b35f869" -dependencies = [ - "proc-macro2", - "quote", - "version_check", -] - -[[package]] -name = "proc-macro2" -version = "1.0.47" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5ea3d908b0e36316caf9e9e2c4625cdde190a7e6f440d794667ed17a1855e725" -dependencies = [ - "unicode-ident", -] - -[[package]] -name = "quote" -version = "1.0.21" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bbe448f377a7d6961e30f5955f9b8d106c3f5e449d493ee1b125c1d43c2b5179" -dependencies = [ - "proc-macro2", -] - -[[package]] -name = "rustix" -version = "0.36.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cb93e85278e08bb5788653183213d3a60fc242b10cb9be96586f5a73dcb67c23" -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 = "1.0.105" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "60b9b43d45702de4c839cb9b51d9f529c5dd26a4aff255b42b1ebc03e88ee908" -dependencies = [ - "proc-macro2", - "quote", - "unicode-ident", -] - [[package]] name = "taoc2022" version = "0.1.0" -dependencies = [ - "clap", -] - -[[package]] -name = "termcolor" -version = "1.1.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bab24d30b911b2376f3a13cc2cd443142f0c81dda04c118693e35b3835757755" -dependencies = [ - "winapi-util", -] - -[[package]] -name = "unicode-ident" -version = "1.0.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6ceab39d59e4c9499d4e5a8ee0e2735b891bb7308ac83dfb4e80cad195c9f6f3" - -[[package]] -name = "version_check" -version = "0.9.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f" - -[[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-util" -version = "0.1.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "70ec6ce85bb158151cae5e5c87f95a8e97d2c0c4b001223f33a334e3ce5de178" -dependencies = [ - "winapi", -] - -[[package]] -name = "winapi-x86_64-pc-windows-gnu" -version = "0.4.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" - -[[package]] -name = "windows-sys" -version = "0.42.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5a3e1820f08b8513f676f7ab6c1f99ff312fb97b553d30ff4dd86f9f15728aa7" -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.42.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "41d2aa71f6f0cbe00ae5167d90ef3cfe66527d6f613ca78ac8024c3ccab9a19e" - -[[package]] -name = "windows_aarch64_msvc" -version = "0.42.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "dd0f252f5a35cac83d6311b2e795981f5ee6e67eb1f9a7f64eb4500fbc4dcdb4" - -[[package]] -name = "windows_i686_gnu" -version = "0.42.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fbeae19f6716841636c28d695375df17562ca208b2b7d0dc47635a50ae6c5de7" - -[[package]] -name = "windows_i686_msvc" -version = "0.42.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "84c12f65daa39dd2babe6e442988fc329d6243fdce47d7d2d155b8d874862246" - -[[package]] -name = "windows_x86_64_gnu" -version = "0.42.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bf7b1b21b5362cbc318f686150e5bcea75ecedc74dd157d874d754a2ca44b0ed" - -[[package]] -name = "windows_x86_64_gnullvm" -version = "0.42.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "09d525d2ba30eeb3297665bd434a54297e4170c7f1a44cad4ef58095b4cd2028" - -[[package]] -name = "windows_x86_64_msvc" -version = "0.42.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f40009d85759725a34da6d89a94e63d7bdc50a862acf0dbc7c8e488f1edcb6f5" diff --git a/Cargo.toml b/Cargo.toml index 54d04fd..fe00db4 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -4,4 +4,3 @@ version = "0.1.0" edition = "2021" [dependencies] -clap = { version = "4.0", features = ["derive"] } diff --git a/src/lib.rs b/src/lib.rs index 8c4cf21..947f87f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,3 +1,4 @@ pub mod problem_01; pub mod problem_02; pub mod problem_03; +pub mod problem_04; diff --git a/src/main.rs b/src/main.rs index 2cc416a..88fe459 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,6 +1,7 @@ use taoc2022::problem_01; use taoc2022::problem_02; use taoc2022::problem_03; +use taoc2022::problem_04; fn main() { println!("== Problem 01 =="); @@ -12,4 +13,7 @@ fn main() { println!("== Problem 03 =="); println!("= a: {}", problem_03::solution_a()); println!("= b: {}", problem_03::solution_b()); + println!("== Problem 04 =="); + println!("= a: {}", problem_04::solution_a()); + println!("= b: {}", problem_04::solution_b()); } diff --git a/src/problem_04/description.txt b/src/problem_04/description.txt new file mode 100644 index 0000000..1ac9283 --- /dev/null +++ b/src/problem_04/description.txt @@ -0,0 +1,64 @@ +--- Day 4: Camp Cleanup --- + +Space needs to be cleared before the last supplies can be unloaded from the ships, and so several Elves have been assigned the job of cleaning up sections of the camp. Every section has a unique ID number, and each Elf is assigned a range of section IDs. + +However, as some of the Elves compare their section assignments with each other, they've noticed that many of the assignments overlap. To try to quickly find overlaps and reduce duplicated effort, the Elves pair up and make a big list of the section assignments for each pair (your puzzle input). + +For example, consider the following list of section assignment pairs: + +2-4,6-8 +2-3,4-5 +5-7,7-9 +2-8,3-7 +6-6,4-6 +2-6,4-8 + +For the first few pairs, this list means: + + Within the first pair of Elves, the first Elf was assigned sections 2-4 (sections 2, 3, and 4), while the second Elf was assigned sections 6-8 (sections 6, 7, 8). + The Elves in the second pair were each assigned two sections. + The Elves in the third pair were each assigned three sections: one got sections 5, 6, and 7, while the other also got 7, plus 8 and 9. + +This example list uses single-digit section IDs to make it easier to draw; your actual list might contain larger numbers. Visually, these pairs of section assignments look like this: + +.234..... 2-4 +.....678. 6-8 + +.23...... 2-3 +...45.... 4-5 + +....567.. 5-7 +......789 7-9 + +.2345678. 2-8 +..34567.. 3-7 + +.....6... 6-6 +...456... 4-6 + +.23456... 2-6 +...45678. 4-8 + +Some of the pairs have noticed that one of their assignments fully contains the other. For example, 2-8 fully contains 3-7, and 6-6 is fully contained by 4-6. In pairs where one assignment fully contains the other, one Elf in the pair would be exclusively cleaning sections their partner will already be cleaning, so these seem like the most in need of reconsideration. In this example, there are 2 such pairs. + +In how many assignment pairs does one range fully contain the other? + +Your puzzle answer was 466. +--- Part Two --- + +It seems like there is still quite a bit of duplicate work planned. Instead, the Elves would like to know the number of pairs that overlap at all. + +In the above example, the first two pairs (2-4,6-8 and 2-3,4-5) don't overlap, while the remaining four pairs (5-7,7-9, 2-8,3-7, 6-6,4-6, and 2-6,4-8) do overlap: + + 5-7,7-9 overlaps in a single section, 7. + 2-8,3-7 overlaps all of the sections 3 through 7. + 6-6,4-6 overlaps in a single section, 6. + 2-6,4-8 overlaps in sections 4, 5, and 6. + +So, in this example, the number of overlapping assignment pairs is 4. + +In how many assignment pairs do the ranges overlap? + +Your puzzle answer was 865. + +Both parts of this puzzle are complete! They provide two gold stars: ** diff --git a/src/problem_04/input.txt b/src/problem_04/input.txt new file mode 100644 index 0000000..107893a --- /dev/null +++ b/src/problem_04/input.txt @@ -0,0 +1,1000 @@ +15-60,14-59 +32-80,17-79 +47-80,79-80 +64-64,12-63 +93-93,8-92 +35-41,34-41 +7-52,7-8 +28-95,28-94 +43-99,2-43 +74-77,75-78 +44-98,55-69 +2-52,53-53 +71-73,71-72 +35-49,48-49 +84-85,22-85 +88-90,7-89 +18-85,5-85 +4-4,3-82 +22-79,21-79 +6-6,6-84 +61-84,61-84 +66-95,16-94 +12-46,47-47 +93-93,55-93 +23-95,22-99 +16-16,16-58 +67-81,65-80 +58-66,24-66 +39-46,47-79 +9-9,9-89 +18-89,19-68 +4-89,10-47 +5-79,3-5 +12-90,20-91 +78-83,79-89 +6-80,5-81 +37-98,37-38 +33-98,32-97 +42-78,41-43 +31-97,2-98 +24-72,23-73 +6-88,5-29 +80-93,81-94 +5-85,4-84 +21-81,4-89 +45-84,22-44 +57-80,81-81 +46-76,47-77 +22-32,21-72 +5-29,4-29 +11-78,10-78 +61-62,61-92 +17-24,18-24 +57-93,56-97 +1-98,4-50 +5-5,5-42 +10-92,10-11 +8-86,22-87 +10-52,9-9 +3-13,10-14 +1-46,3-56 +85-85,7-84 +88-89,88-96 +1-93,1-93 +58-71,53-60 +79-96,30-79 +5-56,5-57 +42-98,41-99 +3-97,96-97 +55-65,54-56 +73-93,93-94 +18-98,99-99 +2-57,1-69 +41-42,41-77 +10-51,50-52 +73-73,73-89 +31-91,31-31 +6-27,6-61 +1-1,1-84 +21-56,57-57 +30-31,30-31 +24-72,16-72 +59-65,56-65 +18-18,17-99 +87-87,32-88 +30-30,30-60 +12-95,13-95 +29-66,6-65 +2-52,2-53 +93-93,2-93 +2-98,3-3 +19-90,15-18 +12-83,3-13 +21-99,22-22 +5-54,5-54 +43-73,44-73 +15-26,7-25 +52-94,53-95 +67-68,2-67 +2-92,91-92 +72-91,73-78 +20-87,21-87 +27-56,10-56 +20-88,89-89 +5-90,10-90 +11-20,12-21 +4-86,4-86 +44-46,45-98 +10-11,10-63 +8-9,13-98 +23-46,23-61 +71-87,15-86 +33-40,19-39 +7-93,8-93 +18-18,17-76 +3-58,8-59 +42-94,42-42 +12-86,44-87 +20-93,93-96 +12-54,13-36 +41-77,41-76 +20-20,19-96 +13-91,12-90 +3-98,4-4 +6-67,6-66 +25-61,30-54 +61-61,15-61 +24-81,24-24 +45-69,46-68 +56-66,52-98 +50-67,59-68 +2-97,3-72 +33-79,34-51 +56-92,55-91 +3-69,1-68 +34-34,33-34 +93-93,48-92 +22-69,21-30 +2-7,16-83 +87-95,19-88 +93-98,94-97 +42-95,43-95 +6-7,6-83 +23-95,19-86 +30-94,19-98 +59-94,14-95 +24-84,24-83 +31-36,32-35 +20-57,21-57 +2-46,2-2 +9-44,6-9 +25-64,26-65 +21-96,43-97 +1-5,4-50 +58-72,57-59 +96-97,22-96 +3-63,31-64 +18-67,16-16 +38-42,37-43 +9-18,18-82 +53-59,52-52 +6-99,6-6 +56-89,89-97 +88-90,13-89 +24-26,25-85 +46-87,30-45 +63-64,18-64 +12-94,9-13 +51-98,50-50 +8-18,7-17 +25-25,25-32 +5-39,39-40 +7-18,7-7 +23-65,65-94 +17-43,16-44 +32-91,90-90 +3-5,2-5 +28-39,27-42 +12-13,12-65 +2-91,48-92 +93-98,72-87 +31-43,31-42 +12-69,12-59 +2-14,14-81 +17-89,17-90 +20-37,38-62 +6-36,3-22 +6-97,5-7 +71-81,8-77 +18-61,61-84 +86-86,22-85 +9-27,12-79 +1-46,8-47 +12-85,85-98 +57-66,58-65 +62-97,3-99 +5-80,6-20 +39-88,38-88 +54-63,13-64 +17-40,39-57 +9-99,10-82 +24-37,24-24 +64-81,65-81 +2-47,48-48 +5-6,6-6 +26-42,38-91 +17-81,80-81 +4-7,5-8 +23-43,23-34 +98-99,3-97 +43-75,74-75 +58-78,18-78 +60-78,60-78 +10-95,88-96 +77-78,20-78 +36-70,36-57 +19-65,20-66 +1-3,3-15 +1-5,5-47 +23-80,22-22 +38-73,29-38 +41-41,40-57 +8-96,9-89 +28-28,29-96 +22-85,85-97 +20-92,21-92 +35-53,54-54 +45-49,47-48 +20-90,82-91 +38-92,37-93 +1-64,55-62 +56-75,32-55 +13-98,14-92 +4-16,3-16 +52-75,52-74 +1-86,1-87 +12-81,12-82 +35-36,35-97 +9-67,67-68 +14-97,13-14 +6-6,5-69 +73-90,48-95 +24-27,26-28 +25-52,26-50 +75-75,75-98 +22-96,7-18 +60-88,60-89 +47-49,8-48 +19-44,36-67 +4-5,9-96 +30-74,75-75 +10-10,11-77 +47-79,80-80 +12-68,12-69 +38-79,52-91 +29-30,4-29 +40-88,40-49 +4-39,4-39 +15-83,82-84 +69-96,4-96 +5-93,2-94 +49-50,48-48 +68-84,68-84 +16-74,74-86 +7-86,6-8 +47-80,48-74 +3-69,69-71 +49-58,60-78 +31-59,32-60 +4-64,4-64 +59-91,91-91 +90-90,9-89 +70-85,44-86 +24-62,62-85 +1-11,2-94 +15-99,19-95 +12-32,31-32 +56-61,56-72 +7-44,1-7 +1-70,3-71 +29-62,1-61 +7-97,8-97 +90-92,48-91 +54-75,53-74 +2-98,98-98 +2-69,70-70 +45-88,64-89 +28-60,28-28 +10-94,27-98 +32-32,32-49 +63-64,31-62 +15-69,16-70 +16-23,13-16 +20-66,66-67 +16-99,17-93 +62-64,7-63 +97-97,41-78 +41-75,27-39 +2-38,37-37 +13-96,95-96 +7-35,42-75 +39-40,3-40 +4-89,2-5 +22-35,35-36 +6-59,58-59 +23-29,9-28 +77-77,76-76 +12-48,11-96 +57-85,56-84 +90-92,68-91 +97-98,62-98 +23-69,22-69 +4-85,3-95 +50-87,33-88 +33-74,32-74 +20-67,21-47 +5-90,6-91 +50-98,50-98 +4-94,1-26 +4-38,39-78 +91-91,11-90 +54-99,55-99 +80-84,84-85 +33-91,16-33 +62-83,29-61 +23-67,23-68 +26-94,29-91 +21-84,32-84 +48-61,28-48 +15-15,15-76 +20-26,58-79 +75-75,47-74 +30-63,31-63 +16-95,55-96 +45-92,44-66 +59-95,58-99 +19-19,18-19 +30-36,34-36 +5-8,3-9 +6-62,6-6 +90-90,67-89 +31-79,31-84 +20-24,19-23 +33-72,33-86 +59-95,1-59 +4-91,4-97 +92-93,75-92 +42-95,18-99 +95-96,20-95 +23-23,22-50 +5-99,5-6 +16-76,17-17 +48-59,49-76 +51-91,13-91 +10-11,5-10 +19-95,18-18 +41-41,1-41 +40-64,40-78 +56-84,12-83 +17-63,16-84 +15-49,15-15 +31-91,90-92 +38-40,8-85 +46-96,46-79 +54-72,54-54 +2-96,8-97 +2-96,1-98 +15-86,16-44 +10-65,9-65 +79-79,9-78 +18-70,17-70 +68-83,14-68 +28-68,26-29 +18-54,17-21 +61-82,20-60 +47-76,47-93 +6-79,79-85 +13-13,13-91 +40-40,40-92 +9-76,18-83 +17-40,18-98 +45-60,10-54 +26-48,48-63 +15-58,14-92 +69-70,22-69 +5-88,5-89 +42-44,46-89 +38-76,44-77 +1-73,2-79 +52-98,49-82 +5-94,29-99 +13-63,13-94 +72-87,71-86 +27-94,93-94 +35-94,1-93 +43-49,42-48 +72-94,72-72 +43-86,42-85 +1-3,3-94 +42-80,30-92 +22-85,23-84 +39-43,44-44 +36-36,28-35 +12-46,13-78 +14-98,14-98 +69-78,73-79 +91-98,74-91 +10-11,10-96 +32-40,25-39 +50-81,12-80 +88-89,6-88 +48-69,7-48 +4-8,9-93 +74-84,74-75 +27-82,27-82 +1-97,1-1 +43-69,43-68 +13-27,14-27 +9-27,32-78 +22-78,22-23 +47-58,46-46 +19-98,3-98 +27-78,27-78 +74-90,4-74 +44-46,31-45 +35-81,6-55 +6-60,6-59 +10-32,32-33 +23-23,23-53 +4-87,4-86 +96-96,6-95 +1-37,2-38 +77-88,74-87 +3-92,5-93 +15-96,16-92 +37-58,34-36 +40-75,38-74 +2-62,55-62 +43-93,43-43 +77-93,70-77 +56-76,56-77 +2-2,3-88 +2-53,53-60 +30-31,29-36 +30-30,29-88 +42-79,41-80 +75-75,28-74 +18-88,8-87 +13-63,64-64 +3-48,46-49 +12-30,12-12 +3-99,4-94 +3-3,4-64 +30-76,29-29 +12-60,11-54 +9-95,9-95 +66-73,61-73 +10-11,10-11 +42-72,42-44 +8-89,10-89 +25-74,75-75 +33-44,34-45 +28-78,27-77 +48-97,37-93 +46-65,10-64 +17-97,68-96 +50-93,93-95 +22-96,23-97 +2-20,1-86 +6-60,60-80 +33-65,66-66 +23-23,16-22 +41-89,40-62 +17-96,97-97 +70-71,9-70 +11-24,11-35 +8-48,49-49 +78-92,75-91 +21-98,99-99 +12-45,12-13 +36-36,35-58 +9-15,15-98 +43-61,61-89 +52-53,52-74 +2-51,1-5 +29-29,28-75 +1-45,3-47 +5-13,12-13 +17-86,17-17 +4-99,5-99 +77-88,76-90 +50-70,50-51 +3-88,2-87 +4-55,4-75 +8-22,8-8 +76-99,77-96 +14-72,13-90 +1-62,63-94 +11-74,5-10 +37-65,65-65 +10-73,11-91 +34-78,21-34 +47-61,46-62 +11-86,8-10 +14-39,14-40 +8-81,82-89 +85-86,18-85 +14-91,66-97 +20-51,19-43 +1-57,3-57 +14-52,45-53 +37-92,36-99 +84-84,32-83 +45-88,44-81 +30-80,80-92 +6-82,6-6 +9-9,9-87 +26-28,21-27 +1-92,7-93 +32-90,33-33 +26-44,44-66 +24-86,23-86 +19-83,84-99 +28-40,28-30 +18-88,17-87 +62-70,71-71 +3-86,3-3 +20-20,19-90 +7-18,4-17 +29-41,41-42 +24-57,40-58 +15-15,15-54 +19-47,19-20 +63-64,21-63 +29-30,21-30 +61-62,16-61 +16-78,8-15 +63-74,63-63 +68-69,31-69 +3-99,4-99 +28-28,29-39 +72-85,3-67 +64-67,68-68 +75-97,74-74 +11-99,80-88 +10-93,24-79 +93-94,2-94 +1-98,1-99 +2-95,2-2 +20-75,21-75 +26-85,2-26 +9-96,9-99 +85-85,85-85 +59-70,59-70 +22-27,23-23 +41-42,41-52 +2-17,1-16 +41-54,55-55 +8-89,7-9 +13-98,9-13 +10-84,3-84 +40-90,39-39 +12-93,2-93 +5-99,4-99 +30-81,30-88 +1-43,2-54 +72-84,81-85 +2-34,4-35 +7-88,6-88 +66-98,65-99 +93-94,36-94 +66-76,67-77 +15-97,15-98 +53-60,53-54 +1-81,81-86 +51-65,17-50 +11-51,4-50 +16-16,17-82 +44-80,45-81 +32-39,9-38 +51-76,51-75 +58-98,59-79 +60-61,60-61 +7-8,7-87 +32-33,32-32 +35-85,79-97 +20-65,20-66 +5-73,6-74 +15-76,14-82 +43-91,43-99 +8-94,42-95 +19-92,92-94 +2-99,2-2 +91-96,5-92 +7-94,7-7 +82-82,33-82 +1-5,1-2 +87-88,48-87 +88-88,40-88 +24-32,10-31 +15-82,14-81 +18-34,18-33 +40-46,39-73 +40-64,46-65 +3-94,93-95 +41-56,57-85 +48-56,2-48 +79-93,80-94 +45-78,44-66 +49-77,77-89 +29-67,28-28 +53-96,95-97 +6-44,24-99 +7-68,28-84 +6-77,76-78 +3-99,98-99 +11-85,11-85 +24-24,25-96 +44-51,52-64 +88-90,89-98 +2-97,3-97 +15-16,15-58 +5-96,6-92 +2-22,5-21 +11-11,10-87 +95-98,75-96 +8-69,3-7 +25-81,26-74 +25-99,24-68 +92-93,17-92 +2-73,2-2 +87-95,11-87 +20-93,15-97 +7-98,49-98 +4-79,4-31 +50-57,50-57 +67-68,34-68 +57-68,72-90 +3-33,10-34 +26-27,26-80 +8-8,8-82 +93-93,2-93 +12-86,1-12 +4-8,8-89 +14-49,15-50 +5-35,33-33 +92-93,6-93 +28-76,76-93 +47-48,47-85 +7-94,2-43 +43-85,22-43 +71-91,90-91 +14-92,14-91 +5-8,9-78 +13-33,33-33 +32-93,37-86 +37-89,37-90 +51-53,11-52 +11-38,37-37 +4-85,4-89 +6-10,7-14 +21-33,20-91 +9-92,92-92 +29-96,97-97 +13-94,12-98 +11-96,12-97 +35-39,39-70 +31-60,31-95 +18-84,17-83 +5-6,5-89 +63-64,40-63 +89-90,6-90 +88-89,89-90 +69-83,76-83 +16-82,81-82 +5-50,49-80 +9-98,9-99 +75-76,18-75 +1-46,16-46 +45-52,46-52 +18-43,17-19 +8-80,19-81 +11-11,11-86 +20-49,19-49 +54-89,53-90 +9-9,9-95 +21-86,17-21 +57-95,33-94 +13-14,13-53 +7-21,8-21 +3-24,12-25 +89-91,45-90 +82-83,22-82 +57-63,64-72 +6-7,6-70 +19-97,18-18 +30-56,30-31 +24-96,25-98 +1-74,7-99 +24-90,25-63 +44-57,43-58 +62-85,86-86 +15-49,20-50 +20-69,20-69 +38-66,67-77 +8-27,7-91 +8-21,15-51 +19-83,9-10 +23-23,20-22 +19-19,19-88 +4-9,1-8 +15-69,16-16 +20-42,42-69 +43-47,7-46 +5-93,6-94 +10-55,9-54 +8-91,9-27 +42-97,43-98 +20-82,14-81 +19-31,19-70 +7-93,7-8 +28-66,17-28 +65-96,17-95 +40-78,77-79 +51-97,53-96 +25-40,11-25 +19-97,20-96 +23-88,40-88 +81-81,35-80 +71-86,34-86 +13-88,14-89 +63-84,64-85 +56-98,7-97 +31-82,30-81 +58-98,58-99 +36-47,46-46 +80-80,20-79 +59-92,19-91 +19-66,63-84 +70-70,4-70 +66-74,65-79 +5-68,1-4 +17-52,11-17 +6-77,78-78 +10-11,10-25 +13-96,22-61 +5-20,6-31 +18-61,19-50 +6-35,6-34 +12-91,11-90 +1-1,2-21 +2-98,1-99 +28-98,25-38 +34-63,35-76 +20-85,84-85 +11-13,13-31 +72-74,2-73 +3-10,6-25 +46-46,46-88 +34-59,32-60 +77-77,23-77 +11-12,11-83 +4-83,5-84 +10-89,9-88 +82-90,79-83 +1-60,4-61 +29-93,28-92 +1-47,36-48 +51-94,45-93 +30-62,30-33 +3-96,4-97 +57-96,56-96 +44-44,44-94 +44-58,44-45 +3-16,26-84 +31-32,31-78 +41-96,42-96 +34-63,62-90 +20-22,16-23 +61-99,60-98 +3-3,4-21 +2-46,11-47 +7-8,7-64 +7-47,47-70 +49-87,50-89 +22-95,21-77 +87-87,18-86 +30-30,30-99 +54-66,65-66 +77-77,37-76 +11-32,13-32 +93-95,19-93 +3-93,1-92 +37-46,36-55 +12-76,76-85 +18-94,23-74 +10-99,98-99 +23-23,24-31 +4-6,9-96 +42-56,9-45 +9-50,14-51 +22-82,23-83 +21-56,56-92 +1-7,1-1 +96-96,1-96 +61-70,27-70 +2-38,9-37 +19-20,20-56 +61-66,1-67 +42-44,2-43 +6-6,6-90 +40-89,4-14 +96-96,11-96 +54-77,77-88 +29-81,29-81 +12-77,5-76 +4-86,3-86 +57-58,20-57 +18-18,19-62 +9-56,10-57 +26-91,35-92 +6-56,7-81 +1-78,1-61 +76-79,43-76 +96-99,32-97 +27-30,28-31 +94-95,21-94 +22-25,18-24 +40-89,39-41 +20-26,20-20 +28-77,77-88 +2-79,14-80 +17-34,20-33 +26-89,25-88 +5-29,20-73 +9-78,9-77 +11-12,11-79 +25-62,24-26 +1-94,1-2 +90-91,51-90 +8-96,9-97 +6-20,17-40 +42-82,24-42 +10-77,10-76 +32-88,33-87 +58-63,62-62 +64-70,64-71 +34-54,55-55 +88-90,19-88 +77-86,77-78 +3-88,88-89 +42-45,31-44 +99-99,88-97 +81-99,18-81 +7-95,11-88 +4-72,2-92 +7-7,7-26 +10-90,9-89 +2-2,3-65 +7-49,8-49 +51-98,52-92 +27-70,2-67 +65-73,37-69 +81-83,81-82 +40-55,41-54 +4-38,29-90 +53-57,36-57 +79-79,13-78 +18-83,19-84 +11-91,10-93 +7-79,8-80 +18-56,18-18 +80-80,32-79 +2-44,34-63 +6-73,73-74 +12-70,13-71 +25-57,15-56 +40-95,40-41 +1-63,1-2 +7-7,8-71 +23-94,23-23 +51-89,46-88 +67-67,8-66 +24-91,21-25 +67-72,9-71 +8-97,9-97 +50-60,36-60 +1-56,27-32 +2-6,6-91 +67-73,68-74 +30-30,30-37 +4-69,5-68 +94-95,10-95 +30-77,77-78 +10-95,10-60 +37-93,2-32 +8-99,7-7 +57-89,58-99 +1-30,13-54 +31-97,32-32 +82-82,18-82 +10-17,15-18 +74-87,75-88 +21-84,82-86 +11-90,12-17 +26-98,97-99 +35-95,35-95 +2-4,3-75 +3-23,24-60 +85-94,85-95 +1-3,3-22 +53-78,53-79 +1-41,3-40 +54-87,54-65 +3-91,17-90 +41-75,40-74 +44-78,43-77 +75-80,41-74 +43-58,36-47 +61-63,62-64 +16-88,16-87 +4-93,5-94 +56-93,93-98 +27-66,27-27 +51-75,35-74 +50-77,38-76 +51-78,29-77 +12-19,20-86 +4-90,1-71 +13-15,14-25 +75-84,74-74 +7-90,6-65 +56-73,72-74 +20-21,20-83 +7-35,3-35 +1-5,5-46 +30-90,46-90 +10-78,3-87 +25-87,25-25 +5-7,6-92 +24-85,25-84 +14-87,16-87 +3-87,4-88 +53-64,52-52 +71-73,72-73 +19-33,32-43 +3-68,24-69 +4-43,9-44 +62-62,62-69 +32-38,37-38 +14-68,68-70 +64-88,88-95 +27-81,81-84 +2-24,24-24 +52-52,52-90 +1-97,11-95 +9-94,4-93 +5-33,4-24 +37-37,36-76 +1-2,3-64 +85-93,10-85 +39-85,38-84 +47-48,16-47 +30-49,31-50 +46-85,45-45 +7-86,4-85 +23-66,66-67 +54-95,16-27 +2-3,7-59 +57-57,35-56 +20-21,20-90 +35-58,34-54 +14-91,91-93 +9-99,16-97 +24-50,25-50 +18-27,19-86 +78-92,79-79 +23-90,53-91 +27-73,69-70 +90-94,25-90 +8-68,1-67 +50-87,5-50 +25-67,24-67 +3-98,8-97 +85-99,57-98 +34-34,17-35 +8-80,81-81 +14-98,15-99 +1-2,1-99 +24-49,23-43 +36-39,35-39 +1-25,11-26 +2-51,6-97 +33-58,23-57 +25-80,24-26 +26-82,25-25 +44-95,43-94 +61-68,9-62 +27-55,28-48 +22-88,20-23 diff --git a/src/problem_04/mod.rs b/src/problem_04/mod.rs new file mode 100644 index 0000000..1f827fc --- /dev/null +++ b/src/problem_04/mod.rs @@ -0,0 +1,60 @@ +static INPUT: &str = include_str!("input.txt"); + +struct ElfRange { + lower: i32, + upper: i32, +} + +fn split_ranges(s: &str) -> (ElfRange, ElfRange) { + let binding = String::from(s) + .split(|c| c == '-' || c == ',') + .map(|c| c.parse().expect("not a number!")) + .collect::>(); + let mut ranges = binding.iter(); + match (ranges.next(), ranges.next(), ranges.next(), ranges.next()) { + (Some(a), Some(b), Some(c), Some(d)) => ( + ElfRange { + lower: *a, + upper: *b, + }, + ElfRange { + lower: *c, + upper: *d, + }, + ), + _ => panic!("error parsing input"), + } +} + +fn total_overlap(a: ElfRange, b: ElfRange) -> bool { + (a.lower <= b.lower && a.upper >= b.upper) || (b.lower <= a.lower && b.upper >= a.upper) +} + +fn partial_overlap(a: ElfRange, b: ElfRange) -> bool { + (b.lower >= a.lower && b.lower <= a.upper) + || (b.upper >= a.lower && b.upper <= a.upper) + || (a.lower >= b.lower && a.lower <= b.upper) + || (a.upper >= b.lower && a.upper <= b.upper) +} + +pub fn solution_a() -> i32 { + let mut total = 0; + for line in INPUT.lines() { + let (a, b) = split_ranges(line); + if total_overlap(a, b) { + total += 1; + } + } + total +} + +pub fn solution_b() -> i32 { + let mut total = 0; + for line in INPUT.lines() { + let (a, b) = split_ranges(line); + if partial_overlap(a, b) { + total += 1; + } + } + total +} From 04520e62cec1852da60321b45ee4a32c94c5d48c Mon Sep 17 00:00:00 2001 From: Thorn Avery Date: Tue, 6 Dec 2022 13:02:15 +1100 Subject: [PATCH 08/10] day 5 done --- src/lib.rs | 1 + src/main.rs | 4 + src/problem_05/description.txt | 111 +++++++ src/problem_05/input.txt | 514 +++++++++++++++++++++++++++++++++ src/problem_05/mod.rs | 102 +++++++ 5 files changed, 732 insertions(+) create mode 100644 src/problem_05/description.txt create mode 100644 src/problem_05/input.txt create mode 100644 src/problem_05/mod.rs diff --git a/src/lib.rs b/src/lib.rs index 947f87f..cc479cc 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -2,3 +2,4 @@ pub mod problem_01; pub mod problem_02; pub mod problem_03; pub mod problem_04; +pub mod problem_05; diff --git a/src/main.rs b/src/main.rs index 88fe459..808995c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,6 +2,7 @@ use taoc2022::problem_01; use taoc2022::problem_02; use taoc2022::problem_03; use taoc2022::problem_04; +use taoc2022::problem_05; fn main() { println!("== Problem 01 =="); @@ -16,4 +17,7 @@ fn main() { println!("== Problem 04 =="); println!("= a: {}", problem_04::solution_a()); println!("= b: {}", problem_04::solution_b()); + println!("== Problem 05 =="); + println!("= a: {}", problem_05::solution_a()); + println!("= b: {}", problem_05::solution_b()); } diff --git a/src/problem_05/description.txt b/src/problem_05/description.txt new file mode 100644 index 0000000..c7c710d --- /dev/null +++ b/src/problem_05/description.txt @@ -0,0 +1,111 @@ +--- Day 5: Supply Stacks --- + +The expedition can depart as soon as the final supplies have been unloaded from the ships. Supplies are stored in stacks of marked crates, but because the needed supplies are buried under many other crates, the crates need to be rearranged. + +The ship has a giant cargo crane capable of moving crates between stacks. To ensure none of the crates get crushed or fall over, the crane operator will rearrange them in a series of carefully-planned steps. After the crates are rearranged, the desired crates will be at the top of each stack. + +The Elves don't want to interrupt the crane operator during this delicate procedure, but they forgot to ask her which crate will end up where, and they want to be ready to unload them as soon as possible so they can embark. + +They do, however, have a drawing of the starting stacks of crates and the rearrangement procedure (your puzzle input). For example: + + [D] +[N] [C] +[Z] [M] [P] + 1 2 3 + +move 1 from 2 to 1 +move 3 from 1 to 3 +move 2 from 2 to 1 +move 1 from 1 to 2 + +In this example, there are three stacks of crates. Stack 1 contains two crates: crate Z is on the bottom, and crate N is on top. Stack 2 contains three crates; from bottom to top, they are crates M, C, and D. Finally, stack 3 contains a single crate, P. + +Then, the rearrangement procedure is given. In each step of the procedure, a quantity of crates is moved from one stack to a different stack. In the first step of the above rearrangement procedure, one crate is moved from stack 2 to stack 1, resulting in this configuration: + +[D] +[N] [C] +[Z] [M] [P] + 1 2 3 + +In the second step, three crates are moved from stack 1 to stack 3. Crates are moved one at a time, so the first crate to be moved (D) ends up below the second and third crates: + + [Z] + [N] + [C] [D] + [M] [P] + 1 2 3 + +Then, both crates are moved from stack 2 to stack 1. Again, because crates are moved one at a time, crate C ends up below crate M: + + [Z] + [N] +[M] [D] +[C] [P] + 1 2 3 + +Finally, one crate is moved from stack 1 to stack 2: + + [Z] + [N] + [D] +[C] [M] [P] + 1 2 3 + +The Elves just need to know which crate will end up on top of each stack; in this example, the top crates are C in stack 1, M in stack 2, and Z in stack 3, so you should combine these together and give the Elves the message CMZ. + +After the rearrangement procedure completes, what crate ends up on top of each stack? + +Your puzzle answer was SBPQRSCDF. +--- Part Two --- + +As you watch the crane operator expertly rearrange the crates, you notice the process isn't following your prediction. + +Some mud was covering the writing on the side of the crane, and you quickly wipe it away. The crane isn't a CrateMover 9000 - it's a CrateMover 9001. + +The CrateMover 9001 is notable for many new and exciting features: air conditioning, leather seats, an extra cup holder, and the ability to pick up and move multiple crates at once. + +Again considering the example above, the crates begin in the same configuration: + + [D] +[N] [C] +[Z] [M] [P] + 1 2 3 + +Moving a single crate from stack 2 to stack 1 behaves the same as before: + +[D] +[N] [C] +[Z] [M] [P] + 1 2 3 + +However, the action of moving three crates from stack 1 to stack 3 means that those three moved crates stay in the same order, resulting in this new configuration: + + [D] + [N] + [C] [Z] + [M] [P] + 1 2 3 + +Next, as both crates are moved from stack 2 to stack 1, they retain their order as well: + + [D] + [N] +[C] [Z] +[M] [P] + 1 2 3 + +Finally, a single crate is still moved from stack 1 to stack 2, but now it's crate C that gets moved: + + [D] + [N] + [Z] +[M] [C] [P] + 1 2 3 + +In this example, the CrateMover 9001 has put the crates in a totally different order: MCD. + +Before the rearrangement process finishes, update your simulation so that the Elves know where they should stand to be ready to unload the final supplies. After the rearrangement procedure completes, what crate ends up on top of each stack? + +Your puzzle answer was RGLVRCQSB. + +Both parts of this puzzle are complete! They provide two gold stars: ** diff --git a/src/problem_05/input.txt b/src/problem_05/input.txt new file mode 100644 index 0000000..9f9e78d --- /dev/null +++ b/src/problem_05/input.txt @@ -0,0 +1,514 @@ +[V] [B] [F] +[N] [Q] [W] [R] [B] +[F] [D] [S] [B] [L] [P] +[S] [J] [C] [F] [C] [D] [G] +[M] [M] [H] [L] [P] [N] [P] [V] +[P] [L] [D] [C] [T] [Q] [R] [S] [J] +[H] [R] [Q] [S] [V] [R] [V] [Z] [S] +[J] [S] [N] [R] [M] [T] [G] [C] [D] + 1 2 3 4 5 6 7 8 9 + +move 1 from 8 to 4 +move 1 from 7 to 8 +move 1 from 6 to 3 +move 2 from 6 to 5 +move 8 from 5 to 1 +move 5 from 3 to 8 +move 1 from 7 to 8 +move 8 from 1 to 2 +move 3 from 3 to 9 +move 13 from 8 to 7 +move 2 from 1 to 2 +move 1 from 6 to 2 +move 2 from 1 to 7 +move 4 from 4 to 2 +move 10 from 9 to 4 +move 7 from 4 to 1 +move 1 from 6 to 7 +move 2 from 4 to 5 +move 1 from 5 to 2 +move 1 from 5 to 8 +move 3 from 1 to 5 +move 2 from 4 to 6 +move 2 from 6 to 4 +move 2 from 4 to 5 +move 5 from 1 to 5 +move 1 from 9 to 5 +move 1 from 8 to 5 +move 14 from 2 to 6 +move 12 from 7 to 4 +move 4 from 6 to 7 +move 3 from 6 to 4 +move 4 from 4 to 9 +move 2 from 4 to 6 +move 2 from 9 to 3 +move 9 from 4 to 3 +move 2 from 1 to 6 +move 5 from 7 to 3 +move 4 from 7 to 8 +move 2 from 6 to 7 +move 3 from 6 to 7 +move 10 from 5 to 8 +move 8 from 8 to 9 +move 10 from 9 to 7 +move 12 from 7 to 5 +move 1 from 1 to 5 +move 3 from 7 to 2 +move 10 from 3 to 7 +move 6 from 5 to 7 +move 2 from 6 to 1 +move 12 from 2 to 7 +move 3 from 3 to 1 +move 1 from 6 to 5 +move 10 from 5 to 7 +move 3 from 3 to 4 +move 34 from 7 to 1 +move 2 from 6 to 9 +move 1 from 6 to 3 +move 3 from 1 to 3 +move 1 from 7 to 3 +move 3 from 3 to 6 +move 1 from 4 to 3 +move 22 from 1 to 6 +move 2 from 9 to 7 +move 2 from 4 to 9 +move 12 from 6 to 8 +move 1 from 7 to 6 +move 4 from 8 to 2 +move 1 from 7 to 1 +move 6 from 8 to 9 +move 1 from 2 to 5 +move 1 from 2 to 7 +move 13 from 1 to 2 +move 2 from 3 to 1 +move 4 from 7 to 5 +move 3 from 9 to 4 +move 1 from 1 to 8 +move 4 from 5 to 2 +move 12 from 6 to 2 +move 3 from 1 to 3 +move 1 from 4 to 1 +move 9 from 8 to 5 +move 6 from 5 to 7 +move 2 from 4 to 5 +move 5 from 9 to 6 +move 5 from 3 to 7 +move 30 from 2 to 6 +move 11 from 7 to 9 +move 36 from 6 to 3 +move 10 from 9 to 3 +move 1 from 6 to 5 +move 3 from 5 to 2 +move 2 from 5 to 2 +move 28 from 3 to 4 +move 6 from 4 to 1 +move 1 from 2 to 3 +move 2 from 5 to 2 +move 6 from 1 to 7 +move 1 from 1 to 6 +move 6 from 3 to 5 +move 6 from 7 to 2 +move 1 from 6 to 4 +move 2 from 2 to 6 +move 8 from 2 to 1 +move 3 from 2 to 4 +move 2 from 3 to 4 +move 4 from 3 to 4 +move 1 from 6 to 1 +move 2 from 1 to 8 +move 1 from 6 to 4 +move 1 from 9 to 3 +move 2 from 5 to 4 +move 1 from 8 to 7 +move 1 from 7 to 9 +move 1 from 3 to 5 +move 1 from 8 to 6 +move 34 from 4 to 9 +move 13 from 9 to 8 +move 1 from 8 to 2 +move 1 from 2 to 1 +move 4 from 5 to 1 +move 9 from 8 to 7 +move 11 from 1 to 3 +move 1 from 4 to 2 +move 1 from 6 to 7 +move 1 from 9 to 4 +move 1 from 4 to 1 +move 1 from 5 to 3 +move 5 from 7 to 8 +move 1 from 2 to 5 +move 1 from 5 to 1 +move 21 from 9 to 5 +move 19 from 3 to 4 +move 17 from 4 to 6 +move 2 from 8 to 4 +move 2 from 6 to 8 +move 2 from 6 to 9 +move 2 from 7 to 6 +move 1 from 4 to 9 +move 6 from 5 to 6 +move 1 from 9 to 8 +move 8 from 5 to 7 +move 15 from 6 to 2 +move 1 from 9 to 7 +move 2 from 1 to 6 +move 3 from 4 to 7 +move 1 from 1 to 6 +move 3 from 5 to 4 +move 2 from 5 to 6 +move 2 from 4 to 1 +move 13 from 7 to 8 +move 2 from 6 to 4 +move 3 from 2 to 4 +move 2 from 7 to 6 +move 5 from 4 to 6 +move 4 from 2 to 6 +move 1 from 1 to 9 +move 18 from 8 to 3 +move 1 from 4 to 5 +move 1 from 2 to 7 +move 15 from 3 to 1 +move 1 from 5 to 1 +move 3 from 3 to 4 +move 1 from 5 to 4 +move 1 from 5 to 6 +move 1 from 6 to 8 +move 2 from 8 to 2 +move 3 from 1 to 8 +move 6 from 2 to 8 +move 1 from 7 to 6 +move 12 from 8 to 5 +move 2 from 9 to 6 +move 6 from 1 to 5 +move 9 from 5 to 3 +move 1 from 2 to 8 +move 20 from 6 to 9 +move 3 from 6 to 7 +move 1 from 7 to 1 +move 7 from 3 to 4 +move 2 from 7 to 2 +move 1 from 8 to 7 +move 8 from 4 to 1 +move 11 from 1 to 7 +move 10 from 7 to 6 +move 2 from 4 to 9 +move 21 from 9 to 3 +move 6 from 5 to 9 +move 6 from 3 to 2 +move 1 from 4 to 5 +move 1 from 7 to 9 +move 8 from 3 to 2 +move 9 from 2 to 1 +move 14 from 1 to 6 +move 1 from 1 to 7 +move 4 from 3 to 8 +move 3 from 8 to 7 +move 5 from 7 to 4 +move 3 from 6 to 9 +move 2 from 3 to 7 +move 3 from 5 to 6 +move 1 from 5 to 6 +move 2 from 7 to 9 +move 1 from 8 to 3 +move 22 from 6 to 5 +move 3 from 9 to 4 +move 3 from 6 to 1 +move 5 from 4 to 6 +move 9 from 2 to 8 +move 4 from 6 to 1 +move 1 from 3 to 2 +move 1 from 2 to 3 +move 6 from 8 to 1 +move 2 from 4 to 3 +move 10 from 1 to 7 +move 2 from 8 to 7 +move 1 from 9 to 6 +move 4 from 3 to 5 +move 1 from 8 to 3 +move 4 from 9 to 8 +move 1 from 4 to 3 +move 1 from 3 to 8 +move 3 from 7 to 6 +move 1 from 1 to 5 +move 10 from 5 to 9 +move 5 from 6 to 4 +move 5 from 8 to 5 +move 4 from 9 to 8 +move 3 from 3 to 9 +move 2 from 8 to 6 +move 5 from 7 to 5 +move 1 from 4 to 1 +move 1 from 1 to 2 +move 2 from 8 to 6 +move 1 from 2 to 1 +move 1 from 7 to 2 +move 1 from 1 to 5 +move 28 from 5 to 9 +move 3 from 6 to 1 +move 1 from 6 to 9 +move 1 from 2 to 9 +move 2 from 1 to 2 +move 2 from 7 to 5 +move 1 from 7 to 5 +move 1 from 2 to 5 +move 3 from 1 to 9 +move 1 from 5 to 8 +move 15 from 9 to 2 +move 11 from 9 to 4 +move 11 from 4 to 7 +move 2 from 4 to 1 +move 7 from 7 to 8 +move 1 from 1 to 4 +move 20 from 9 to 1 +move 2 from 7 to 8 +move 1 from 4 to 6 +move 1 from 6 to 2 +move 2 from 7 to 5 +move 1 from 9 to 6 +move 1 from 4 to 9 +move 4 from 5 to 2 +move 1 from 6 to 8 +move 1 from 4 to 9 +move 11 from 8 to 3 +move 1 from 1 to 9 +move 1 from 5 to 9 +move 1 from 2 to 6 +move 4 from 9 to 8 +move 4 from 8 to 7 +move 10 from 1 to 6 +move 7 from 1 to 5 +move 8 from 3 to 4 +move 2 from 3 to 5 +move 3 from 7 to 4 +move 1 from 4 to 5 +move 2 from 1 to 6 +move 9 from 2 to 6 +move 1 from 7 to 9 +move 1 from 3 to 2 +move 7 from 4 to 3 +move 3 from 3 to 7 +move 5 from 2 to 3 +move 1 from 1 to 9 +move 2 from 2 to 7 +move 1 from 4 to 6 +move 3 from 5 to 6 +move 4 from 7 to 6 +move 1 from 7 to 4 +move 1 from 4 to 7 +move 1 from 2 to 8 +move 1 from 7 to 1 +move 27 from 6 to 2 +move 1 from 4 to 1 +move 7 from 5 to 7 +move 1 from 4 to 1 +move 1 from 8 to 3 +move 3 from 7 to 3 +move 2 from 1 to 6 +move 2 from 9 to 1 +move 18 from 2 to 1 +move 2 from 7 to 5 +move 12 from 3 to 4 +move 1 from 5 to 6 +move 3 from 6 to 1 +move 24 from 1 to 8 +move 9 from 2 to 4 +move 3 from 2 to 1 +move 2 from 6 to 3 +move 1 from 6 to 9 +move 1 from 5 to 6 +move 1 from 6 to 2 +move 1 from 1 to 7 +move 1 from 2 to 1 +move 1 from 1 to 2 +move 3 from 7 to 2 +move 2 from 1 to 4 +move 8 from 4 to 5 +move 22 from 8 to 1 +move 1 from 8 to 1 +move 13 from 4 to 1 +move 1 from 8 to 5 +move 3 from 3 to 1 +move 1 from 2 to 7 +move 38 from 1 to 6 +move 27 from 6 to 1 +move 2 from 2 to 9 +move 3 from 9 to 8 +move 2 from 8 to 6 +move 1 from 8 to 3 +move 1 from 2 to 1 +move 1 from 3 to 6 +move 1 from 2 to 3 +move 1 from 7 to 6 +move 7 from 6 to 3 +move 20 from 1 to 4 +move 6 from 1 to 6 +move 17 from 4 to 7 +move 3 from 6 to 5 +move 14 from 7 to 9 +move 8 from 5 to 7 +move 3 from 1 to 6 +move 3 from 3 to 1 +move 2 from 4 to 1 +move 4 from 5 to 1 +move 9 from 6 to 2 +move 3 from 6 to 4 +move 4 from 7 to 8 +move 4 from 1 to 6 +move 2 from 3 to 1 +move 6 from 6 to 7 +move 4 from 8 to 7 +move 4 from 2 to 1 +move 4 from 2 to 3 +move 4 from 9 to 5 +move 8 from 9 to 5 +move 1 from 9 to 5 +move 1 from 2 to 1 +move 16 from 7 to 2 +move 10 from 2 to 9 +move 11 from 9 to 8 +move 4 from 3 to 5 +move 3 from 1 to 4 +move 13 from 5 to 7 +move 10 from 8 to 5 +move 2 from 1 to 5 +move 11 from 7 to 4 +move 2 from 3 to 6 +move 3 from 7 to 6 +move 1 from 3 to 2 +move 1 from 1 to 8 +move 2 from 8 to 4 +move 3 from 1 to 2 +move 4 from 6 to 1 +move 7 from 1 to 9 +move 1 from 6 to 7 +move 2 from 5 to 8 +move 1 from 2 to 9 +move 1 from 7 to 8 +move 5 from 5 to 8 +move 1 from 2 to 3 +move 4 from 2 to 5 +move 17 from 4 to 1 +move 10 from 5 to 9 +move 2 from 4 to 2 +move 2 from 4 to 1 +move 1 from 4 to 9 +move 1 from 3 to 7 +move 1 from 7 to 8 +move 12 from 9 to 2 +move 1 from 2 to 4 +move 1 from 4 to 1 +move 1 from 1 to 9 +move 1 from 8 to 1 +move 8 from 8 to 3 +move 2 from 5 to 1 +move 3 from 1 to 9 +move 1 from 2 to 6 +move 4 from 3 to 7 +move 1 from 7 to 6 +move 10 from 9 to 2 +move 1 from 5 to 9 +move 1 from 9 to 3 +move 17 from 1 to 6 +move 2 from 1 to 2 +move 11 from 6 to 7 +move 2 from 2 to 9 +move 2 from 9 to 5 +move 12 from 7 to 9 +move 20 from 2 to 7 +move 5 from 9 to 5 +move 21 from 7 to 1 +move 2 from 6 to 4 +move 11 from 1 to 4 +move 5 from 4 to 6 +move 1 from 7 to 8 +move 5 from 9 to 3 +move 5 from 2 to 8 +move 3 from 9 to 3 +move 2 from 8 to 7 +move 2 from 1 to 7 +move 10 from 6 to 3 +move 1 from 2 to 6 +move 2 from 8 to 5 +move 1 from 6 to 5 +move 2 from 4 to 9 +move 1 from 4 to 5 +move 8 from 1 to 6 +move 4 from 4 to 8 +move 6 from 8 to 4 +move 21 from 3 to 9 +move 5 from 9 to 2 +move 4 from 7 to 9 +move 22 from 9 to 3 +move 9 from 6 to 4 +move 2 from 2 to 6 +move 2 from 2 to 1 +move 2 from 5 to 7 +move 7 from 5 to 4 +move 22 from 4 to 2 +move 2 from 5 to 4 +move 16 from 2 to 5 +move 2 from 6 to 2 +move 13 from 3 to 4 +move 5 from 5 to 7 +move 15 from 4 to 7 +move 3 from 2 to 3 +move 3 from 2 to 5 +move 1 from 1 to 2 +move 1 from 2 to 4 +move 6 from 5 to 9 +move 4 from 3 to 6 +move 2 from 5 to 9 +move 1 from 2 to 7 +move 1 from 1 to 9 +move 2 from 4 to 5 +move 19 from 7 to 8 +move 1 from 6 to 5 +move 1 from 5 to 1 +move 1 from 9 to 4 +move 5 from 8 to 1 +move 3 from 8 to 1 +move 7 from 5 to 6 +move 3 from 7 to 1 +move 1 from 2 to 5 +move 4 from 9 to 8 +move 2 from 5 to 6 +move 10 from 1 to 4 +move 1 from 7 to 2 +move 6 from 3 to 4 +move 9 from 4 to 3 +move 2 from 2 to 8 +move 2 from 9 to 5 +move 5 from 8 to 3 +move 1 from 1 to 5 +move 2 from 5 to 6 +move 1 from 1 to 7 +move 2 from 9 to 7 +move 8 from 4 to 7 +move 3 from 3 to 9 +move 4 from 6 to 3 +move 1 from 5 to 3 +move 1 from 7 to 2 +move 1 from 2 to 1 +move 1 from 6 to 5 +move 1 from 5 to 2 +move 10 from 7 to 4 +move 10 from 4 to 1 +move 10 from 1 to 8 +move 1 from 9 to 6 +move 1 from 1 to 4 +move 11 from 8 to 1 +move 2 from 9 to 5 +move 5 from 6 to 3 +move 1 from 3 to 8 +move 4 from 1 to 3 +move 5 from 3 to 8 +move 1 from 4 to 7 +move 1 from 7 to 2 +move 13 from 3 to 5 +move 2 from 2 to 1 +move 4 from 3 to 1 +move 4 from 5 to 6 +move 3 from 6 to 2 +move 4 from 5 to 4 +move 8 from 8 to 7 +move 1 from 3 to 9 diff --git a/src/problem_05/mod.rs b/src/problem_05/mod.rs new file mode 100644 index 0000000..8ca09d1 --- /dev/null +++ b/src/problem_05/mod.rs @@ -0,0 +1,102 @@ +use std::collections::VecDeque; + +static INPUT: &str = include_str!("input.txt"); + +type Instruction = (usize, usize, usize); + +fn parse_input(s: &str) -> (Vec>, Vec) { + let mut initial: Vec> = vec![]; + let mut instructions = vec![]; + let mut input = s.lines(); + for line in input.by_ref() { + if line.is_empty() { + break; + } + initial.push(line.chars().collect()); + } + initial.reverse(); + initial.remove(0); + let num_columns = (initial[0].len() + 1) / 4; + let mut finals = vec![VecDeque::new(); num_columns]; + for line in initial { + for (i, tower) in finals.iter_mut().enumerate().take(num_columns) { + let ind = (i * 4) + 1; + match line[ind] { + ' ' => continue, + _ => tower.push_front(line[ind]), + } + } + } + for line in input { + let parsed_line = parse_instruction(line); + instructions.push(parsed_line); + } + (finals, instructions) +} + +fn parse_instruction(s: &str) -> Instruction { + let binding = String::from(s) + .split(|c: char| c == ' ' || c.is_alphabetic()) + .filter(|v| !v.is_empty()) + .map(|c| c.parse().expect("not a number!")) + .collect::>(); + let mut instructions = binding.iter(); + match ( + instructions.next(), + instructions.next(), + instructions.next(), + ) { + (Some(a), Some(b), Some(c)) => (*a, *b, *c), + _ => panic!("error parsing input"), + } +} + +fn execute_instructions_900( + instructions: Vec, + mut initial: Vec>, +) -> Vec> { + for (num, src, dst) in instructions { + for _i in 0..num { + match initial[src - 1].pop_front() { + Some(a) => initial[dst - 1].push_front(a), + _ => panic!("attempted to pop from empty stack"), + } + } + } + initial +} + +fn execute_instructions_9001( + instructions: Vec, + mut initial: Vec>, +) -> Vec> { + for (num, src, dst) in instructions { + for i in 0..num { + match initial[src - 1].pop_front() { + Some(a) => initial[dst - 1].insert(i, a), + _ => panic!("attempted to pop from empty stack"), + } + } + } + initial +} + +pub fn solution_a() -> String { + let (initial, instructions) = parse_input(INPUT); + let initial = execute_instructions_900(instructions, initial); + let mut result = String::new(); + for tower in initial { + result.push(tower[0]); + } + result +} + +pub fn solution_b() -> String { + let (initial, instructions) = parse_input(INPUT); + let initial = execute_instructions_9001(instructions, initial); + let mut result = String::new(); + for tower in initial { + result.push(tower[0]); + } + result +} From 22e444e6ea903c0340c32bf82b567cde1254765e Mon Sep 17 00:00:00 2001 From: Thorn Avery Date: Tue, 13 Dec 2022 15:00:43 +1100 Subject: [PATCH 09/10] day 6 done, kinda proud of this one --- src/lib.rs | 1 + src/main.rs | 4 +++ src/problem_06/description.txt | 53 ++++++++++++++++++++++++++++++++++ src/problem_06/input.txt | 1 + src/problem_06/mod.rs | 39 +++++++++++++++++++++++++ 5 files changed, 98 insertions(+) create mode 100644 src/problem_06/description.txt create mode 100644 src/problem_06/input.txt create mode 100644 src/problem_06/mod.rs diff --git a/src/lib.rs b/src/lib.rs index cc479cc..310933f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -3,3 +3,4 @@ pub mod problem_02; pub mod problem_03; pub mod problem_04; pub mod problem_05; +pub mod problem_06; diff --git a/src/main.rs b/src/main.rs index 808995c..22bcd42 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,6 +3,7 @@ use taoc2022::problem_02; use taoc2022::problem_03; use taoc2022::problem_04; use taoc2022::problem_05; +use taoc2022::problem_06; fn main() { println!("== Problem 01 =="); @@ -20,4 +21,7 @@ fn main() { println!("== Problem 05 =="); println!("= a: {}", problem_05::solution_a()); println!("= b: {}", problem_05::solution_b()); + println!("== Problem 06 =="); + println!("= a: {}", problem_06::solution_a()); + println!("= b: {}", problem_06::solution_b()); } diff --git a/src/problem_06/description.txt b/src/problem_06/description.txt new file mode 100644 index 0000000..d04fa37 --- /dev/null +++ b/src/problem_06/description.txt @@ -0,0 +1,53 @@ +--- Day 6: Tuning Trouble --- + +The preparations are finally complete; you and the Elves leave camp on foot and begin to make your way toward the star fruit grove. + +As you move through the dense undergrowth, one of the Elves gives you a handheld device. He says that it has many fancy features, but the most important one to set up right now is the communication system. + +However, because he's heard you have significant experience dealing with signal-based systems, he convinced the other Elves that it would be okay to give you their one malfunctioning device - surely you'll have no problem fixing it. + +As if inspired by comedic timing, the device emits a few colorful sparks. + +To be able to communicate with the Elves, the device needs to lock on to their signal. The signal is a series of seemingly-random characters that the device receives one at a time. + +To fix the communication system, you need to add a subroutine to the device that detects a start-of-packet marker in the datastream. In the protocol being used by the Elves, the start of a packet is indicated by a sequence of four characters that are all different. + +The device will send your subroutine a datastream buffer (your puzzle input); your subroutine needs to identify the first position where the four most recently received characters were all different. Specifically, it needs to report the number of characters from the beginning of the buffer to the end of the first such four-character marker. + +For example, suppose you receive the following datastream buffer: + +mjqjpqmgbljsphdztnvjfqwrcgsmlb + +After the first three characters (mjq) have been received, there haven't been enough characters received yet to find the marker. The first time a marker could occur is after the fourth character is received, making the most recent four characters mjqj. Because j is repeated, this isn't a marker. + +The first time a marker appears is after the seventh character arrives. Once it does, the last four characters received are jpqm, which are all different. In this case, your subroutine should report the value 7, because the first start-of-packet marker is complete after 7 characters have been processed. + +Here are a few more examples: + + bvwbjplbgvbhsrlpgdmjqwftvncz: first marker after character 5 + nppdvjthqldpwncqszvftbrmjlhg: first marker after character 6 + nznrnfrfntjfmvfwmzdfjlvtqnbhcprsg: first marker after character 10 + zcfzfwzzqfrljwzlrfnpqdbhtmscgvjw: first marker after character 11 + +How many characters need to be processed before the first start-of-packet marker is detected? + +Your puzzle answer was 1816. +--- Part Two --- + +Your device's communication system is correctly detecting packets, but still isn't working. It looks like it also needs to look for messages. + +A start-of-message marker is just like a start-of-packet marker, except it consists of 14 distinct characters rather than 4. + +Here are the first positions of start-of-message markers for all of the above examples: + + mjqjpqmgbljsphdztnvjfqwrcgsmlb: first marker after character 19 + bvwbjplbgvbhsrlpgdmjqwftvncz: first marker after character 23 + nppdvjthqldpwncqszvftbrmjlhg: first marker after character 23 + nznrnfrfntjfmvfwmzdfjlvtqnbhcprsg: first marker after character 29 + zcfzfwzzqfrljwzlrfnpqdbhtmscgvjw: first marker after character 26 + +How many characters need to be processed before the first start-of-message marker is detected? + +Your puzzle answer was 2625. + +Both parts of this puzzle are complete! They provide two gold stars: ** diff --git a/src/problem_06/input.txt b/src/problem_06/input.txt new file mode 100644 index 0000000..66a449a --- /dev/null +++ b/src/problem_06/input.txt @@ -0,0 +1 @@ +mnlnvlljqqccznnjtjljbllrtllwwpmmhjjbbzppnndmmsppdqqwvvstvssgmsggmlmttnvvfbbdsssnzzbssjrsjjpmpvmmcjjwsssndsslwsswtwnwrrslshhvzzsppffmpfmmfvfpfpsssqpqzpqqcjcjnjcnnzbzjzpzbpbnbwbcctvvhgvgsvvpwwvjjvqjjjdqqrmrmqmsqszqsqpsqslsddhbhcbhbchcvvjvjcjnccdbcdcrddldblbffhvffpvpzpvvmvfmmwhwqhqvhqhmmpdmmlbmbgmbbrqqpmqqcvcmvvcncllptltvtdtbbqzzcggjgsjjvjsvvgmgffqhqqgpptspsffvdvbbhqhzzllvvjbvvbpppggfpgptgtvvzdvdzdgzgccmmphpmhppldlnlpnnhghhrrgwrwssnllmpllbvbvqvtvhtvvmnvvpgvgfvggtztpthhcfhfqfhhnhtnhhljhjppqjjffgjggrwrjjhphzhtztggwswnwzzvbzzmmtrtqtjqttwlwmmmmnddmnddwvvcllgrgfgzznwnsswjjhwwspsbbvzzqvzvbvcvmmtltnlnfnfnwwsvwwpswppjhjdhhmbbblfbfwffwvvjgghwhzhjzzrttwhwjhjchjhggdrgdgmmsjsfstftvtmtctggcwgcgzccgzgsgrgmmjhhqzzrmrttgtgfgcffvsfvvslsvvpqvvnjnrrwdwcwcnnhllwpwdpwdpdqqtwtftdftddppncpnccllqqrffpssgvsvtvmvssrbrhbbzggtssdsvddqfdfjfhjhdjhhncnddfpdfdmmrddncnvcnvccgvvhzvzwztwzwtzwtwqttrlrvrddztzrzcccgmmqgqjgqjgjqqspqqpjppbggchcqcpqqgbgdbbspbbrbhrhzhqhrqhhhtbhhvshspsvsggjdjwjwvjvdvwddjggmrrbnrrztthlllhlclbclbbhpbhphjhccdwccdbbjrbjjmrjrhhnlnjllltwltlmmlqlnqntqnntsnsqqvtvwvgwvwnncgcdctddnttfjfqfttrhrjhjqqcnnsmnmgmqgmgbblcctntrntnccnvvmpmjjvfjfrrbpbttsbttvnttmnnjdnndnzntnrnwrwcctllvhvqhhddmzztppphghphzzglzlnnfccrfffvvhllpspwssstwstsvttcrtccfssbccdjdqdfqddrbrqbbtllmmsfmfcmmzwzpznpnttjgtgbbdtdvdwwpmphpprsrjrbrqbqwwljlslrlrhhpchpcprcrtcrcfcfssndsspddcjjjfmfqfggmssnhsnhnpncnfnmffdrdjdhjjrgjrjgjqgjqqlmlljffbcfcrrrzwwftwtrrpgpprqqmrmhmwhwmmcrrhqrqwqppwjwggpdpgpvgvzvttqlljhhbvbhblhhcsssvmsmppcvpvrvzvbvtbtssplpgptgtnthhvwhvwwfvvfwwmtwmwfmfgmgnmnllgsgmsgswwhqhhhzqhhfwwnttmfmrrfnnpbpssvbsshqhqvhqqbmbpmbmqqjtjqjvjtjjhtttpzphpqqwqfqttqqhfhbfhfwwcpcpssdvvzhzwwqddjzdjdldlggvnvlnlbljlqjjmcczbccznnlnslswlwplpttvrvllfwflftllhclldhlhddbvvpvzpvpmmrccvgvdvqqjcqqwvvnjjlbbjwjrjhhlzhlhttljlcjjsnsgngrnntzzbsbmbsbrbdrdppjrjlrrjljqlqhqqnqsgdvhpgdhmnslqtjclmcfzrmgmlfnjbzznfgfprvwprwdbcgfcclmspgnzpbshwjbqvhzhrhswjzbfvnmcjtfvqbwmjpvfvctpmwsspdbtvfhfdfzjdpqnvslgmdvrnflzwzcnzmvzsvznwhpwtjwnqdgrrttmmdwzbbnwtllpbffrgtpjjjwltqrcbqcttdwnfjpmhdsbbpqmstjqchgjvfrmrbgqrlstnbdnzzzbzbsmsnnsssswmqhcbswtjhmcgnwmcclhzjqjzqcpbzgdzjgqzpqbmvvhtcznfrhdndswfvfhtfpdpszpjqrlwfdscvcngftwqmfttjtjrlbgcwvcjwsstqmcblmjzsgtgrqnqqvhzhvsphjmbcpfcznlcqldcvhlsvggbjngmhspwwqhlwstslvwmmbwqdmrgdvvnlstmjllhzscrhzjtmnsjfbndnlmzqbzgdgbcqchnbvwsftjtznnbsnvsgzpdzdqznjsslrlfnccdhwsljhczggvmgqswjltmrqqmwtbzmtdzhpjcvmwsscsdzpfnwlcrrdgzqqdmgwdlzvvvjcqsgpcwvrdnrstpcmgfjnjffbfmgzjthhllzrlsjtnqfppltbrlnqnjvqlvtpqvsbfgmmlcdzhgmzzqjwtqtzmpwwddbqrqnfzzpsjglsjddsslwwlrttzfzplmwsswlnvrvwwcgddjwcmvsjjbfgcfjmthfbpmcwjptchhnsmzttjqnwzdljffghhqdcwzwgbvfsmwqdbtblphdgcmbhprtbccjbzqrpvjdbnsmlwfntvjgptnshzmddwbhgwsnfrjbpqqwlsfdpnmmnnwhdmhzvjcmddbdnjzfzvffbgdqgwbggprcrbzwhvtzzgbhhcscrlmfgztfswjbsnwsmdfwlntwjzvlwhvlrfzszllmflmrsrcfnncvszvgdmmnvgrqnjhljcnrrhpdhffwmrsqfnbcpfdmmgppwjbjrwdfmpcrbznrjnbmssszhnlbpgmlczhhcdtgjqcbqrvzcbpgrftfhzdqthhspwnqqswntlpcmmqtcszngpggqvfjnmnprhdfjsngwrncjcmqdmjhpdlfnshpdlnlfpcnprwjgdvwwbvhvsbrfjtqsqjnvcpfdsrnfwmrrbtcvcqzflhdlbpcthzthdjzsrvwgbhjvhbtrngthfrszlvrbtnscsqlblcwlngslspcrhqzzdlzcdbhqdhthlpmdrntbhnqtwtzwpndbgphpsllbvgqjtmszdvjpgttzcmbwgrgdwmsbfgvgbbcmsnhvmnsbcsthsdwdqtghpdclfbglbdjgnnnwhmzzvnhbmgfbmvqwvwqhdswgtzslspmbmznnwdmjbzbddhzchtdzdzgwtlmlpmwrqvghpfwhvfjrtvmjwgjjnwdwnpdcqjdmcctjfcrdgpvczvnhlrbfmqgnrhmdwsrmmpqhvwgqgbqccpznpjfldwpntnvzgdfzljmtqwvfnrdsjsqgbvzjsczwwjggqtrpvwgqggwwhqggtgqfjmzsmjvdhdwqggbgnftpqqqlsfpflwrdpjwnhfdpchcgntjshgtwnwrnpsvwmplvqcltbgrcpflpgzbqfclghfnwjchnbgjnplgldmphdplvjrnrtzcmlftprsnmrjmnffpqjlvqlztbwprjwprrmmgzhjgdnhbfdrwjtvsvnbqhtfhbqgdrvcwlwfdbbcthgvttpvrwrqmpmrmvgpjzwlpvbqcvgccpgfddjbwhrvgqmjqzwgghllrtrblcpbttmcrgjsftlqhjfvqnbhmhbhngwnfqtgdttstzvmstrqcpfjrdgtsdbqqccqvbhpwhnpmpqgntfqszndjrmfhlqjqjbqvjtlfmrnnzzrtqlzzhjfqmmsmzvzrcplmfjpcmpfmpzbsbrmbnbnjqwjcfwnnwwrwzvvrsvhvrwnhlmwqjdztqthcwnwrlbjdflfsbplbwfmzqqnpwvzjbcfdgztpwttlrvhlfzzsfltpqwcpnzlsqgvwnqfvgclrfvssfcfmfvvjsndrhqdbrqfggfhjbvdvvmgpglqzwgjdmqtscjpfhgsbshghtmftrrhznttpzrzcsmrrvzdjmwtmbcbpqbsdmqzqdzrncwzmptltvdphsltfrhbbrdzbnbsqdhfvrgvmbgfvwblsjvfphlpzfvsllwnqjmbhngzzslcdmdzfgrgscbzggrzmbmwlzbnpzcvsbsfgdpnwzljsf diff --git a/src/problem_06/mod.rs b/src/problem_06/mod.rs new file mode 100644 index 0000000..b6bcdf6 --- /dev/null +++ b/src/problem_06/mod.rs @@ -0,0 +1,39 @@ +use std::collections::VecDeque; +use std::collections::HashSet; + +static INPUT: &str = include_str!("input.txt"); + +fn solve(input: &str, n: usize) -> i32 { + let mut position = 1; + let mut buffer = input.chars().take(n).collect::>(); + let mut datastream = input.chars(); + + loop { + let mut uniqs = HashSet::new(); + let pointer = datastream.next(); + match pointer { + None => panic!("no header found"), + Some(c) => { + buffer.pop_front(); + buffer.push_back(c); + let result = buffer.clone().into_iter().all( + |x| uniqs.insert(x.clone()) + ); + if result { + return position; + } else { + position += 1; + continue; + } + } + } + } +} + +pub fn solution_a() -> i32 { + solve(INPUT, 4) +} + +pub fn solution_b() -> i32 { + solve(INPUT, 14) +} From 454e468d93f4557463f41111eca349d5fa60c2f8 Mon Sep 17 00:00:00 2001 From: Thorn Avery Date: Tue, 13 Dec 2022 15:08:10 +1100 Subject: [PATCH 10/10] forgot to run checks on day 6 :( --- src/problem_06/mod.rs | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/problem_06/mod.rs b/src/problem_06/mod.rs index b6bcdf6..1ec49db 100644 --- a/src/problem_06/mod.rs +++ b/src/problem_06/mod.rs @@ -1,5 +1,5 @@ -use std::collections::VecDeque; use std::collections::HashSet; +use std::collections::VecDeque; static INPUT: &str = include_str!("input.txt"); @@ -16,9 +16,7 @@ fn solve(input: &str, n: usize) -> i32 { Some(c) => { buffer.pop_front(); buffer.push_back(c); - let result = buffer.clone().into_iter().all( - |x| uniqs.insert(x.clone()) - ); + let result = buffer.clone().into_iter().all(|x| uniqs.insert(x)); if result { return position; } else {