|
1 | 1 | //! # Internet Protocol Version 7
|
2 | 2 | //!
|
3 |
| -//! It's faster to treat the entire input as one big stream, using line breaks to increment |
4 |
| -//! the count if an address is valid. |
5 |
| -//! |
6 | 3 | //! For part two there are at most 26 * 26 = 676 possible ABA or BAB sequences so we can use
|
7 | 4 | //! a fixed size array to keep track of which ones we've seen for the current address so far.
|
8 |
| -pub fn parse(input: &str) -> &[u8] { |
9 |
| - input.as_bytes() |
| 5 | +pub fn parse(input: &str) -> Vec<&[u8]> { |
| 6 | + input.lines().map(str::as_bytes).collect() |
10 | 7 | }
|
11 | 8 |
|
12 |
| -pub fn part1(input: &[u8]) -> usize { |
13 |
| - let mut count = 0; |
14 |
| - let mut inside = false; |
15 |
| - let mut positive = false; |
16 |
| - let mut negative = false; |
| 9 | +pub fn part1(input: &[&[u8]]) -> usize { |
| 10 | + input |
| 11 | + .iter() |
| 12 | + .filter(|line| { |
| 13 | + let mut has_abba = false; |
| 14 | + let mut inside_brackets = false; |
17 | 15 |
|
18 |
| - for w in input.windows(4) { |
19 |
| - if w[0].is_ascii_lowercase() { |
20 |
| - if w[0] == w[3] && w[1] == w[2] && w[0] != w[1] { |
21 |
| - if inside { |
22 |
| - negative = true; |
| 16 | + for w in line.windows(4) { |
| 17 | + if w[0].is_ascii_lowercase() { |
| 18 | + if w[0] == w[3] && w[1] == w[2] && w[0] != w[1] { |
| 19 | + if inside_brackets { |
| 20 | + return false; |
| 21 | + } |
| 22 | + has_abba = true; |
| 23 | + } |
23 | 24 | } else {
|
24 |
| - positive = true; |
| 25 | + inside_brackets = w[0] == b'['; |
25 | 26 | }
|
26 | 27 | }
|
27 |
| - } else if w[0] == b'[' { |
28 |
| - inside = true; |
29 |
| - } else if w[0] == b']' { |
30 |
| - inside = false; |
31 |
| - } else { |
32 |
| - // Next line |
33 |
| - if positive && !negative { |
34 |
| - count += 1; |
35 |
| - } |
36 |
| - positive = false; |
37 |
| - negative = false; |
38 |
| - } |
39 |
| - } |
40 | 28 |
|
41 |
| - if positive && !negative { count + 1 } else { count } |
| 29 | + has_abba |
| 30 | + }) |
| 31 | + .count() |
42 | 32 | }
|
43 | 33 |
|
44 |
| -pub fn part2(input: &[u8]) -> usize { |
45 |
| - let mut count = 0; |
46 |
| - let mut version = 0; |
47 |
| - let mut inside = false; |
48 |
| - let mut positive = false; |
| 34 | +pub fn part2(input: &[&[u8]]) -> usize { |
49 | 35 | let mut aba = [usize::MAX; 676];
|
50 | 36 | let mut bab = [usize::MAX; 676];
|
51 | 37 |
|
52 |
| - for w in input.windows(3) { |
53 |
| - if w[1].is_ascii_lowercase() { |
54 |
| - if w[0] == w[2] && w[0] != w[1] { |
55 |
| - let first = (w[0] - b'a') as usize; |
56 |
| - let second = (w[1] - b'a') as usize; |
| 38 | + input |
| 39 | + .iter() |
| 40 | + .enumerate() |
| 41 | + .filter(|&(version, line)| { |
| 42 | + let mut inside_brackets = false; |
57 | 43 |
|
58 |
| - if inside { |
59 |
| - // Reverse the order of letters |
60 |
| - let index = 26 * second + first; |
61 |
| - bab[index] = version; |
62 |
| - positive |= aba[index] == version; |
| 44 | + for w in line.windows(3) { |
| 45 | + if w[0].is_ascii_lowercase() { |
| 46 | + if w[0] == w[2] && w[0] != w[1] && w[1].is_ascii_lowercase() { |
| 47 | + let first = (w[0] - b'a') as usize; |
| 48 | + let second = (w[1] - b'a') as usize; |
| 49 | + |
| 50 | + if inside_brackets { |
| 51 | + // Reverse the order of letters |
| 52 | + let index = 26 * second + first; |
| 53 | + bab[index] = version; |
| 54 | + if aba[index] == version { |
| 55 | + return true; |
| 56 | + } |
| 57 | + } else { |
| 58 | + let index = 26 * first + second; |
| 59 | + aba[index] = version; |
| 60 | + if bab[index] == version { |
| 61 | + return true; |
| 62 | + } |
| 63 | + } |
| 64 | + } |
63 | 65 | } else {
|
64 |
| - let index = 26 * first + second; |
65 |
| - aba[index] = version; |
66 |
| - positive |= bab[index] == version; |
| 66 | + inside_brackets = w[0] == b'['; |
67 | 67 | }
|
68 | 68 | }
|
69 |
| - } else if w[1] == b'[' { |
70 |
| - inside = true; |
71 |
| - } else if w[1] == b']' { |
72 |
| - inside = false; |
73 |
| - } else { |
74 |
| - // Next line |
75 |
| - if positive { |
76 |
| - count += 1; |
77 |
| - } |
78 |
| - version += 1; |
79 |
| - positive = false; |
80 |
| - } |
81 |
| - } |
82 | 69 |
|
83 |
| - if positive { count + 1 } else { count } |
| 70 | + false |
| 71 | + }) |
| 72 | + .count() |
84 | 73 | }
|
0 commit comments