Day 10 + rename old files for proper sorting

This commit is contained in:
Daniel S. 2020-12-11 01:33:19 +01:00
parent 2249b4b9ef
commit cc1cb04b33
11 changed files with 124 additions and 13 deletions

View file

@ -16,6 +16,7 @@ impl Data {
}
#[aoc_generator(day7)]
#[inline(always)]
pub fn input_generator(input: &str) -> Data {
let mut ret = Data::new();
for line in input.lines() {
@ -51,6 +52,7 @@ pub fn input_generator(input: &str) -> Data {
}
#[aoc(day7, part1)]
#[inline(always)]
pub fn solve_part1(input: &Data) -> usize {
let mut cnt = 0;
let mut q = VecDeque::new();
@ -65,6 +67,7 @@ pub fn solve_part1(input: &Data) -> usize {
}
#[aoc(day7, part2)]
#[inline(always)]
pub fn solve_part2(input: &Data) -> usize {
let mut cnt = 0;
let mut q = VecDeque::new();

View file

@ -81,11 +81,13 @@ impl CPU {
}
#[aoc_generator(day8)]
#[inline(always)]
pub fn input_generator(input: &str) -> Vec<Inst> {
input.trim().lines().map(|l| Inst::from(l.trim())).collect()
}
#[aoc(day8, part1)]
#[inline(always)]
pub fn solve_part1(input: &Vec<Inst>) -> usize {
let mut seen = HashSet::new();
let mut cpu = CPU::new(input);
@ -100,6 +102,7 @@ pub fn solve_part1(input: &Vec<Inst>) -> usize {
}
#[aoc(day8, part2)]
#[inline(always)]
pub fn solve_part2(input: &Vec<Inst>) -> usize {
for (idx, inst) in input.iter().enumerate() {
match inst {

View file

@ -1,6 +1,7 @@
use aoc_runner_derive::{aoc, aoc_generator};
#[aoc_generator(day9)]
#[inline(always)]
pub fn input_generator(input: &str) -> Vec<usize> {
input
.lines()
@ -10,6 +11,7 @@ pub fn input_generator(input: &str) -> Vec<usize> {
}
#[aoc(day9, part1)]
#[inline(always)]
pub fn solve_part1(input: &Vec<usize>) -> usize {
for (n, win) in input.as_slice().windows(25).enumerate() {
let n = n + 25;
@ -36,6 +38,7 @@ pub fn solve_part1(input: &Vec<usize>) -> usize {
}
#[aoc(day9, part2, bruteforce)]
#[inline(always)]
pub fn solve_part2(input: &Vec<usize>) -> usize {
let inv_num = solve_part1(input);
for start in 0..input.len() {
@ -50,6 +53,7 @@ pub fn solve_part2(input: &Vec<usize>) -> usize {
}
#[aoc(day9, part2, faster)]
#[inline(always)]
pub fn solve_part2_fast(input: &Vec<usize>) -> usize {
let target_num = solve_part1(input);
for i in 2..=input.len() {

100
src/day10.rs Normal file
View file

@ -0,0 +1,100 @@
use aoc_runner_derive::{aoc, aoc_generator};
use std::iter::FromIterator;
use std::{
collections::{HashMap, HashSet, VecDeque},
ops::{Sub, SubAssign},
};
#[derive(Debug, Eq, PartialEq, Clone)]
struct SearchState {
available: HashSet<u16>,
node: u16,
}
fn solve_part_2_rec(
node: u16,
goal: u16,
map: &HashMap<u16, HashSet<u16>>,
used: &HashSet<u16>,
cache: &mut HashMap<(u16, u16), usize>,
) -> usize {
if node == goal {
return 1;
}
let mut ret = 0;
let mut used = used.clone();
// println!("+{}", node);
for next in map.get(&node).iter().map(|v| v.iter()).flatten() {
if !used.insert(*next) {
continue;
};
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);
}
// println!("-{}", node);
ret
}
pub struct Data(pub Vec<u16>);
impl Data {
#[inline(always)]
fn count_paths(&self) -> usize {
let mut nodes = self.0.clone();
nodes.push(0);
nodes.push(nodes.iter().max().unwrap() + 3);
nodes.sort();
let goal = *nodes.last().unwrap();
let mut map: HashMap<u16, HashSet<u16>> = HashMap::new();
for n in &nodes {
for v in &nodes {
let d = v - n;
if d > 0 && d < 4 {
map.entry(*n).or_default().insert(*v);
}
}
}
return solve_part_2_rec(0, goal, &map, &HashSet::new(), &mut HashMap::new());
}
}
#[aoc_generator(day10)]
pub fn input_generator(input: &str) -> Data {
let mut numbers = input
.lines()
.map(|l| l.trim().parse())
.collect::<Result<Vec<u16>, _>>()
.unwrap();
numbers.sort_unstable();
return Data(numbers);
}
#[aoc(day10, part1)]
#[inline(always)]
pub fn solve_part1(input: &Data) -> usize {
let mut n_1 = 0;
let mut n_3 = 0;
let mut nums = input.0.clone();
nums.push(0);
nums.push(nums.iter().max().unwrap() + 3);
nums.sort_unstable();
for w in nums.windows(2) {
match w[1] - w[0] {
1 => n_1 += 1,
3 => n_3 += 1,
_ => (),
}
}
return n_1 * n_3;
}
#[aoc(day10, part2)]
#[inline(always)]
pub fn solve_part2(input: &Data) -> usize {
return input.count_paths();
}

View file

@ -1,11 +1,12 @@
use aoc_runner_derive::aoc_lib;
pub mod day1;
pub mod day2;
pub mod day3;
pub mod day4;
pub mod day5;
pub mod day6;
pub mod day7;
pub mod day8;
pub mod day9;
pub mod day01;
pub mod day02;
pub mod day03;
pub mod day04;
pub mod day05;
pub mod day06;
pub mod day07;
pub mod day08;
pub mod day09;
pub mod day10;
aoc_lib! { year = 2020 }