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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
|
#![allow(dead_code)]
use std::fs;
#[derive(Eq, PartialEq, Clone, Copy)]
struct Point {
x : i32,
y : i32,
}
struct Line {
p1 : Point,
p2 : Point,
}
impl Point {
fn distance_origin(&self) -> i32 {
self.x.abs() + self.y.abs()
}
}
impl Line {
fn horizontal(&self) -> bool {
self.p1.y == self.p2.y
}
fn length(&self) -> i32 {
match self.horizontal() {
true => (self.p1.x - self.p2.x).abs(),
false => (self.p1.y - self.p2.y).abs(),
}
}
fn contains_point(&self, point : &Point) -> bool {
if self.horizontal() && self.p1.y != point.y {
return false;
} else if !self.horizontal() && self.p1.x != point.x {
return false;
}
match self.horizontal() {
true => {
self.p1.x <= point.x && self.p2.x >= point.x
|| self.p2.x <= point.x && self.p1.x >= point.x
}
false => {
self.p1.y <= point.y && self.p2.y >= point.y
|| self.p2.y <= point.y && self.p1.y >= point.y
}
}
}
fn intersection(&self, other : &Line) -> Option<Point> {
if self.horizontal() == other.horizontal() {
return None;
}
let point = match self.horizontal() {
true => Point { x : other.p1.x, y : self.p1.y },
false => Point { x : self.p1.x, y : other.p1.y },
};
if self.contains_point(&point) && other.contains_point(&point) {
Some(point)
} else {
None
}
}
}
fn read_file(file : &str) -> String {
fs::read_to_string(file).unwrap()
}
fn required_fuel(mass : u32) -> u32 {
(mass / 3).saturating_sub(2)
}
fn required_fuel_with_fuel(mass : u32) -> u32 {
let mut total = 0;
let mut current = mass;
loop {
current = required_fuel(current);
total += current;
if current == 0 {
break;
}
}
total
}
fn day1() {
let input = read_file("input1");
let sum : u32 = input.split('\n')
.filter(|x| !x.is_empty())
.map(|x| x.parse::<u32>().unwrap())
.map(|x| required_fuel(x))
.sum();
println!("1a: {}", sum);
let sum2 : u32 = input.split('\n')
.filter(|x| !x.is_empty())
.map(|x| x.parse::<u32>().unwrap())
.map(|x| required_fuel_with_fuel(x))
.sum();
println!("1b: {}", sum2);
}
fn run_program(input : Vec<usize>) -> Vec<usize> {
let mut mem = input.clone();
for i in 0..mem.len()/4 {
let pos = i * 4;
let opcode = mem[pos];
if opcode == 99 {
break;
}
let op1 = mem[pos+1];
let op2 = mem[pos+2];
let op3 = mem[pos+3];
match opcode {
1 => { mem[op3] = mem[op1] + mem[op2]; }
2 => { mem[op3] = mem[op1] * mem[op2]; }
_ => panic!("invalid opcode")
}
}
mem
}
fn day2() {
let mut input = read_file("input2");
input.pop();
let mut program : Vec<usize> = input.split(',')
.map(|x| x.parse::<usize>().unwrap())
.collect();
program[1] = 12;
program[2] = 2;
let mem = run_program(program.clone());
println!("2a: {}", mem[0]);
for noun in 0..99 {
for verb in 0..99 {
program[1] = noun;
program[2] = verb;
let mem = run_program(program.clone());
if mem[0] == 19690720 {
println!("2b: {}", 100 * noun + verb);
return;
}
}
}
}
fn parse_lines(wire : &str) -> Vec<Line> {
let mut lines = Vec::new();
let mut p1 = Point { x : 0, y : 0 };
for next in wire.split(',') {
let dist = next[1..].parse::<i32>().unwrap();
let mut p2 = Point { x : p1.x, y : p1.y };
match next.chars().next().unwrap() {
'R' => p2.x += dist,
'L' => p2.x -= dist,
'U' => p2.y += dist,
'D' => p2.y -= dist,
_ => panic!("unexpected direction"),
};
lines.push(Line { p1, p2 });
p1 = p2;
}
lines
}
fn intersections(lines1 : &Vec<Line>, lines2 : &Vec<Line>) -> Vec<Point> {
let mut points = Vec::new();
for line1 in lines1 {
for line2 in lines2 {
if let Some(p) = line1.intersection(line2) {
points.push(p);
}
}
}
points
}
fn closest_intersection(wire1 : &str, wire2 : &str) -> i32 {
let lines1 = parse_lines(wire1);
let lines2 = parse_lines(wire2);
intersections(&lines1, &lines2).iter()
.map(|p| p.distance_origin())
.filter(|&x| x > 0)
.min().unwrap()
}
fn delay_to_point(lines : &Vec<Line>, p : &Point) -> i32 {
let mut delay = 0;
for line in lines {
if line.contains_point(&p) {
delay += (line.p1.distance_origin() - p.distance_origin()).abs();
break;
}
delay += line.length();
}
delay
}
fn min_delay(wire1 : &str, wire2 : &str) -> i32 {
let lines1 = parse_lines(wire1);
let lines2 = parse_lines(wire2);
let points = intersections(&lines1, &lines2);
points.iter()
.map(|p| delay_to_point(&lines1, &p) + delay_to_point(&lines2, &p))
.filter(|&d| d > 0)
.min().unwrap()
}
fn day3() {
let mut input = read_file("input3");
input.pop();
let wires : Vec<&str> = input.split('\n').collect();
let distance = closest_intersection(wires[0], wires[1]);
println!("3a: {}", distance);
let delay = min_delay(wires[0], wires[1]);
println!("3b: {}", delay);
}
fn digits(input : u32) -> Vec<u32> {
let mut tmp = input;
let mut digits = Vec::new();
loop {
digits.push(tmp % 10);
tmp /= 10;
if tmp == 0 {
break;
}
}
digits.reverse();
digits
}
fn valid_password(password : u32) -> bool {
let digits = digits(password);
if digits.len() != 6 {
return false;
}
let mut prev = 0;
let mut has_double = false;
for d in digits {
if d < prev {
return false;
}
if d == prev {
has_double = true;
}
prev = d
}
has_double
}
fn valid_password2(input : u32) -> bool {
if !valid_password(input) {
return false;
}
let digits = digits(input);
let mut same_count = 1;
let mut prev = 0;
for d in digits {
if d == prev {
same_count += 1;
} else {
if same_count == 2 {
return true;
}
same_count = 1;
}
prev = d;
}
same_count == 2
}
fn day4() {
let min = 264360;
let max = 746325;
let count = (min..max+1).filter(|&pw| valid_password(pw)).count();
println!("4a: {}", count);
let count = (min..max+1).filter(|&pw| valid_password2(pw)).count();
println!("4a: {}", count);
}
fn main() {
day4();
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_day1() {
assert_eq!(required_fuel(12), 2);
assert_eq!(required_fuel(14), 2);
assert_eq!(required_fuel(1969), 654);
assert_eq!(required_fuel(100756), 33583);
assert_eq!(required_fuel_with_fuel(14), 2);
assert_eq!(required_fuel_with_fuel(1969), 966);
assert_eq!(required_fuel_with_fuel(100756), 50346);
}
#[test]
fn test_day2() {
assert_eq!(run_program(vec!(1,0,0,0,99)), vec!(2,0,0,0,99));
assert_eq!(run_program(vec!(2,3,0,3,99)), vec!(2,3,0,6,99));
assert_eq!(run_program(vec!(2,4,4,5,99,0)), vec!(2,4,4,5,99,9801));
assert_eq!(run_program(vec!(1,1,1,4,99,5,6,0,99)), vec!(30,1,1,4,2,5,6,0,99));
}
#[test]
fn test_day3() {
let wire1 = "R8,U5,L5,D3";
let wire2 = "U7,R6,D4,L4";
assert_eq!(closest_intersection(wire1, wire2), 6);
assert_eq!(min_delay(wire1, wire2), 30);
let wire1 = "R75,D30,R83,U83,L12,D49,R71,U7,L72";
let wire2 = "U62,R66,U55,R34,D71,R55,D58,R83";
assert_eq!(closest_intersection(wire1, wire2), 159);
assert_eq!(min_delay(wire1, wire2), 610);
let wire1 = "R98,U47,R26,D63,R33,U87,L62,D20,R33,U53,R51";
let wire2 = "U98,R91,D20,R16,D67,R40,U7,R15,U6,R7";
assert_eq!(closest_intersection(wire1, wire2), 135);
assert_eq!(min_delay(wire1, wire2), 410);
}
#[test]
fn test_day4() {
assert!(valid_password(111111));
assert!(!valid_password(223450));
assert!(!valid_password(123789));
assert!(valid_password2(112233));
assert!(!valid_password2(123444));
assert!(valid_password2(111122));
}
}
|