From c4266bb16cfdb353e8b4c71ca5579a76dab6c17c Mon Sep 17 00:00:00 2001 From: george Date: Fri, 2 Dec 2022 17:29:15 +0000 Subject: [PATCH] day 2 --- .gitignore | 1 + day02/Cargo.lock | 7 +++++ day02/Cargo.toml | 8 +++++ day02/src/main.rs | 75 +++++++++++++++++++++++++++++++++++++++++++++++ fetchinput.sh | 2 +- 5 files changed, 92 insertions(+), 1 deletion(-) create mode 100644 day02/Cargo.lock create mode 100644 day02/Cargo.toml create mode 100644 day02/src/main.rs diff --git a/.gitignore b/.gitignore index 87e021d..f5d7f64 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ target/ input testinput +main diff --git a/day02/Cargo.lock b/day02/Cargo.lock new file mode 100644 index 0000000..52d399b --- /dev/null +++ b/day02/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "day02" +version = "0.1.0" diff --git a/day02/Cargo.toml b/day02/Cargo.toml new file mode 100644 index 0000000..843335d --- /dev/null +++ b/day02/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "day02" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/day02/src/main.rs b/day02/src/main.rs new file mode 100644 index 0000000..4785271 --- /dev/null +++ b/day02/src/main.rs @@ -0,0 +1,75 @@ +use std::io::{self, BufRead}; + +enum Outcome { + Won = 6, + Drew = 3, + Lost = 0 +} + +fn main() { + let lines = io::stdin().lock().lines().map(|l| l.unwrap()); + let mut p1_sum = 0; + let mut p2_sum = 0; + + for line in lines { + let (opponent, player) = line.split_once(' ').unwrap(); + p1_sum += play_score(opponent, player); + p2_sum += result_score(opponent, player); + } + println!("P1: {}, P2: {}", p1_sum, p2_sum); +} + +fn player_win_score(player: &str) -> usize { + match player { + "X" => 1, + "Y" => 2, + "Z" => 3, + _ => panic!("at the disco") + } +} + +fn play_score(opponent: &str, player: &str) -> usize { + let outcome = match (player, opponent) { + ("X", "C") | ("Y", "A") | ("Z", "B") => Outcome::Won, + ("X", "A") | ("Y", "B") | ("Z", "C") => Outcome::Drew, + ("X", "B") | ("Y", "C") | ("Z", "A") => Outcome::Lost, + _ => panic!("on the streets of london") + }; + player_win_score(player) + outcome as usize +} + +fn player_hand_for_outcome<'a>(outcome: &Outcome, opponent: &str) -> &'a str { + match outcome { + Outcome::Won => match opponent { + "A" => "Y", + "B" => "Z", + "C" => "X", + _ => panic!("on the streets of birmingham") + } + Outcome::Drew => match opponent { + "A" => "X", + "B" => "Y", + "C" => "Z", + _ => panic!("on the streets of carlisle") + }, + Outcome::Lost => match opponent { + "A" => "Z", + "B" => "X", + "C" => "Y", + _ => panic!("dublin, dundee, humberside") + }, + } +} + +fn result_score(opponent: &str, result: &str) -> usize { + let outcome = match result { + "X" => Outcome::Lost, + "Y" => Outcome::Drew, + "Z" => Outcome::Won, + _ => panic!("the leeds side streets that you slip down") + }; + + let player = player_hand_for_outcome(&outcome, opponent); + + player_win_score(player) + outcome as usize +} diff --git a/fetchinput.sh b/fetchinput.sh index 5589903..97ae01f 100755 --- a/fetchinput.sh +++ b/fetchinput.sh @@ -6,4 +6,4 @@ DAY=$(printf "%01d" `date +%d`) ! [ "$1" = "" ] && printf "%d" $1 >/dev/null 2>&1 && DAY=$1 [ "$AOC_SESSION_COOKIE" = "" ] && echo "\$AOC_SESSION_COOKIE not set" && exit 1 -curl -i https://adventofcode.com/2022/day/$DAY/input --cookie "session=$AOC_SESSION_COOKIE" -o day`printf "%02d" $DAY`/input +curl -s https://adventofcode.com/2022/day/$DAY/input --cookie "session=$AOC_SESSION_COOKIE" -o day`printf "%02d" $DAY`/input