mirror of
https://github.com/cupcakearmy/advent-of-code.git
synced 2024-12-23 16:46:32 +00:00
Compare commits
3 Commits
796033f6b3
...
4a876daa71
Author | SHA1 | Date | |
---|---|---|---|
4a876daa71 | |||
2b5a930d3d | |||
9923bbe321 |
@ -9,37 +9,45 @@ mod tests {
|
||||
|
||||
#[bench]
|
||||
fn bench_a(b: &mut Bencher) {
|
||||
b.iter(|| part_a());
|
||||
b.iter(|| part_a(INPUT));
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_b(b: &mut Bencher) {
|
||||
b.iter(|| part_b());
|
||||
b.iter(|| part_b(INPUT));
|
||||
}
|
||||
}
|
||||
|
||||
fn part_a() {
|
||||
let result: u32 = include_str!("../input.txt")
|
||||
const INPUT: &str = include_str!("../input.txt");
|
||||
const TEST: &str = include_str!("../test.txt");
|
||||
|
||||
fn part_a(input: &str) {
|
||||
let result: u32 = input
|
||||
.trim()
|
||||
.split("\n\n")
|
||||
.map(|x| x.lines().map(|x| x.parse::<u32>().unwrap()).sum())
|
||||
.max()
|
||||
.unwrap();
|
||||
println!("A: {}", result);
|
||||
println!("{}", result);
|
||||
}
|
||||
|
||||
fn part_b() {
|
||||
let mut result = include_str!("../input.txt")
|
||||
fn part_b(input: &str) {
|
||||
let mut result = input
|
||||
.trim()
|
||||
.split("\n\n")
|
||||
.map(|x| x.lines().map(|x| x.parse::<u32>().unwrap()).sum::<u32>())
|
||||
.collect::<Vec<u32>>();
|
||||
result.sort_unstable();
|
||||
let total = result.into_iter().rev().take(3).sum::<u32>();
|
||||
println!("B: {}", total);
|
||||
println!("{}", total);
|
||||
}
|
||||
|
||||
fn main() {
|
||||
part_a();
|
||||
part_b();
|
||||
println!("Part A:");
|
||||
part_a(TEST);
|
||||
part_a(INPUT);
|
||||
|
||||
println!("\nPart B:");
|
||||
part_b(TEST);
|
||||
part_b(INPUT);
|
||||
}
|
||||
|
129
2022/02/rust/main.rs
Normal file
129
2022/02/rust/main.rs
Normal 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);
|
||||
}
|
@ -10,4 +10,8 @@ edition = "2021"
|
||||
name = "2022-01"
|
||||
path = "./2022/01/rust/main.rs"
|
||||
|
||||
[[bin]]
|
||||
name = "2022-02"
|
||||
path = "./2022/02/rust/main.rs"
|
||||
|
||||
# INJECT HERE
|
||||
|
@ -13,26 +13,34 @@ mod tests {
|
||||
|
||||
#[bench]
|
||||
fn bench_a(b: &mut Bencher) {
|
||||
b.iter(|| part_a());
|
||||
b.iter(|| part_a(INPUT));
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_b(b: &mut Bencher) {
|
||||
b.iter(|| part_b());
|
||||
b.iter(|| part_b(INPUT));
|
||||
}
|
||||
}
|
||||
|
||||
fn part_a() {
|
||||
let result = include_str!("../input.txt");
|
||||
println!("A: {}", result);
|
||||
const INPUT: &str = include_str!("../input.txt");
|
||||
const TEST: &str = include_str!("../test.txt");
|
||||
|
||||
fn part_a(input: &str) {
|
||||
let result = input.trim();
|
||||
println!("{}", result);
|
||||
}
|
||||
|
||||
fn part_b() {
|
||||
let result = include_str!("../input.txt");
|
||||
println!("A: {}", result);
|
||||
fn part_b(input: &str) {
|
||||
let result = input.trim();
|
||||
println!("{}", result);
|
||||
}
|
||||
|
||||
fn main() {
|
||||
part_a();
|
||||
part_b();
|
||||
println!("Part A:");
|
||||
part_a(TEST);
|
||||
// part_a(INPUT);
|
||||
|
||||
println!("\nPart B:");
|
||||
part_b(TEST);
|
||||
// part_b(INPUT);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user