cargo fmt

This commit is contained in:
Daniel S. 2020-12-03 15:10:04 +01:00
parent 0bb5d960e5
commit 336a03055d
5 changed files with 187 additions and 168 deletions

View file

@ -1,40 +1,49 @@
use aoc_runner_derive::{aoc,aoc_generator}; use aoc_runner_derive::{aoc, aoc_generator};
#[aoc_generator(day1)] #[aoc_generator(day1)]
pub fn input_generator(input: &str) -> Vec<usize> { pub fn input_generator(input: &str) -> Vec<usize> {
input.lines().map(|l| l.trim().parse()).collect::<Result<Vec<usize>,_>>().unwrap() input
} .lines()
.map(|l| l.trim().parse())
#[aoc(day1, part1)] .collect::<Result<Vec<usize>, _>>()
pub fn solve_part1(input: &Vec<usize>) -> usize { .unwrap()
let mut input=input.clone(); }
input.sort();
for (i1,v1) in input.iter().enumerate() { #[aoc(day1, part1)]
if v1>&2020 {break;} pub fn solve_part1(input: &Vec<usize>) -> usize {
for v2 in input.iter().skip(i1+1) { let mut input = input.clone();
if (v1+v2)==2020 { input.sort();
return v1*v2; for (i1, v1) in input.iter().enumerate() {
} if v1 > &2020 {
} break;
} }
panic!("No pair summed to 20202!"); for v2 in input.iter().skip(i1 + 1) {
} if (v1 + v2) == 2020 {
return v1 * v2;
}
#[aoc(day1, part2)] }
pub fn solve_part2(input: &Vec<usize>) -> usize { }
let mut input=input.clone(); panic!("No pair summed to 20202!");
input.sort(); }
for (i1,v1) in input.iter().enumerate() {
if v1>&2020 {break;} #[aoc(day1, part2)]
for (i2,v2) in input.iter().enumerate().skip(i1+1) { pub fn solve_part2(input: &Vec<usize>) -> usize {
if (v1+v2)>2020 {break;} let mut input = input.clone();
for v3 in input.iter().skip(i2+1) { input.sort();
if (v1+v2+v3)==2020 { for (i1, v1) in input.iter().enumerate() {
return v1*v2*v3; if v1 > &2020 {
} break;
} }
} for (i2, v2) in input.iter().enumerate().skip(i1 + 1) {
} if (v1 + v2) > 2020 {
panic!("No triplet summed to 20202!"); break;
} }
for v3 in input.iter().skip(i2 + 1) {
if (v1 + v2 + v3) == 2020 {
return v1 * v2 * v3;
}
}
}
}
panic!("No triplet summed to 20202!");
}

View file

@ -1,68 +1,75 @@
use aoc_runner_derive::{aoc,aoc_generator}; use aoc_runner_derive::{aoc, aoc_generator};
#[derive(Debug,PartialEq,Eq,Ord,PartialOrd)] #[derive(Debug, PartialEq, Eq, Ord, PartialOrd)]
pub struct Rule { pub struct Rule {
min: usize, min: usize,
max: usize, max: usize,
ch: char, ch: char,
pw: String pw: String,
} }
impl Rule { impl Rule {
fn is_valid_part1(&self) -> bool { fn is_valid_part1(&self) -> bool {
let n=self.pw.chars().filter(|c| *c==self.ch).count(); let n = self.pw.chars().filter(|c| *c == self.ch).count();
return n>=self.min && n<=self.max; return n >= self.min && n <= self.max;
} }
fn is_valid_part2(&self) -> bool { fn is_valid_part2(&self) -> bool {
let idx_1=self.min-1; let idx_1 = self.min - 1;
let idx_2=self.max-1; let idx_2 = self.max - 1;
let chars: Vec<char> = self.pw.chars().collect(); let chars: Vec<char> = self.pw.chars().collect();
return (chars.get(idx_1)==Some(&self.ch))^(chars.get(idx_2)==Some(&self.ch)) return (chars.get(idx_1) == Some(&self.ch)) ^ (chars.get(idx_2) == Some(&self.ch));
} }
} }
#[aoc_generator(day2)] #[aoc_generator(day2)]
pub fn input_generator(input: &str) -> Vec<Rule> { pub fn input_generator(input: &str) -> Vec<Rule> {
input.lines().map(|l| { input
let mut ws = l.trim().split_whitespace(); .lines()
let range= ws.next().unwrap(); .map(|l| {
let ch= ws.next().unwrap().trim_end_matches(|c| c==':').chars().next().unwrap(); let mut ws = l.trim().split_whitespace();
let pw= ws.next().unwrap().to_owned(); let range = ws.next().unwrap();
let (r_min, r_max) = { let ch = ws
let mut r=range.split('-'); .next()
let r_min=r.next().unwrap().parse().unwrap(); .unwrap()
let r_max=r.next().unwrap().parse().unwrap(); .trim_end_matches(|c| c == ':')
(r_min,r_max) .chars()
}; .next()
Rule { .unwrap();
min:r_min, let pw = ws.next().unwrap().to_owned();
max:r_max, let (r_min, r_max) = {
ch, let mut r = range.split('-');
pw let r_min = r.next().unwrap().parse().unwrap();
} let r_max = r.next().unwrap().parse().unwrap();
}).collect::<Vec<Rule>>() (r_min, r_max)
} };
Rule {
#[aoc(day2, part1)] min: r_min,
pub fn solve_part1(input: &Vec<Rule>) -> usize { max: r_max,
let mut total=0; ch,
for rule in input { pw,
if rule.is_valid_part1() { }
total+=1; })
} .collect::<Vec<Rule>>()
} }
total
} #[aoc(day2, part1)]
pub fn solve_part1(input: &Vec<Rule>) -> usize {
let mut total = 0;
#[aoc(day2, part2)] for rule in input {
pub fn solve_part2(input: &Vec<Rule>) -> usize { if rule.is_valid_part1() {
let mut total=0; total += 1;
for rule in input { }
if rule.is_valid_part2() { }
total+=1; total
} }
}
total #[aoc(day2, part2)]
} pub fn solve_part2(input: &Vec<Rule>) -> usize {
let mut total = 0;
for rule in input {
if rule.is_valid_part2() {
total += 1;
}
}
total
}

