day 2
This commit is contained in:
@@ -1,3 +1,4 @@
|
|||||||
target/
|
target/
|
||||||
input
|
input
|
||||||
testinput
|
testinput
|
||||||
|
main
|
||||||
|
|||||||
Generated
+7
@@ -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"
|
||||||
@@ -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]
|
||||||
@@ -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
|
||||||
|
}
|
||||||
+1
-1
@@ -6,4 +6,4 @@ DAY=$(printf "%01d" `date +%d`)
|
|||||||
! [ "$1" = "" ] && printf "%d" $1 >/dev/null 2>&1 && DAY=$1
|
! [ "$1" = "" ] && printf "%d" $1 >/dev/null 2>&1 && DAY=$1
|
||||||
|
|
||||||
[ "$AOC_SESSION_COOKIE" = "" ] && echo "\$AOC_SESSION_COOKIE not set" && exit 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
|
||||||
|
|||||||
Reference in New Issue
Block a user