Compare commits

..

3 Commits

Author SHA1 Message Date
4a876daa71
refactor 2023-11-19 16:51:29 +01:00
2b5a930d3d
template 2023-11-19 16:51:08 +01:00
9923bbe321
day 2 2023-11-19 16:44:12 +01:00
4 changed files with 169 additions and 20 deletions

View File

@ -9,37 +9,45 @@ mod tests {
#[bench] #[bench]
fn bench_a(b: &mut Bencher) { fn bench_a(b: &mut Bencher) {
b.iter(|| part_a()); b.iter(|| part_a(INPUT));
} }
#[bench] #[bench]
fn bench_b(b: &mut Bencher) { fn bench_b(b: &mut Bencher) {
b.iter(|| part_b()); b.iter(|| part_b(INPUT));
} }
} }
fn part_a() { const INPUT: &str = include_str!("../input.txt");
let result: u32 = include_str!("../input.txt") const TEST: &str = include_str!("../test.txt");
fn part_a(input: &str) {
let result: u32 = input
.trim() .trim()
.split("\n\n") .split("\n\n")
.map(|x| x.lines().map(|x| x.parse::<u32>().unwrap()).sum()) .map(|x| x.lines().map(|x| x.parse::<u32>().unwrap()).sum())
.max() .max()
.unwrap(); .unwrap();
println!("A: {}", result); println!("{}", result);
} }
fn part_b() { fn part_b(input: &str) {
let mut result = include_str!("../input.txt") let mut result = input
.trim() .trim()
.split("\n\n") .split("\n\n")
.map(|x| x.lines().map(|x| x.parse::<u32>().unwrap()).sum::<u32>()) .map(|x| x.lines().map(|x| x.parse::<u32>().unwrap()).sum::<u32>())
.collect::<Vec<u32>>(); .collect::<Vec<u32>>();
result.sort_unstable(); result.sort_unstable();
let total = result.into_iter().rev().take(3).sum::<u32>(); let total = result.into_iter().rev().take(3).sum::<u32>();
println!("B: {}", total); println!("{}", total);
} }
fn main() { fn main() {
part_a(); println!("Part A:");
part_b(); part_a(TEST);
part_a(INPUT);
println!("\nPart B:");
part_b(TEST);
part_b(INPUT);
} }

129
2022/02/rust/main.rs Normal file
View File

@ -0,0 +1,129 @@
#![feature(test)]
extern crate test;
#[cfg(test)]
mod tests {
use super::*;
use test::Bencher;
#[bench]
fn bench_a(b: &mut Bencher) {
b.iter(|| part_a(INPUT));
}
#[bench]
fn bench_a_old(b: &mut Bencher) {
b.iter(|| part_a_old(INPUT));
}
#[bench]
fn bench_b(b: &mut Bencher) {
b.iter(|| part_b(INPUT));
}
}
const TEST: &str = include_str!("../test.txt");
const INPUT: &str = include_str!("../input.txt");
#[derive(Debug, Clone, Copy)]
enum Type {
Rock = 1,
Paper = 2,
Scissor = 3,
}
#[derive(Debug)]
struct Game(Type, Type);
impl Game {
fn score(&self) -> u32 {
let mut total = self.0 as u32;
// Result will be: 0=draw 1=win 2=loss
let result = (self.0 as i8 - self.1 as i8).rem_euclid(3) as u8;
total += match result {
1 => 6,
0 => 3,
_ => 0,
};
total
}
}
fn part_a_old(input: &str) {
let result = input
.trim()
.lines()
.map(|line| {
let opponent: Type = match line.chars().nth(0).unwrap() {
'A' => Type::Rock,
'B' => Type::Paper,
'C' => Type::Scissor,
_ => panic!("unkown input"),
};
let me = match line.chars().nth(2).unwrap() {
'X' => Type::Rock,
'Y' => Type::Paper,
'Z' => Type::Scissor,
_ => panic!("unkown input"),
};
let g: Game = Game(me, opponent);
g.score()
})
.sum::<u32>();
println!("A: {:?}", result);
}
fn part_a(input: &str) {
let result = input
.trim()
.lines()
.map(|line| {
let bytes = line.as_bytes();
let opponent = bytes[0] - b'A';
let me: u8 = bytes[2] - b'X';
let total = me + 1;
// Result will be: 0=draw 1=win 2=loss
let result = (me as i8 - opponent as i8).rem_euclid(3);
let score = match result {
1 => 6,
0 => 3,
_ => 0,
};
(total + score) as u16
})
.sum::<u16>();
println!("{}", result);
}
fn part_b(input: &str) {
let result = input
.trim()
.lines()
.map(|line| {
let bytes = line.as_bytes();
let opponent = bytes[0] - 'A' as u8;
let result: u8 = bytes[2] - 'X' as u8;
let total: u8 = match result {
1 => 3,
2 => 6,
_ => 0,
};
let me = (opponent + result + 2).rem_euclid(3);
(total + me + 1) as u16
})
.sum::<u16>();
println!("{}", result);
}
fn main() {
println!("Part A:");
part_a(TEST);
part_a(INPUT);
println!("\nPart B:");
part_b(TEST);
part_b(INPUT);
}

View File

@ -10,4 +10,8 @@ edition = "2021"
name = "2022-01" name = "2022-01"
path = "./2022/01/rust/main.rs" path = "./2022/01/rust/main.rs"
[[bin]]
name = "2022-02"
path = "./2022/02/rust/main.rs"
# INJECT HERE # INJECT HERE

View File

@ -13,26 +13,34 @@ mod tests {
#[bench] #[bench]
fn bench_a(b: &mut Bencher) { fn bench_a(b: &mut Bencher) {
b.iter(|| part_a()); b.iter(|| part_a(INPUT));
} }
#[bench] #[bench]
fn bench_b(b: &mut Bencher) { fn bench_b(b: &mut Bencher) {
b.iter(|| part_b()); b.iter(|| part_b(INPUT));
} }
} }
fn part_a() { const INPUT: &str = include_str!("../input.txt");
let result = include_str!("../input.txt"); const TEST: &str = include_str!("../test.txt");
println!("A: {}", result);
fn part_a(input: &str) {
let result = input.trim();
println!("{}", result);
} }
fn part_b() { fn part_b(input: &str) {
let result = include_str!("../input.txt"); let result = input.trim();
println!("A: {}", result); println!("{}", result);
} }
fn main() { fn main() {
part_a(); println!("Part A:");
part_b(); part_a(TEST);
// part_a(INPUT);
println!("\nPart B:");
part_b(TEST);
// part_b(INPUT);
} }