View file

@ -1,54 +1,57 @@
use aoc_runner_derive::{aoc,aoc_generator}; use aoc_runner_derive::{aoc, aoc_generator};
pub struct Forest { pub struct Forest {
slice: Vec<Vec<bool>>, slice: Vec<Vec<bool>>,
} }
impl Forest { impl Forest {
fn get(&self, x: usize,y: usize) -> Option<bool> { fn get(&self, x: usize, y: usize) -> Option<bool> {
self.slice.get(y).map(|line| line.get(x%line.len())).flatten().cloned() self.slice
} .get(y)
} .map(|line| line.get(x % line.len()))
.flatten()
.cloned()
#[aoc_generator(day3)] }
pub fn input_generator(input: &str) -> Forest { }
let slice=input.lines().map(|l| l.chars().map(|c| c=='#').collect()).collect();
Forest{ #[aoc_generator(day3)]
slice pub fn input_generator(input: &str) -> Forest {
} let slice = input
} .lines()
.map(|l| l.chars().map(|c| c == '#').collect())
#[aoc(day3,part1)] .collect();
pub fn solve_part1(input: &Forest) -> usize { Forest { slice }
let mut sum: usize=0; }
let dx=3;
let dy=1; #[aoc(day3, part1)]
let mut pos=(0,0); pub fn solve_part1(input: &Forest) -> usize {
while let Some(tree) = input.get(pos.0,pos.1) { let mut sum: usize = 0;
if tree { let dx = 3;
sum+=1; let dy = 1;
} let mut pos = (0, 0);
pos.0+=dx; while let Some(tree) = input.get(pos.0, pos.1) {
pos.1+=dy; if tree {
}; sum += 1;
sum }
} pos.0 += dx;
pos.1 += dy;
}
#[aoc(day3,part2)] sum
pub fn solve_part2(input: &Forest) -> usize { }
let mut prod: usize = 1;
for (dx,dy) in &[(1,1),(3,1),(5,1),(7,1),(1,2)] { #[aoc(day3, part2)]
let mut sum: usize=0; pub fn solve_part2(input: &Forest) -> usize {
let mut pos=(0,0); let mut prod: usize = 1;
while let Some(tree) = input.get(pos.0,pos.1) { for (dx, dy) in &[(1, 1), (3, 1), (5, 1), (7, 1), (1, 2)] {
if tree { let mut sum: usize = 0;
sum+=1; let mut pos = (0, 0);
} while let Some(tree) = input.get(pos.0, pos.1) {
pos.0+=dx; if tree {
pos.1+=dy; sum += 1;
}; }
prod*=sum; pos.0 += dx;
} pos.1 += dy;
prod }
} prod *= sum;
}
prod
}

View file

@ -1,5 +1,5 @@
use aoc_runner_derive::aoc_lib; use aoc_runner_derive::aoc_lib;
pub mod day1; pub mod day1;
pub mod day2; pub mod day2;
pub mod day3; pub mod day3;
aoc_lib! { year = 2020 } aoc_lib! { year = 2020 }

View file

@ -1,3 +1,3 @@
extern crate advent_of_code_2020; extern crate advent_of_code_2020;
use aoc_runner_derive::aoc_main; use aoc_runner_derive::aoc_main;
aoc_main! { lib = advent_of_code_2020 } aoc_main! { lib = advent_of_code_2020 }