diff options
| author | Sebastiano Tronto <sebastiano@tronto.net> | 2025-06-26 09:47:23 +0200 |
|---|---|---|
| committer | Sebastiano Tronto <sebastiano@tronto.net> | 2025-06-26 23:37:23 +0200 |
| commit | fa26f694d8cf98272763952a698301ee9935c19e (patch) | |
| tree | 5af7a788fb8e9fa12b2b2fbea64250ed496a23c0 /2022/02 | |
| parent | f0159b7d36e2c81182f2d047a34bf883394894db (diff) | |
| download | aoc-fa26f694d8cf98272763952a698301ee9935c19e.tar.gz aoc-fa26f694d8cf98272763952a698301ee9935c19e.zip | |
Added 2022, first few problems
Diffstat (limited to '2022/02')
| -rw-r--r-- | 2022/02/a.rs | 13 | ||||
| -rw-r--r-- | 2022/02/b.rs | 13 | ||||
| -rw-r--r-- | 2022/02/common.rs | 10 |
3 files changed, 36 insertions, 0 deletions
diff --git a/2022/02/a.rs b/2022/02/a.rs new file mode 100644 index 0000000..1a49a6b --- /dev/null +++ b/2022/02/a.rs | |||
| @@ -0,0 +1,13 @@ | |||
| 1 | mod common; | ||
| 2 | use common::*; | ||
| 3 | |||
| 4 | pub fn main() { | ||
| 5 | let play_score = |s: &[u8]| (s[2] as i64) - ('X' as i64) + 1; | ||
| 6 | let win_score = |s: &[u8]| { | ||
| 7 | let other = s[0] as i64 - ('A' as i64); | ||
| 8 | let me = s[2] as i64 - ('X' as i64); | ||
| 9 | 3 * ((me - other + 4) % 3) | ||
| 10 | }; | ||
| 11 | let score = get_score(play_score, win_score); | ||
| 12 | println!("{score}"); | ||
| 13 | } | ||
diff --git a/2022/02/b.rs b/2022/02/b.rs new file mode 100644 index 0000000..9f2b580 --- /dev/null +++ b/2022/02/b.rs | |||
| @@ -0,0 +1,13 @@ | |||
| 1 | mod common; | ||
| 2 | use common::*; | ||
| 3 | |||
| 4 | pub fn main() { | ||
| 5 | let play_score = |s: &[u8]| { | ||
| 6 | let other = s[0] as i64 - ('A' as i64); | ||
| 7 | let w = s[2] as i64 - ('X' as i64); | ||
| 8 | (other + w + 2) % 3 + 1 | ||
| 9 | }; | ||
| 10 | let win_score = |s: &[u8]| 3 * ((s[2] as i64) - ('X' as i64)); | ||
| 11 | let score = get_score(play_score, win_score); | ||
| 12 | println!("{score}"); | ||
| 13 | } | ||
diff --git a/2022/02/common.rs b/2022/02/common.rs new file mode 100644 index 0000000..04581cf --- /dev/null +++ b/2022/02/common.rs | |||
| @@ -0,0 +1,10 @@ | |||
| 1 | pub fn get_score(play_score: fn(&[u8]) -> i64, win_score: fn(&[u8]) -> i64) -> i64 { | ||
| 2 | let mut score = 0; | ||
| 3 | let mut line = String::new(); | ||
| 4 | while std::io::stdin().read_line(&mut line).unwrap() > 0 { | ||
| 5 | let l = line.as_bytes(); | ||
| 6 | score += play_score(l) + win_score(l); | ||
| 7 | line.clear(); | ||
| 8 | } | ||
| 9 | score | ||
| 10 | } | ||
