-
Notifications
You must be signed in to change notification settings - Fork 0
/
day_6.rs
135 lines (106 loc) · 4.95 KB
/
day_6.rs
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
// --- Day 6: Tuning Trouble ---
// The preparations are finally complete; you and the Elves leave camp on foot
// and begin to make your way toward the star fruit grove.
// As you move through the dense undergrowth, one of the Elves gives you a
// handheld device. He says that it has many fancy features, but the most
// important one to set up right now is the communication system.
// However, because he's heard you have significant experience dealing with
// signal-based systems, he convinced the other Elves that it would be okay to
// give you their one malfunctioning device - surely you'll have no problem
// fixing it.
// As if inspired by comedic timing, the device emits a few colorful sparks.
// To be able to communicate with the Elves, the device needs to lock on to
// their signal. The signal is a series of seemingly-random characters that the
// device receives one at a time.
// To fix the communication system, you need to add a subroutine to the device
// that detects a start-of-packet marker in the datastream. In the protocol
// being used by the Elves, the start of a packet is indicated by a sequence of
// four characters that are all different.
// The device will send your subroutine a datastream buffer (your puzzle input);
// your subroutine needs to identify the first position where the four most
// recently received characters were all different. Specifically, it needs to
// report the number of characters from the beginning of the buffer to the end
// of the first such four-character marker.
// For example, suppose you receive the following datastream buffer:
// mjqjpqmgbljsphdztnvjfqwrcgsmlb
// After the first three characters (mjq) have been received, there haven't been
// enough characters received yet to find the marker. The first time a marker
// could occur is after the fourth character is received, making the most recent
// four characters mjqj. Because j is repeated, this isn't a marker.
// The first time a marker appears is after the seventh character arrives. Once
// it does, the last four characters received are jpqm, which are all different.
// In this case, your subroutine should report the value 7, because the first
// start-of-packet marker is complete after 7 characters have been processed.
// Here are a few more examples:
// bvwbjplbgvbhsrlpgdmjqwftvncz: first marker after character 5
// nppdvjthqldpwncqszvftbrmjlhg: first marker after character 6
// nznrnfrfntjfmvfwmzdfjlvtqnbhcprsg: first marker after character 10
// zcfzfwzzqfrljwzlrfnpqdbhtmscgvjw: first marker after character 11
// How many characters need to be processed before the first start-of-packet
// marker is detected?
// --- Part Two ---
// Your device's communication system is correctly detecting packets, but still
// isn't working. It looks like it also needs to look for messages.
// A start-of-message marker is just like a start-of-packet marker, except it
// consists of 14 distinct characters rather than 4.
// Here are the first positions of start-of-message markers for all of the above
// examples:
// mjqjpqmgbljsphdztnvjfqwrcgsmlb: first marker after character 19
// bvwbjplbgvbhsrlpgdmjqwftvncz: first marker after character 23
// nppdvjthqldpwncqszvftbrmjlhg: first marker after character 23
// nznrnfrfntjfmvfwmzdfjlvtqnbhcprsg: first marker after character 29
// zcfzfwzzqfrljwzlrfnpqdbhtmscgvjw: first marker after character 26
// How many characters need to be processed before the first start-of-message
// marker is detected?
use std::collections::HashSet;
pub fn part_1(input: &str) -> Vec<usize> {
let mut outputs = vec![];
for line in input.lines() {
for (idx, c) in line.as_bytes().windows(4).enumerate() {
let set = c.iter().copied().collect::<HashSet<_>>();
if set.len() == c.len() {
outputs.push(idx + c.len());
break;
}
}
}
outputs
}
pub fn part_2(input: &str) -> Vec<usize> {
let mut outputs = vec![];
for line in input.lines() {
for (idx, c) in line.as_bytes().windows(14).enumerate() {
let set = c.iter().copied().collect::<HashSet<_>>();
if set.len() == c.len() {
outputs.push(idx + c.len());
break;
}
}
}
outputs
}
#[cfg(test)]
pub mod tests {
use crate::day_6::{part_1, part_2};
const INPUTS: &str = r#"mjqjpqmgbljsphdztnvjfqwrcgsmlb
bvwbjplbgvbhsrlpgdmjqwftvncz
nppdvjthqldpwncqszvftbrmjlhg
nznrnfrfntjfmvfwmzdfjlvtqnbhcprsg
zcfzfwzzqfrljwzlrfnpqdbhtmscgvjw"#;
#[test]
pub fn test_day_6_example_part1() {
assert_eq!(part_1(INPUTS), vec![7, 5, 6, 10, 11]);
}
#[test]
pub fn test_day_6_part1() {
assert_eq!(part_1(include_str!("input/day_6.txt")), vec![1275]);
}
#[test]
pub fn test_day_6_example_part2() {
assert_eq!(part_2(INPUTS), vec![19, 23, 23, 29, 26]);
}
#[test]
pub fn test_day_6_part2() {
assert_eq!(part_2(include_str!("input/day_6.txt")), vec![3605]);
}
}