Day 13+formatting

This commit is contained in:
Daniel S. 2020-12-14 14:15:43 +01:00
parent 7743a1cdc6
commit d9e4bf6ad3
4 changed files with 232 additions and 157 deletions

1
.gitignore vendored
View file

@ -1,3 +1,4 @@
/target
.history
input
src/template.rs

View file

@ -5,7 +5,7 @@ pub enum Direction {
North,
East,
South,
West
West,
}
impl Direction {
@ -15,7 +15,7 @@ impl Direction {
1 => Self::South,
2 => Self::West,
3 => Self::North,
_ => unreachable!()
_ => unreachable!(),
}
}
}
@ -25,14 +25,14 @@ pub enum Command {
Direction(Direction, i64),
Left(i64),
Right(i64),
Forward(i64)
Forward(i64),
}
#[derive(Debug)]
pub struct Ship {
rot: i64,
pos: (i64, i64),
wp: (i64,i64)
wp: (i64, i64),
}
impl Ship {
@ -40,7 +40,7 @@ impl Ship {
Self {
rot: 0,
pos: (0, 0),
wp: (10,1)
wp: (10, 1),
}
}
@ -68,9 +68,7 @@ impl Ship {
Command::Right(n) => {
self.rot += n;
}
Command::Forward(n) => {
self.cmd_p1(&Command::Direction(self.facing(),*n))
}
Command::Forward(n) => self.cmd_p1(&Command::Direction(self.facing(), *n)),
}
}
@ -95,7 +93,7 @@ impl Ship {
1 => (wp.1, -wp.0),
2 => (-wp.0, -wp.1),
3 => (-wp.1, wp.0),
_ => unreachable!()
_ => unreachable!(),
};
}
Command::Left(n) => {
@ -105,7 +103,7 @@ impl Ship {
1 => (-wp.1, wp.0),
2 => (-wp.0, -wp.1),
3 => (wp.1, -wp.0),
_ => unreachable!()
_ => unreachable!(),
};
}
Command::Forward(n) => {
@ -132,9 +130,10 @@ pub fn input_generator(input: &str) -> Vec<Command> {
'L' if num % 90 == 0 => Command::Left((num / 90) % 4),
'R' if num % 90 == 0 => Command::Right((num / 90) % 4),
'F' => Command::Forward(num),
_ => panic!("Invalid command: {}",l)
_ => panic!("Invalid command: {}", l),
}
}).collect::<Vec<Command>>()
})
.collect::<Vec<Command>>()
}
#[aoc(day12, part1)]

74
src/day13.rs Normal file
View file

@ -0,0 +1,74 @@
use aoc_runner_derive::{aoc, aoc_generator};
#[derive(Debug)]
pub struct BusInfo {
earliest: u64,
busses: Vec<u64>,
}
type Data = BusInfo;
#[aoc_generator(day13)]
pub fn input_generator(input: &str) -> Data {
let mut l = input.lines();
let earliest = l.next().unwrap().parse().unwrap();
let busses = l
.next()
.unwrap()
.split(',')
.map(|c| match c {
"x" => 0,
n => n.parse().unwrap(),
})
.collect();
BusInfo { earliest, busses }
}
#[aoc(day13, part1)]
pub fn solve_part1(input: &Data) -> usize {
let e = input.earliest;
let mut min = std::u64::MAX;
let mut min_id = 0;
for n in &input.busses {
if *n == 0 {
continue;
}
let m = n - (e % n);
if m < min {
min = m;
min_id = *n;
}
}
return min as usize * min_id as usize;
}
#[aoc(day13, part2)]
pub fn solve_part2(input: &Data) -> i64 {
let b = input
.busses
.iter()
.enumerate()
.filter(|(_, n)| **n != 0)
.map(|(a, b)| (a as i64, *b as i64))
.collect::<Vec<_>>();
let mut t = 0;
let target = b.iter().map(|(_, b)| b).fold(1, |a, b| a * b);
loop {
let mut cnt = 0;
let dt = b
.iter()
.filter_map(|(i, n)| {
if (t + i) % n == 0 {
cnt += 1;
Some(*n)
} else {
None
}
})
.fold(1, |a, b| a * b);
if dt == target {
return t % dt;
}
t += dt;
}
}

View file

@ -11,4 +11,5 @@ pub mod day09;
pub mod day10;
pub mod day11;
pub mod day12;
pub mod day13;
aoc_lib! { year = 2020 }