-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday9.rs
158 lines (133 loc) · 3.99 KB
/
day9.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
//! [Day 9: Explosives in Cyberspace](https://adventofcode.com/2016/day/9)
pub fn main() {
let args = aoc::parse_args();
args.run(solve);
}
/// # Panics
#[must_use]
pub fn solve(data: &str) -> (u64, u64) {
(part1(data), part2(data))
}
/// Do part 1 of the puzzle
fn part1(data: &str) -> u64 {
data.lines().fold(0, |acc, line| acc + expand_v1(line).len() as u64)
}
/// Do part 2 of the puzzle
fn part2(data: &str) -> u64 {
data.lines().fold(0, |acc, line| acc + expand_v2(line))
}
/// ``expand_v1`` returns the expanded string according to the format v1.
fn expand_v1(s: &str) -> String {
let mut new_s = String::new();
let mut chars = s.chars();
while let Some(c) = chars.next() {
if c == '(' {
// do expansion
let take = chars
.by_ref()
.take_while(|c| *c != 'x')
.collect::<String>()
.parse::<usize>()
.unwrap();
let repeat = chars
.by_ref()
.take_while(|c| *c != ')')
.collect::<String>()
.parse::<usize>()
.unwrap();
let expanded = chars.by_ref().take(take).collect::<String>().repeat(repeat);
new_s.push_str(expanded.as_str());
} else {
// just add the character
new_s.push(c);
}
}
new_s
}
/// ``expand_v2`` returns the length of expanded string according to the format v2.
fn expand_v2(s: &str) -> u64 {
expand(s, 2)
}
/// ``expand`` returns the length of the expanded string (v1 or v2).
fn expand(s: &str, version: u8) -> u64 {
let mut new_len = 0;
let mut chars = s.chars();
while let Some(c) = chars.next() {
if c == '(' {
// do expansion
let take = chars
.by_ref()
.take_while(|c| *c != 'x')
.collect::<String>()
.parse::<usize>()
.unwrap();
let repeat = chars
.by_ref()
.take_while(|c| *c != ')')
.collect::<String>()
.parse::<u64>()
.unwrap();
let taken = chars.by_ref().take(take);
let count = if version == 1 {
taken.count() as u64
} else {
expand(taken.collect::<String>().as_str(), version)
};
new_len += repeat * count;
} else {
// just add the character
new_len += 1;
}
}
new_len
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn test_expand_v1() {
fn test_v1(s: &str, expected: &str) {
assert_eq!(expand_v1(s), expected);
assert_eq!(expand(s, 1), expected.len() as u64);
}
test_v1("ADVENT", "ADVENT");
test_v1("A(1x5)BC", "ABBBBBC");
test_v1("(3x3)XYZ", "XYZXYZXYZ");
test_v1("A(2x2)BCD(2x2)EFG", "ABCBCDEFEFG");
test_v1("(6x1)(1x3)A", "(1x3)A");
test_v1("X(8x2)(3x3)ABCY", "X(3x3)ABC(3x3)ABCY");
}
#[test]
fn test_part1() {
let data = "ADVENT
A(1x5)BC
(3x3)XYZ
A(2x2)BCD(2x2)EFG
(6x1)(1x3)A
X(8x2)(3x3)ABCY";
assert_eq!(part1(data), 6 + 7 + 9 + 11 + 6 + 18);
}
#[test]
fn test_expand() {
assert_eq!(expand("X(8x2)(3x3)ABCY", 1), 18);
assert_eq!(expand("X(8x2)(3x3)ABCY", 2), 20);
}
#[test]
fn test_expand_v2() {
assert_eq!(expand_v2("(3x3)XYZ"), 9); // XYZXYZXYZ
assert_eq!(expand_v2("X(8x2)(3x3)ABCY"), 20); // XABCABCABCABCABCABCY
assert_eq!(expand_v2("(27x12)(20x12)(13x14)(7x10)(1x12)A"), 241_920);
assert_eq!(
expand_v2("(25x3)(3x3)ABC(2x3)XY(5x2)PQRSTX(18x9)(3x2)TWO(5x7)SEVEN"),
445
);
}
#[test]
fn test_part2() {
let data = "(3x3)XYZ
X(8x2)(3x3)ABCY
(27x12)(20x12)(13x14)(7x10)(1x12)A
(25x3)(3x3)ABC(2x3)XY(5x2)PQRSTX(18x9)(3x2)TWO(5x7)SEVEN";
assert_eq!(part2(data), 9 + 20 + 241_920 + 445);
}
}