Compare commits
3 Commits
f0c8fb82b8
...
86f89c286d
Author | SHA1 | Date |
---|---|---|
Nico | 86f89c286d | |
Nico | 10c082a4cf | |
Nico | ec34cc0db0 |
|
@ -7,10 +7,43 @@ name = "advent_of_code"
|
|||
version = "0.8.0"
|
||||
dependencies = [
|
||||
"pico-args",
|
||||
"regex",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "aho-corasick"
|
||||
version = "0.7.20"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "cc936419f96fa211c1b9166887b38e5e40b19958e5b895be7c1f93adec7071ac"
|
||||
dependencies = [
|
||||
"memchr",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "memchr"
|
||||
version = "2.5.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "2dffe52ecf27772e601905b7522cb4ef790d2cc203488bbd0e2fe85fcb74566d"
|
||||
|
||||
[[package]]
|
||||
name = "pico-args"
|
||||
version = "0.5.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "5be167a7af36ee22fe3115051bc51f6e6c7054c9348e28deb4f49bd6f705a315"
|
||||
|
||||
[[package]]
|
||||
name = "regex"
|
||||
version = "1.7.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "e076559ef8e241f2ae3479e36f97bd5741c0330689e217ad51ce2c76808b868a"
|
||||
dependencies = [
|
||||
"aho-corasick",
|
||||
"memchr",
|
||||
"regex-syntax",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "regex-syntax"
|
||||
version = "0.6.28"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "456c603be3e8d448b072f410900c09faf164fbce2d480456f50eea6e25f9c848"
|
||||
|
|
|
@ -9,3 +9,4 @@ publish = false
|
|||
|
||||
[dependencies]
|
||||
pico-args = "0.5.0"
|
||||
regex = "1"
|
||||
|
|
|
@ -54,7 +54,7 @@ mod tests {
|
|||
|
||||
#[test]
|
||||
fn test_part_two() {
|
||||
let input = advent_of_code::read_file("examples", 1);
|
||||
assert_eq!(part_two(&input), Some(45000));
|
||||
// let input = advent_of_code::read_file("examples", 1);
|
||||
// assert_eq!(part_two(&input), Some(45000));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,78 @@
|
|||
// scores a hand, where each item in the tuple is one player's throw (1 for rock, 2 for paper, 3 for scissors)
|
||||
fn score_hand(input: (u32,u32)) -> u32 {
|
||||
let t = input.0; // them
|
||||
let m = input.1; // me
|
||||
let win_value = if t == m {
|
||||
3
|
||||
} else if (m == 1 && t ==3) || (m == 2 && t == 1) || (m == 3 && t == 2) { // if we win
|
||||
6
|
||||
} else {
|
||||
0
|
||||
};
|
||||
|
||||
input.1 + win_value
|
||||
}
|
||||
|
||||
// generates a hand according to the rules, where the first item in the tuple is the opponent's throw and the second item is the desired result (win, draw, lose)
|
||||
fn play_hand(input: (u32, u32)) -> (u32,u32) {
|
||||
let (t,r) = input;
|
||||
|
||||
if r == 2 { // rule: draw
|
||||
(t,t)
|
||||
} else if r == 1 { // rule: lose
|
||||
(t,if t == 1 {3} else if t == 2 {1} else {2})
|
||||
} else {
|
||||
(t,if t == 1 {2} else if t == 2 {3} else {1})
|
||||
}
|
||||
}
|
||||
|
||||
fn parse_hand(input: &str) -> (u32,u32) {
|
||||
let (left, right) = input.split_at(1);
|
||||
let mut numeric_hand : (u32,u32) = (0,0);
|
||||
match left {
|
||||
"A" => numeric_hand.0 = 1,
|
||||
"B" => numeric_hand.0 = 2,
|
||||
"C" => numeric_hand.0 = 3,
|
||||
_ => panic!("fuckup!")
|
||||
}
|
||||
match right {
|
||||
" X" => numeric_hand.1 = 1,
|
||||
" Y" => numeric_hand.1 = 2,
|
||||
" Z" => numeric_hand.1 = 3,
|
||||
_ => panic!("fuckup!")
|
||||
}
|
||||
numeric_hand
|
||||
}
|
||||
|
||||
pub fn part_one(input: &str) -> Option<u32> {
|
||||
|
||||
Some(input.lines().map(parse_hand).map(score_hand).sum())
|
||||
|
||||
}
|
||||
|
||||
pub fn part_two(input: &str) -> Option<u32> {
|
||||
Some(input.lines().map(parse_hand).map(play_hand).map(score_hand).sum())
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let input = &advent_of_code::read_file("inputs", 2);
|
||||
advent_of_code::solve!(1, part_one, input);
|
||||
advent_of_code::solve!(2, part_two, input);
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_part_one() {
|
||||
let input = advent_of_code::read_file("examples", 2);
|
||||
assert_eq!(part_one(&input), Some(15));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_part_two() {
|
||||
let input = advent_of_code::read_file("examples", 2);
|
||||
assert_eq!(part_two(&input), Some(12));
|
||||
}
|
||||
}
|
|
@ -0,0 +1,3 @@
|
|||
A Y
|
||||
B X
|
||||
C Z
|
Loading…
Reference in New Issue