aboutsummaryrefslogtreecommitdiff
path: root/2022/03
diff options
context:
space:
mode:
authorSebastiano Tronto <sebastiano@tronto.net>2025-06-26 09:47:23 +0200
committerSebastiano Tronto <sebastiano@tronto.net>2025-06-26 23:37:23 +0200
commitfa26f694d8cf98272763952a698301ee9935c19e (patch)
tree5af7a788fb8e9fa12b2b2fbea64250ed496a23c0 /2022/03
parentf0159b7d36e2c81182f2d047a34bf883394894db (diff)
downloadaoc-fa26f694d8cf98272763952a698301ee9935c19e.tar.gz
aoc-fa26f694d8cf98272763952a698301ee9935c19e.zip
Added 2022, first few problems
Diffstat (limited to '2022/03')
-rw-r--r--2022/03/a.rs26
-rw-r--r--2022/03/b.rs29
-rw-r--r--2022/03/common.rs7
3 files changed, 62 insertions, 0 deletions
diff --git a/2022/03/a.rs b/2022/03/a.rs
new file mode 100644
index 0000000..568546c
--- /dev/null
+++ b/2022/03/a.rs
@@ -0,0 +1,26 @@
1mod common;
2use common::*;
3
4fn get_rep_val(rucksack: &[u8]) -> i64 {
5 let mut seen = [false; 256];
6 let mid = rucksack.len()/2;
7 for i in 0..mid {
8 seen[rucksack[i] as usize] = true;
9 }
10 for i in mid..rucksack.len() {
11 if seen[rucksack[i] as usize] {
12 return value(rucksack[i] as char);
13 }
14 }
15 panic!("Could not find repeated char");
16}
17
18fn main() {
19 let mut sum = 0;
20 let mut line = String::new();
21 while std::io::stdin().read_line(&mut line).unwrap() > 0 {
22 sum += get_rep_val(line.as_bytes());
23 line.clear();
24 }
25 println!("{sum}");
26}
diff --git a/2022/03/b.rs b/2022/03/b.rs
new file mode 100644
index 0000000..89da764
--- /dev/null
+++ b/2022/03/b.rs
@@ -0,0 +1,29 @@
1mod common;
2use common::*;
3
4fn get_rep_val(e: &[String; 3]) -> i64 {
5 let mut seen = [[false; 256]; 2];
6 for i in 0..2 {
7 for c in e[i].chars() {
8 seen[i][c as usize] = true;
9 }
10 }
11 for c in e[2].chars() {
12 if seen[0][c as usize] && seen[1][c as usize] {
13 return value(c);
14 }
15 }
16 panic!("Could not find repeated char");
17}
18
19fn main() {
20 let mut sum = 0;
21 let mut lines: [String; 3] = Default::default();
22 while std::io::stdin().read_line(&mut lines[0]).unwrap() > 0 {
23 let _ = std::io::stdin().read_line(&mut lines[1]);
24 let _ = std::io::stdin().read_line(&mut lines[2]);
25 sum += get_rep_val(&lines);
26 for l in &mut lines { l.clear(); }
27 }
28 println!("{sum}");
29}
diff --git a/2022/03/common.rs b/2022/03/common.rs
new file mode 100644
index 0000000..cf323f5
--- /dev/null
+++ b/2022/03/common.rs
@@ -0,0 +1,7 @@
1pub fn value(c: char) -> i64 {
2 match c {
3 'a'..='z' => (c as i64) - ('a' as i64) + 1,
4 'A'..='Z' => (c as i64) - ('A' as i64) + 27,
5 _ => panic!("Error: unexpected character '{}'", c)
6 }
7}

Generated with cgit - Back to sebastiano.tronto.net