summaryrefslogtreecommitdiff
path: root/aoc22/day2/src/main.rs
diff options
context:
space:
mode:
Diffstat (limited to 'aoc22/day2/src/main.rs')
-rw-r--r--aoc22/day2/src/main.rs64
1 files changed, 64 insertions, 0 deletions
diff --git a/aoc22/day2/src/main.rs b/aoc22/day2/src/main.rs
new file mode 100644
index 0000000..51936b8
--- /dev/null
+++ b/aoc22/day2/src/main.rs
@@ -0,0 +1,64 @@
+fn main() {
+ let input = lib::read_input(2);
+
+ part1(&input);
+ part2(&input);
+}
+
+fn part1(input: &str) {
+ let mut score = 0;
+
+ for line in input.split("\n") {
+ let mut split = line.split(" ").flat_map(|s| s.chars().next());
+ let Some(opp) = split.next() else { continue };
+ let me = split.next().unwrap();
+
+ let outcome_score = match (opp, me) {
+ ('A', 'Z') | ('B', 'X') | ('C', 'Y') => 0,
+ ('A', 'X') | ('B', 'Y') | ('C', 'Z') => 3,
+ ('A', 'Y') | ('B', 'Z') | ('C', 'X') => 6,
+ _ => unreachable!(),
+ };
+
+ let choice_score = match me {
+ 'X' => 1,
+ 'Y' => 2,
+ 'Z' => 3,
+ _ => unreachable!(),
+ };
+
+ score += outcome_score + choice_score;
+ }
+
+ println!("{}", score);
+}
+
+fn part2(input: &str) {
+ let mut score = 0;
+
+ for line in input.split("\n") {
+ let mut split = line.split(" ").flat_map(|s| s.chars().next());
+ let Some(opp) = split.next() else { continue };
+ let outcome = split.next().unwrap();
+
+ let opp = match opp {
+ 'A' => 0,
+ 'B' => 1,
+ 'C' => 2,
+ _ => unreachable!(),
+ };
+
+ let (me, outcome_score) = match outcome {
+ 'X' => ((opp - 1 + 3) % 3, 0),
+ 'Y' => (opp, 3),
+ 'Z' => ((opp + 1 + 3) % 3, 6),
+ _ => unreachable!(),
+ };
+
+ let choice_score = me + 1;
+
+ score += outcome_score + choice_score;
+ }
+
+ println!("{}", score);
+}