1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
use crate::input::Input;
use std::ops::{BitAnd, BitOr};
type AnswersBitSet = u32;
fn person_answers_to_bit_set(answers: &str) -> AnswersBitSet {
answers
.bytes()
.map(|question_identifier| 1 << (question_identifier - b'a'))
.sum::<AnswersBitSet>()
}
pub fn solve(input: &mut Input) -> Result<AnswersBitSet, String> {
const GROUP_SEPARATOR: &str = "\n\n";
if !input
.text
.bytes()
.all(|b| matches!(b, b'a'..=b'z' | b'\r' | b'\n'))
{
return Err("Invalid input - only a-z, \r and \n expected".to_string());
}
let initial_bit_set = input.part_values(0, AnswersBitSet::MAX);
let bit_set_merger = if input.is_part_one() {
BitOr::bitor
} else {
BitAnd::bitand
};
let computer = |text: &str| {
Ok(text
.split(GROUP_SEPARATOR)
.map(|group_answers| {
group_answers
.lines()
.map(person_answers_to_bit_set)
.fold(initial_bit_set, bit_set_merger)
.count_ones()
})
.sum())
};
if input.text.contains('\r') {
computer(&input.text.replace('\r', ""))
} else {
computer(input.text)
}
}
#[test]
pub fn tests() {
use crate::{test_part_one, test_part_two};
test_part_one!("abc\n\nabc" => 6);
test_part_one!("abc\r\n\r\nabc" => 6);
let real_input = include_str!("day06_input.txt");
test_part_one!(real_input => 6686);
test_part_two!(real_input => 3476);
}
#[cfg(feature = "count-allocations")]
#[test]
pub fn no_memory_allocations() {
use crate::{test_part_one, test_part_two};
let real_input = include_str!("day06_input.txt");
let allocations = allocation_counter::count(|| {
test_part_one!(real_input => 6686);
test_part_two!(real_input => 3476);
});
assert_eq!(allocations, 0);
}