Day 13+formatting
This commit is contained in:
parent
7743a1cdc6
commit
d9e4bf6ad3
4 changed files with 232 additions and 157 deletions
1
.gitignore
vendored
1
.gitignore
vendored
|
@ -1,3 +1,4 @@
|
||||||
/target
|
/target
|
||||||
.history
|
.history
|
||||||
input
|
input
|
||||||
|
src/template.rs
|
23
src/day12.rs
23
src/day12.rs
|
@ -5,7 +5,7 @@ pub enum Direction {
|
||||||
North,
|
North,
|
||||||
East,
|
East,
|
||||||
South,
|
South,
|
||||||
West
|
West,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Direction {
|
impl Direction {
|
||||||
|
@ -15,7 +15,7 @@ impl Direction {
|
||||||
1 => Self::South,
|
1 => Self::South,
|
||||||
2 => Self::West,
|
2 => Self::West,
|
||||||
3 => Self::North,
|
3 => Self::North,
|
||||||
_ => unreachable!()
|
_ => unreachable!(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -25,14 +25,14 @@ pub enum Command {
|
||||||
Direction(Direction, i64),
|
Direction(Direction, i64),
|
||||||
Left(i64),
|
Left(i64),
|
||||||
Right(i64),
|
Right(i64),
|
||||||
Forward(i64)
|
Forward(i64),
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct Ship {
|
pub struct Ship {
|
||||||
rot: i64,
|
rot: i64,
|
||||||
pos: (i64, i64),
|
pos: (i64, i64),
|
||||||
wp: (i64,i64)
|
wp: (i64, i64),
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Ship {
|
impl Ship {
|
||||||
|
@ -40,7 +40,7 @@ impl Ship {
|
||||||
Self {
|
Self {
|
||||||
rot: 0,
|
rot: 0,
|
||||||
pos: (0, 0),
|
pos: (0, 0),
|
||||||
wp: (10,1)
|
wp: (10, 1),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -68,9 +68,7 @@ impl Ship {
|
||||||
Command::Right(n) => {
|
Command::Right(n) => {
|
||||||
self.rot += n;
|
self.rot += n;
|
||||||
}
|
}
|
||||||
Command::Forward(n) => {
|
Command::Forward(n) => self.cmd_p1(&Command::Direction(self.facing(), *n)),
|
||||||
self.cmd_p1(&Command::Direction(self.facing(),*n))
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -95,7 +93,7 @@ impl Ship {
|
||||||
1 => (wp.1, -wp.0),
|
1 => (wp.1, -wp.0),
|
||||||
2 => (-wp.0, -wp.1),
|
2 => (-wp.0, -wp.1),
|
||||||
3 => (-wp.1, wp.0),
|
3 => (-wp.1, wp.0),
|
||||||
_ => unreachable!()
|
_ => unreachable!(),
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
Command::Left(n) => {
|
Command::Left(n) => {
|
||||||
|
@ -105,7 +103,7 @@ impl Ship {
|
||||||
1 => (-wp.1, wp.0),
|
1 => (-wp.1, wp.0),
|
||||||
2 => (-wp.0, -wp.1),
|
2 => (-wp.0, -wp.1),
|
||||||
3 => (wp.1, -wp.0),
|
3 => (wp.1, -wp.0),
|
||||||
_ => unreachable!()
|
_ => unreachable!(),
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
Command::Forward(n) => {
|
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),
|
'L' if num % 90 == 0 => Command::Left((num / 90) % 4),
|
||||||
'R' if num % 90 == 0 => Command::Right((num / 90) % 4),
|
'R' if num % 90 == 0 => Command::Right((num / 90) % 4),
|
||||||
'F' => Command::Forward(num),
|
'F' => Command::Forward(num),
|
||||||
_ => panic!("Invalid command: {}",l)
|
_ => panic!("Invalid command: {}", l),
|
||||||
}
|
}
|
||||||
}).collect::<Vec<Command>>()
|
})
|
||||||
|
.collect::<Vec<Command>>()
|
||||||
}
|
}
|
||||||
|
|
||||||
#[aoc(day12, part1)]
|
#[aoc(day12, part1)]
|
||||||
|
|
74
src/day13.rs
Normal file
74
src/day13.rs
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
|
@ -11,4 +11,5 @@ pub mod day09;
|
||||||
pub mod day10;
|
pub mod day10;
|
||||||
pub mod day11;
|
pub mod day11;
|
||||||
pub mod day12;
|
pub mod day12;
|
||||||
|
pub mod day13;
|
||||||
aoc_lib! { year = 2020 }
|
aoc_lib! { year = 2020 }
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue