1
0
Fork 0

Refactoring, Added day4

This commit is contained in:
Ishan Jain 2022-12-12 01:02:39 +05:30
parent 8ca18b0c41
commit e72cca67c5
Signed by: ishan
GPG Key ID: 0506DB2A1CC75C27
4 changed files with 1129 additions and 0 deletions

48
src/day4/1.rs Normal file
View File

@ -0,0 +1,48 @@
#![feature(test)]
extern crate test;
const INPUTS: [&str; 2] = [include_str!("./sample.txt"), include_str!("./input.txt")];
fn parse(input: &'static str) -> impl Iterator<Item = ((u64, u64), (u64, u64))> {
input.trim().lines().map(|line| {
let (a, b) = line.split_once(',').unwrap();
let (ai, aj) = a.split_once('-').unwrap();
let ai = ai.parse::<u64>().unwrap();
let aj = aj.parse::<u64>().unwrap();
let (bi, bj) = b.split_once('-').unwrap();
let bi = bi.parse::<u64>().unwrap();
let bj = bj.parse::<u64>().unwrap();
((ai, aj), (bi, bj))
})
}
fn main() {
for input in INPUTS.iter() {
let output = parse(input);
let score = solution(output);
println!("{:?}", score);
}
}
fn solution(input: impl Iterator<Item = ((u64, u64), (u64, u64))>) -> usize {
let mut score = 0;
for ((a0, a1), (b0, b1)) in input {
score += ((a0 <= b0 && a1 >= b1) || (b0 <= a0 && b1 >= a1)) as usize;
}
score
}
#[bench]
fn solution_bench(b: &mut test::Bencher) {
b.iter(|| {
let input = parse(INPUTS[1]);
let result = solution(input);
test::black_box(result);
})
}

73
src/day4/2.rs Normal file
View File

@ -0,0 +1,73 @@
#![feature(test)]
extern crate test;
const INPUTS: [&[u8]; 2] = [
include_bytes!("./sample.txt"),
include_bytes!("./input.txt"),
];
fn parse(input: &[u8]) -> Vec<((u8, u8), (u8, u8))> {
input
.split(|&c| c == b'\n')
.filter(|line| !line.is_empty())
.map(|line| {
let mut line = line.splitn(2, |&c| c == b'-');
let e1 = line.next().unwrap();
let rest = line.next().unwrap();
let mut line = rest.split(|&c| c == b',');
let e2 = line.next().unwrap();
let rest = line.next().unwrap();
let mut line = rest.splitn(2, |&x| x == b'-');
let e3 = line.next().unwrap();
let e4 = line.next().unwrap();
(
(
e1.get(1)
.map_or(e1[0] - b'0', |c| ((e1[0] - b'0') * 10 + c - b'0')),
e2.get(1)
.map_or(e2[0] - b'0', |c| ((e2[0] - b'0') * 10 + c - b'0')),
),
(
e3.get(1)
.map_or(e3[0] - b'0', |c| ((e3[0] - b'0') * 10 + c - b'0')),
e4.get(1)
.map_or(e4[0] - b'0', |c| ((e4[0] - b'0') * 10 + c - b'0')),
),
)
})
.collect()
}
fn main() {
for input in INPUTS.iter() {
let output = parse(input);
let score = solution(output);
println!("{:?}", score);
}
}
fn solution(input: Vec<((u8, u8), (u8, u8))>) -> usize {
let mut score = 0;
for ((a0, a1), (b0, b1)) in input {
score += ((a0 <= b0 && a1 >= b1)
|| (b0 <= a0 && b1 >= a1)
|| (b0 >= a0 && b0 <= a1)
|| (a0 >= b0 && a0 <= b1)) as usize;
}
score
}
#[bench]
fn solution_bench(b: &mut test::Bencher) {
b.iter(|| {
let input = parse(INPUTS[1]);
let result = solution(input);
test::black_box(result);
})
}

1001
src/day4/input.txt Normal file

File diff suppressed because it is too large Load Diff

7
src/day4/sample.txt Normal file
View File

@ -0,0 +1,7 @@
2-4,6-8
2-3,4-5
5-7,7-9
2-8,3-7
6-6,4-6
2-6,4-8