Added day 4 part 1
This commit is contained in:
parent
4126148778
commit
3c16932a91
1301
inputs/input.txt
1301
inputs/input.txt
File diff suppressed because it is too large
Load Diff
|
@ -1,6 +1,7 @@
|
|||
vJrwpWtwJgWrhcsFMMfFFhFp
|
||||
jqHRNqRjqzjGDLGLrsFMfFZSrLrFZsSL
|
||||
PmmdzqPrVvPwwTWBwg
|
||||
wMqvLMZHhHMvwLHjbvcjnnSBnvTQFn
|
||||
ttgJtRGJQctTZtZT
|
||||
CrZsJsPPZsGzwwsLwLmpwMDw
|
||||
2-4,6-8
|
||||
2-3,4-5
|
||||
5-7,7-9
|
||||
2-8,3-7
|
||||
6-6,4-6
|
||||
2-6,4-8
|
||||
|
||||
|
|
51
src/main.rs
51
src/main.rs
|
@ -6,13 +6,20 @@ const INPUTS: [&str; 2] = [
|
|||
include_str!("../inputs/input.txt"),
|
||||
];
|
||||
|
||||
fn parse(input: &'static str) -> Vec<(&str, &str, &str)> {
|
||||
let lines: Vec<&str> = input.trim().lines().collect();
|
||||
fn parse(input: &'static str) -> impl Iterator<Item = ((u64, u64), (u64, u64))> {
|
||||
input.trim().lines().map(|line| {
|
||||
let (a, b) = line.split_once(',').unwrap();
|
||||
|
||||
lines
|
||||
.chunks(3)
|
||||
.map(|chunk| (chunk[0], chunk[1], chunk[2]))
|
||||
.collect()
|
||||
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() {
|
||||
|
@ -24,42 +31,16 @@ fn main() {
|
|||
}
|
||||
}
|
||||
|
||||
fn solution(input: Vec<(&str, &str, &str)>) -> usize {
|
||||
fn solution(input: impl Iterator<Item = ((u64, u64), (u64, u64))>) -> usize {
|
||||
let mut score = 0;
|
||||
|
||||
for (a, b, c) in input.into_iter() {
|
||||
let ai = find_items(a);
|
||||
let bi = find_items(b);
|
||||
let ci = find_items(c);
|
||||
|
||||
for i in 0..128 {
|
||||
if ai[i] & bi[i] & ci[i] {
|
||||
let c = i as u8 as char;
|
||||
|
||||
let lscore = if ('a'..='z').contains(&c) {
|
||||
i - 97 + 1
|
||||
} else {
|
||||
i - 65 + 27
|
||||
};
|
||||
|
||||
score += lscore;
|
||||
}
|
||||
}
|
||||
for ((a0, a1), (b0, b1)) in input {
|
||||
score += ((a0 <= b0 && a1 >= b1) || (b0 <= a0 && b1 >= a1)) as usize;
|
||||
}
|
||||
|
||||
score
|
||||
}
|
||||
|
||||
fn find_items(ip: &str) -> [bool; 128] {
|
||||
let mut out = [false; 128];
|
||||
|
||||
for c in ip.bytes() {
|
||||
out[c as usize] = true;
|
||||
}
|
||||
|
||||
out
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn solution_bench(b: &mut test::Bencher) {
|
||||
b.iter(|| {
|
||||
|
|
Loading…
Reference in New Issue
Block a user