day13 only part 1
This commit is contained in:
parent
d504254ecf
commit
6a4ac7008b
2
input/2020/day13.txt
Normal file
2
input/2020/day13.txt
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
1004345
|
||||||
|
41,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,37,x,x,x,x,x,379,x,x,x,x,x,x,x,23,x,x,x,x,13,x,x,x,17,x,x,x,x,x,x,x,x,x,x,x,29,x,557,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,19
|
||||||
42
src/day13.rs
Normal file
42
src/day13.rs
Normal file
@ -0,0 +1,42 @@
|
|||||||
|
use aoc_runner_derive::aoc;
|
||||||
|
|
||||||
|
#[aoc(day13, part1)]
|
||||||
|
fn solve_d13_p1(input: &str) -> usize {
|
||||||
|
let first_newline = input.find('\n').unwrap();
|
||||||
|
let (t, bus_ids) = input.split_at(first_newline);
|
||||||
|
let bus_ids = bus_ids.trim();
|
||||||
|
|
||||||
|
let t: usize = t.parse().unwrap();
|
||||||
|
|
||||||
|
struct WaitTime {
|
||||||
|
bus_id: usize,
|
||||||
|
wait_time: usize,
|
||||||
|
}
|
||||||
|
let min_wait_time = bus_ids
|
||||||
|
.split(',')
|
||||||
|
.filter(|&x| x != "x")
|
||||||
|
.map(|x| x.parse::<usize>().unwrap())
|
||||||
|
.fold(
|
||||||
|
WaitTime {
|
||||||
|
bus_id: usize::MAX,
|
||||||
|
wait_time: usize::MAX,
|
||||||
|
},
|
||||||
|
|min, bus_id| {
|
||||||
|
let offset = t % bus_id;
|
||||||
|
let wait_time = if offset == 0 {
|
||||||
|
0
|
||||||
|
} else {
|
||||||
|
bus_id - offset
|
||||||
|
};
|
||||||
|
if min.wait_time < wait_time {
|
||||||
|
min
|
||||||
|
} else {
|
||||||
|
WaitTime{
|
||||||
|
bus_id,
|
||||||
|
wait_time,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
);
|
||||||
|
min_wait_time.bus_id * min_wait_time.wait_time
|
||||||
|
}
|
||||||
@ -2,6 +2,7 @@ pub mod day1;
|
|||||||
pub mod day10;
|
pub mod day10;
|
||||||
pub mod day11;
|
pub mod day11;
|
||||||
pub mod day12;
|
pub mod day12;
|
||||||
|
pub mod day13;
|
||||||
pub mod day2;
|
pub mod day2;
|
||||||
pub mod day3;
|
pub mod day3;
|
||||||
pub mod day4;
|
pub mod day4;
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user