Day 12+cleanup
This commit is contained in:
parent
fd583e70e4
commit
7743a1cdc6
4 changed files with 194 additions and 27 deletions
37
src/day10.rs
37
src/day10.rs
|
@ -1,9 +1,5 @@
|
||||||
use aoc_runner_derive::{aoc, aoc_generator};
|
use aoc_runner_derive::{aoc, aoc_generator};
|
||||||
use std::iter::FromIterator;
|
use std::collections::{HashMap, HashSet};
|
||||||
use std::{
|
|
||||||
collections::{HashMap, HashSet, VecDeque},
|
|
||||||
ops::{Sub, SubAssign},
|
|
||||||
};
|
|
||||||
#[derive(Debug, Eq, PartialEq, Clone)]
|
#[derive(Debug, Eq, PartialEq, Clone)]
|
||||||
struct SearchState {
|
struct SearchState {
|
||||||
available: HashSet<u16>,
|
available: HashSet<u16>,
|
||||||
|
@ -41,6 +37,37 @@ fn solve_part_2_rec(
|
||||||
// println!("-{}", node);
|
// println!("-{}", node);
|
||||||
ret
|
ret
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn solve_part_2_it(node: u16, goal: u16, map: &HashMap<u16, HashSet<u16>>) -> usize {
|
||||||
|
// let mut cache: HashMap<(u16, u16), usize> = HashMap::new();
|
||||||
|
// let mut used = HashSet::new();
|
||||||
|
// let mut stack=Vec::new();
|
||||||
|
// // stack.push((node);
|
||||||
|
// if node == goal {
|
||||||
|
// return 1;
|
||||||
|
// }
|
||||||
|
// let mut ret = 0;
|
||||||
|
// while let Some(node) = stack.pop() {
|
||||||
|
// for next in map.get(&node).iter().map(|v| v.iter()).flatten() {
|
||||||
|
// if used.contains(next) {
|
||||||
|
// continue;
|
||||||
|
// };
|
||||||
|
// used.insert(*next);
|
||||||
|
// let value = match cache.get(&(node, *next)) {
|
||||||
|
// Some(value) => *value,
|
||||||
|
// None => {
|
||||||
|
// let value = solve_part_2_rec(*next, goal, map, used, cache);
|
||||||
|
// cache.insert((node, *next), value);
|
||||||
|
// value
|
||||||
|
// }
|
||||||
|
// };
|
||||||
|
// ret += value;
|
||||||
|
// used.remove(next);
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
pub struct Data(pub Vec<u16>);
|
pub struct Data(pub Vec<u16>);
|
||||||
impl Data {
|
impl Data {
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
|
|
25
src/day11.rs
25
src/day11.rs
|
@ -1,5 +1,4 @@
|
||||||
use aoc_runner_derive::{aoc, aoc_generator};
|
use aoc_runner_derive::{aoc, aoc_generator};
|
||||||
use std::io::Write;
|
|
||||||
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
|
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
|
||||||
pub enum Seat {
|
pub enum Seat {
|
||||||
Floor,
|
Floor,
|
||||||
|
@ -10,19 +9,6 @@ pub enum Seat {
|
||||||
pub struct Data(Vec<Vec<Seat>>);
|
pub struct Data(Vec<Vec<Seat>>);
|
||||||
|
|
||||||
impl Data {
|
impl Data {
|
||||||
fn print(&self) {
|
|
||||||
for row in &self.0 {
|
|
||||||
for seat in row {
|
|
||||||
let c = match seat {
|
|
||||||
Seat::Floor => '.',
|
|
||||||
Seat::Empty => 'L',
|
|
||||||
Seat::Occupied => '#',
|
|
||||||
};
|
|
||||||
print!("{}", c);
|
|
||||||
}
|
|
||||||
println!("");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
fn count_nb_p1(&self, x: usize, y: usize) -> usize {
|
fn count_nb_p1(&self, x: usize, y: usize) -> usize {
|
||||||
let mut ret = 0;
|
let mut ret = 0;
|
||||||
for dy in &[-1i64, 0, 1] {
|
for dy in &[-1i64, 0, 1] {
|
||||||
|
@ -33,12 +19,9 @@ impl Data {
|
||||||
let x = ((x as i64) + dx) as usize;
|
let x = ((x as i64) + dx) as usize;
|
||||||
let y = ((y as i64) + dy) as usize;
|
let y = ((y as i64) + dy) as usize;
|
||||||
if let Some(v) = self.0.get(y).map(|v| v.get(x)).flatten() {
|
if let Some(v) = self.0.get(y).map(|v| v.get(x)).flatten() {
|
||||||
match v {
|
if let Seat::Occupied = v {
|
||||||
Seat::Occupied => {
|
|
||||||
ret += 1;
|
ret += 1;
|
||||||
}
|
}
|
||||||
_ => (),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -75,7 +58,7 @@ impl Data {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return ret;
|
ret
|
||||||
}
|
}
|
||||||
|
|
||||||
fn update_p1(&mut self) -> bool {
|
fn update_p1(&mut self) -> bool {
|
||||||
|
@ -100,7 +83,7 @@ impl Data {
|
||||||
}
|
}
|
||||||
let changed = data != self.0;
|
let changed = data != self.0;
|
||||||
self.0 = data;
|
self.0 = data;
|
||||||
return changed;
|
changed
|
||||||
}
|
}
|
||||||
|
|
||||||
fn update_p2(&mut self) -> bool {
|
fn update_p2(&mut self) -> bool {
|
||||||
|
@ -125,7 +108,7 @@ impl Data {
|
||||||
}
|
}
|
||||||
let changed = data != self.0;
|
let changed = data != self.0;
|
||||||
self.0 = data;
|
self.0 = data;
|
||||||
return changed;
|
changed
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
156
src/day12.rs
Normal file
156
src/day12.rs
Normal file
|
@ -0,0 +1,156 @@
|
||||||
|
use aoc_runner_derive::{aoc, aoc_generator};
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub enum Direction {
|
||||||
|
North,
|
||||||
|
East,
|
||||||
|
South,
|
||||||
|
West
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Direction {
|
||||||
|
fn from_int(n: i64) -> Self {
|
||||||
|
match n%4 {
|
||||||
|
0 => Self::East,
|
||||||
|
1 => Self::South,
|
||||||
|
2 => Self::West,
|
||||||
|
3 => Self::North,
|
||||||
|
_ => unreachable!()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub enum Command {
|
||||||
|
Direction(Direction,i64),
|
||||||
|
Left(i64),
|
||||||
|
Right(i64),
|
||||||
|
Forward(i64)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub struct Ship {
|
||||||
|
rot: i64,
|
||||||
|
pos: (i64,i64),
|
||||||
|
wp: (i64,i64)
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Ship {
|
||||||
|
fn new() -> Self {
|
||||||
|
Self {
|
||||||
|
rot:0,
|
||||||
|
pos: (0,0),
|
||||||
|
wp: (10,1)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn facing(&self) -> Direction {
|
||||||
|
Direction::from_int(self.rot)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn cmd_p1(&mut self, cmd: &Command) {
|
||||||
|
match cmd {
|
||||||
|
Command::Direction(Direction::North,n) => {
|
||||||
|
self.pos.1+=n;
|
||||||
|
}
|
||||||
|
Command::Direction(Direction::South,n) => {
|
||||||
|
self.pos.1-=n;
|
||||||
|
}
|
||||||
|
Command::Direction(Direction::East,n) => {
|
||||||
|
self.pos.0+=n;
|
||||||
|
}
|
||||||
|
Command::Direction(Direction::West,n) => {
|
||||||
|
self.pos.0-=n;
|
||||||
|
}
|
||||||
|
Command::Left(n) => {
|
||||||
|
self.rot-=n;
|
||||||
|
}
|
||||||
|
Command::Right(n) => {
|
||||||
|
self.rot+=n;
|
||||||
|
}
|
||||||
|
Command::Forward(n) => {
|
||||||
|
self.cmd_p1(&Command::Direction(self.facing(),*n))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn cmd_p2(&mut self, cmd: &Command) {
|
||||||
|
match cmd {
|
||||||
|
Command::Direction(Direction::North,n) => {
|
||||||
|
self.wp.1+=n;
|
||||||
|
}
|
||||||
|
Command::Direction(Direction::South,n) => {
|
||||||
|
self.wp.1-=n;
|
||||||
|
}
|
||||||
|
Command::Direction(Direction::East,n) => {
|
||||||
|
self.wp.0+=n;
|
||||||
|
}
|
||||||
|
Command::Direction(Direction::West,n) => {
|
||||||
|
self.wp.0-=n;
|
||||||
|
}
|
||||||
|
Command::Right(n) => {
|
||||||
|
let wp=self.wp;
|
||||||
|
self.wp=match n%4 {
|
||||||
|
0 => wp,
|
||||||
|
1 => (wp.1,-wp.0),
|
||||||
|
2 => (-wp.0,-wp.1),
|
||||||
|
3 => (-wp.1,wp.0),
|
||||||
|
_ => unreachable!()
|
||||||
|
};
|
||||||
|
}
|
||||||
|
Command::Left(n) => {
|
||||||
|
let wp=self.wp;
|
||||||
|
self.wp=match n%4 {
|
||||||
|
0 => wp,
|
||||||
|
1 => (-wp.1,wp.0),
|
||||||
|
2 => (-wp.0,-wp.1),
|
||||||
|
3 => (wp.1,-wp.0),
|
||||||
|
_ => unreachable!()
|
||||||
|
};
|
||||||
|
}
|
||||||
|
Command::Forward(n) => {
|
||||||
|
self.pos.0+=self.wp.0*n;
|
||||||
|
self.pos.1+=self.wp.1*n;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[aoc_generator(day12)]
|
||||||
|
pub fn input_generator(input: &str) -> Vec<Command> {
|
||||||
|
input
|
||||||
|
.lines()
|
||||||
|
.map(|l| {
|
||||||
|
let mut chars=l.chars();
|
||||||
|
let cmd=chars.next().unwrap();
|
||||||
|
let num = chars.collect::<String>().parse().unwrap();
|
||||||
|
match cmd {
|
||||||
|
'N' => Command::Direction(Direction::North, num),
|
||||||
|
'S' => Command::Direction(Direction::South,num),
|
||||||
|
'E' => Command::Direction(Direction::East,num),
|
||||||
|
'W' => Command::Direction(Direction::West,num),
|
||||||
|
'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)
|
||||||
|
}
|
||||||
|
}).collect::<Vec<Command>>()
|
||||||
|
}
|
||||||
|
|
||||||
|
#[aoc(day12, part1)]
|
||||||
|
pub fn solve_part1(input: &Vec<Command>) -> usize {
|
||||||
|
let mut ship=Ship::new();
|
||||||
|
for cmd in input {
|
||||||
|
ship.cmd_p1(cmd);
|
||||||
|
}
|
||||||
|
return (ship.pos.0.abs() as usize) + (ship.pos.1.abs() as usize);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[aoc(day12, part2)]
|
||||||
|
pub fn solve_part2(input: &Vec<Command>) -> usize {
|
||||||
|
let mut ship=Ship::new();
|
||||||
|
for cmd in input {
|
||||||
|
ship.cmd_p2(cmd);
|
||||||
|
}
|
||||||
|
return (ship.pos.0.abs() as usize) + (ship.pos.1.abs() as usize);
|
||||||
|
}
|
|
@ -10,4 +10,5 @@ pub mod day08;
|
||||||
pub mod day09;
|
pub mod day09;
|
||||||
pub mod day10;
|
pub mod day10;
|
||||||
pub mod day11;
|
pub mod day11;
|
||||||
|
pub mod day12;
|
||||||
aoc_lib! { year = 2020 }
|
aoc_lib! { year = 2020 }
|
||||||
|
|
Loading…
Reference in a new issue