blob: 81ac498b13a11c556cbd63671432c50be44111f8 (
plain)
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
|
use advent_lib::prelude::*;
use std::convert::TryFrom;
fn main() -> Result<()> {
let mut args = std::env::args();
if args.len() != 2 {
eprintln!("Usage: advent input");
}
let _ = args.next();
let filename = args.next().unwrap();
let input = advent_lib::read_lines_file(&filename)?;
let timestamp = input[0].parse::<i64>()?;
let mut busses: Vec<Option<i64>> = Vec::new();
for word in input[1].split(',') {
if word == "x" {
busses.push(None);
} else {
busses.push(Some(word.parse::<i64>().unwrap()));
}
}
let mut best_bus = 0;
let mut best_delay = timestamp;
for bus in &busses {
match bus {
Some(frequency) => {
let delay = frequency - (timestamp % frequency);
if delay < best_delay {
best_bus = *frequency;
best_delay = delay;
}
},
None => { },
}
}
println!("{}", best_bus * best_delay);
let mut frequency_so_far = 0;
let mut offset_so_far = 0;
let mut skip_from_previous_bus = 0;
for i in 0 .. busses.len() {
match busses[i] {
Some(frequency) => {
if i == 0 {
frequency_so_far = frequency;
offset_so_far = 0;
} else {
// frequency_so_far * a + skip_from_previous_bus = frequency * b
let mut proposed_offset = offset_so_far + skip_from_previous_bus;
loop {
if proposed_offset % frequency == 0 {
offset_so_far = proposed_offset;
frequency_so_far *= frequency;
break;
} else {
proposed_offset += frequency_so_far;
}
}
}
skip_from_previous_bus = 1;
},
None => {
skip_from_previous_bus += 1;
},
}
}
offset_so_far -= i64::try_from(busses.len()).unwrap() - 1;
println!("{}", offset_so_far);
Ok(())
}
|