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
|
2-4,6-8
|
||||||
jqHRNqRjqzjGDLGLrsFMfFZSrLrFZsSL
|
2-3,4-5
|
||||||
PmmdzqPrVvPwwTWBwg
|
5-7,7-9
|
||||||
wMqvLMZHhHMvwLHjbvcjnnSBnvTQFn
|
2-8,3-7
|
||||||
ttgJtRGJQctTZtZT
|
6-6,4-6
|
||||||
CrZsJsPPZsGzwwsLwLmpwMDw
|
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"),
|
include_str!("../inputs/input.txt"),
|
||||||
];
|
];
|
||||||
|
|
||||||
fn parse(input: &'static str) -> Vec<(&str, &str, &str)> {
|
fn parse(input: &'static str) -> impl Iterator<Item = ((u64, u64), (u64, u64))> {
|
||||||
let lines: Vec<&str> = input.trim().lines().collect();
|
input.trim().lines().map(|line| {
|
||||||
|
let (a, b) = line.split_once(',').unwrap();
|
||||||
|
|
||||||
lines
|
let (ai, aj) = a.split_once('-').unwrap();
|
||||||
.chunks(3)
|
let ai = ai.parse::<u64>().unwrap();
|
||||||
.map(|chunk| (chunk[0], chunk[1], chunk[2]))
|
let aj = aj.parse::<u64>().unwrap();
|
||||||
.collect()
|
|
||||||
|
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() {
|
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;
|
let mut score = 0;
|
||||||
|
|
||||||
for (a, b, c) in input.into_iter() {
|
for ((a0, a1), (b0, b1)) in input {
|
||||||
let ai = find_items(a);
|
score += ((a0 <= b0 && a1 >= b1) || (b0 <= a0 && b1 >= a1)) as usize;
|
||||||
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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
score
|
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]
|
#[bench]
|
||||||
fn solution_bench(b: &mut test::Bencher) {
|
fn solution_bench(b: &mut test::Bencher) {
|
||||||
b.iter(|| {
|
b.iter(|| {
|
||||||
|
|
Loading…
Reference in New Issue
Block a user