From 9b62fa0d92b8371f1d1f18dc5de5a98c6f42a414 Mon Sep 17 00:00:00 2001 From: Joe Ardent Date: Thu, 1 Dec 2022 21:37:30 -0800 Subject: [PATCH] day 2 done --- 2022-aoc/src/d2.rs | 82 +++++++++++++++++++++++++++++++++++++++++++++ 2022-aoc/src/lib.rs | 1 + 2 files changed, 83 insertions(+) create mode 100644 2022-aoc/src/d2.rs diff --git a/2022-aoc/src/d2.rs b/2022-aoc/src/d2.rs new file mode 100644 index 0000000..37b9c1a --- /dev/null +++ b/2022-aoc/src/d2.rs @@ -0,0 +1,82 @@ +use aoc_runner_derive::{aoc as aoc_run, aoc_generator}; + +fn score(turn: &str) -> u32 { + let mv = turn.split(' ').collect::>()[1]; + let mut out = match mv { + "X" => 1, + "Y" => 2, + "Z" => 3, + _ => unreachable!(), + }; + + match turn { + "A Y" | "B Z" | "C X" => out += 6, + "A X" | "B Y" | "C Z" => out += 3, + _ => {} + } + out +} + +fn s2(turn: &str) -> u32 { + let wld = turn.split(' ').collect::>()[1]; + let mut out = match wld { + "X" => 0, + "Y" => 3, + "Z" => 6, + _ => unreachable!(), + }; + + let mv = turn.split(' ').collect::>()[0]; + + out += match mv { + // + "A" => match out { + 0 => 3, + 3 => 1, + 6 => 2, + _ => 0, + }, + "B" => match out { + 0 => 1, + 3 => 2, + 6 => 3, + _ => 0, + }, + "C" => match out { + 0 => 2, + 3 => 3, + 6 => 1, + _ => 0, + }, + _ => 0, + }; + out +} + +#[aoc_generator(day2, part1)] +fn parse_input(input: &str) -> Vec { + let mut out = Vec::new(); + for line in input.lines() { + out.push(score(line)); + } + out +} + +#[aoc_generator(day2, part2)] +fn parse_input2(input: &str) -> Vec { + let mut out = Vec::new(); + for line in input.lines() { + out.push(s2(line)); + } + out +} + +#[aoc_run(day2, part1)] +fn part1(scores: &[u32]) -> u32 { + scores.iter().sum() +} + +#[aoc_run(day2, part2)] +fn part2(scores: &[u32]) -> u32 { + scores.iter().sum() +} diff --git a/2022-aoc/src/lib.rs b/2022-aoc/src/lib.rs index 03fef30..08ff265 100644 --- a/2022-aoc/src/lib.rs +++ b/2022-aoc/src/lib.rs @@ -1,5 +1,6 @@ use aoc_runner_derive::aoc_lib; mod d1; +mod d2; aoc_lib! { year = 2022 }