From f5f940c52b38a5e17092f5896dc2fc13e8443478 Mon Sep 17 00:00:00 2001 From: j-james Date: Fri, 2 Dec 2022 18:39:47 -0800 Subject: Day Two --- 2022/nim/day02/src/main.nim | 42 ++++++++++++++++++++++++++++++++++++ 2022/rust/day02/Cargo.lock | 7 ++++++ 2022/rust/day02/Cargo.toml | 8 +++++++ 2022/rust/day02/src/main.rs | 52 +++++++++++++++++++++++++++++++++++++++++++++ util/main.rs | 2 +- 5 files changed, 110 insertions(+), 1 deletion(-) create mode 100644 2022/nim/day02/src/main.nim create mode 100644 2022/rust/day02/Cargo.lock create mode 100644 2022/rust/day02/Cargo.toml create mode 100644 2022/rust/day02/src/main.rs diff --git a/2022/nim/day02/src/main.nim b/2022/nim/day02/src/main.nim new file mode 100644 index 0000000..c26789a --- /dev/null +++ b/2022/nim/day02/src/main.nim @@ -0,0 +1,42 @@ +# Day Two: Rock Paper Scissors +import std/[os, strutils, sequtils] + +let input = paramStr(1).readFile().strip().split("\n").mapIt(it.split(" ")) + +var score = 0 +for line in input: + if line[1] == "X": + score += 1 + if line[0] == "A": score += 3 + if line[0] == "B": score += 0 + if line[0] == "C": score += 6 + if line[1] == "Y": + score += 2 + if line[0] == "A": score += 6 + if line[0] == "B": score += 3 + if line[0] == "C": score += 0 + if line[1] == "Z": + score += 3 + if line[0] == "A": score += 0 + if line[0] == "B": score += 6 + if line[0] == "C": score += 3 +echo score + +score = 0 +for line in input: + if line[1] == "X": + score += 0 + if line[0] == "A": score += 3 + if line[0] == "B": score += 1 + if line[0] == "C": score += 2 + if line[1] == "Y": + score += 3 + if line[0] == "A": score += 1 + if line[0] == "B": score += 2 + if line[0] == "C": score += 3 + if line[1] == "Z": + score += 6 + if line[0] == "A": score += 2 + if line[0] == "B": score += 3 + if line[0] == "C": score += 1 +echo score diff --git a/2022/rust/day02/Cargo.lock b/2022/rust/day02/Cargo.lock new file mode 100644 index 0000000..52d399b --- /dev/null +++ b/2022/rust/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/2022/rust/day02/Cargo.toml b/2022/rust/day02/Cargo.toml new file mode 100644 index 0000000..843335d --- /dev/null +++ b/2022/rust/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/2022/rust/day02/src/main.rs b/2022/rust/day02/src/main.rs new file mode 100644 index 0000000..fe1494f --- /dev/null +++ b/2022/rust/day02/src/main.rs @@ -0,0 +1,52 @@ +use std::env; +use std::fs; + +fn main() { + let args = env::args().nth(1).expect("missing input"); + let input = fs::read_to_string(args).unwrap(); + let moves = input.trim().split("\n").map(|x| { + let mut vec = x.split(" "); + return (vec.next().unwrap(), vec.next().unwrap()); + }).collect::>(); + + let mut sum = 0; + // note: without the iter "moves" is implicitly moved into the loop + for i in moves.iter() { + match i { + // (x, "X") => sum += 1, + ("A", "X") => sum += 3 + 1, + ("B", "X") => sum += 0 + 1, + ("C", "X") => sum += 6 + 1, + // (x, "Y") => sum += 2, + ("A", "Y") => sum += 6 + 2, + ("B", "Y") => sum += 3 + 2, + ("C", "Y") => sum += 0 + 2, + // (x, "Z") => sum += 3, + ("A", "Z") => sum += 0 + 3, + ("B", "Z") => sum += 6 + 3, + ("C", "Z") => sum += 3 + 3, + _ => () + } + } + println!("{}", sum); + + sum = 0; + for i in moves.iter() { + match i { + // (x, "X") => sum += 1, + ("A", "X") => sum += 3 + 0, + ("B", "X") => sum += 1 + 0, + ("C", "X") => sum += 2 + 0, + // (x, "Y") => sum += 2, + ("A", "Y") => sum += 1 + 3, + ("B", "Y") => sum += 2 + 3, + ("C", "Y") => sum += 3 + 3, + // (x, "Z") => sum += 3, + ("A", "Z") => sum += 2 + 6, + ("B", "Z") => sum += 3 + 6, + ("C", "Z") => sum += 1 + 6, + _ => () + } + } + println!("{}", sum); +} diff --git a/util/main.rs b/util/main.rs index 8b1acab..f6f1868 100644 --- a/util/main.rs +++ b/util/main.rs @@ -2,7 +2,7 @@ use std::env; use std::fs; fn main() { - let args = env::args().nth(1).unwrap(); + let args = env::args().nth(1).expect("missing input"); let input = fs::read_to_string(args).unwrap(); println!("{}", input); } -- cgit v1.2.3-70-g09d2