cargo fmt
This commit is contained in:
parent
0bb5d960e5
commit
336a03055d
5 changed files with 187 additions and 168 deletions
89
src/day1.rs
89
src/day1.rs
|
@ -1,40 +1,49 @@
|
|||
use aoc_runner_derive::{aoc,aoc_generator};
|
||||
|
||||
#[aoc_generator(day1)]
|
||||
pub fn input_generator(input: &str) -> Vec<usize> {
|
||||
input.lines().map(|l| l.trim().parse()).collect::<Result<Vec<usize>,_>>().unwrap()
|
||||
}
|
||||
|
||||
#[aoc(day1, part1)]
|
||||
pub fn solve_part1(input: &Vec<usize>) -> usize {
|
||||
let mut input=input.clone();
|
||||
input.sort();
|
||||
for (i1,v1) in input.iter().enumerate() {
|
||||
if v1>&2020 {break;}
|
||||
for v2 in input.iter().skip(i1+1) {
|
||||
if (v1+v2)==2020 {
|
||||
return v1*v2;
|
||||
}
|
||||
}
|
||||
}
|
||||
panic!("No pair summed to 20202!");
|
||||
}
|
||||
|
||||
|
||||
#[aoc(day1, part2)]
|
||||
pub fn solve_part2(input: &Vec<usize>) -> usize {
|
||||
let mut input=input.clone();
|
||||
input.sort();
|
||||
for (i1,v1) in input.iter().enumerate() {
|
||||
if v1>&2020 {break;}
|
||||
for (i2,v2) in input.iter().enumerate().skip(i1+1) {
|
||||
if (v1+v2)>2020 {break;}
|
||||
for v3 in input.iter().skip(i2+1) {
|
||||
if (v1+v2+v3)==2020 {
|
||||
return v1*v2*v3;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
panic!("No triplet summed to 20202!");
|
||||
}
|
||||
use aoc_runner_derive::{aoc, aoc_generator};
|
||||
|
||||
#[aoc_generator(day1)]
|
||||
pub fn input_generator(input: &str) -> Vec<usize> {
|
||||
input
|
||||
.lines()
|
||||
.map(|l| l.trim().parse())
|
||||
.collect::<Result<Vec<usize>, _>>()
|
||||
.unwrap()
|
||||
}
|
||||
|
||||
#[aoc(day1, part1)]
|
||||
pub fn solve_part1(input: &Vec<usize>) -> usize {
|
||||
let mut input = input.clone();
|
||||
input.sort();
|
||||
for (i1, v1) in input.iter().enumerate() {
|
||||
if v1 > &2020 {
|
||||
break;
|
||||
}
|
||||
for v2 in input.iter().skip(i1 + 1) {
|
||||
if (v1 + v2) == 2020 {
|
||||
return v1 * v2;
|
||||
}
|
||||
}
|
||||
}
|
||||
panic!("No pair summed to 20202!");
|
||||
}
|
||||
|
||||
#[aoc(day1, part2)]
|
||||
pub fn solve_part2(input: &Vec<usize>) -> usize {
|
||||
let mut input = input.clone();
|
||||
input.sort();
|
||||
for (i1, v1) in input.iter().enumerate() {
|
||||
if v1 > &2020 {
|
||||
break;
|
||||
}
|
||||
for (i2, v2) in input.iter().enumerate().skip(i1 + 1) {
|
||||
if (v1 + v2) > 2020 {
|
||||
break;
|
||||
}
|
||||
for v3 in input.iter().skip(i2 + 1) {
|
||||
if (v1 + v2 + v3) == 2020 {
|
||||
return v1 * v2 * v3;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
panic!("No triplet summed to 20202!");
|
||||
}
|
||||
|
|
143
src/day2.rs
143
src/day2.rs
|
@ -1,68 +1,75 @@
|
|||
use aoc_runner_derive::{aoc,aoc_generator};
|
||||
|
||||
#[derive(Debug,PartialEq,Eq,Ord,PartialOrd)]
|
||||
pub struct Rule {
|
||||
min: usize,
|
||||
max: usize,
|
||||
ch: char,
|
||||
pw: String
|
||||
}
|
||||
|
||||
impl Rule {
|
||||
fn is_valid_part1(&self) -> bool {
|
||||
let n=self.pw.chars().filter(|c| *c==self.ch).count();
|
||||
return n>=self.min && n<=self.max;
|
||||
}
|
||||
fn is_valid_part2(&self) -> bool {
|
||||
let idx_1=self.min-1;
|
||||
let idx_2=self.max-1;
|
||||
let chars: Vec<char> = self.pw.chars().collect();
|
||||
return (chars.get(idx_1)==Some(&self.ch))^(chars.get(idx_2)==Some(&self.ch))
|
||||
}
|
||||
}
|
||||
|
||||
#[aoc_generator(day2)]
|
||||
pub fn input_generator(input: &str) -> Vec<Rule> {
|
||||
input.lines().map(|l| {
|
||||
let mut ws = l.trim().split_whitespace();
|
||||
let range= ws.next().unwrap();
|
||||
let ch= ws.next().unwrap().trim_end_matches(|c| c==':').chars().next().unwrap();
|
||||
let pw= ws.next().unwrap().to_owned();
|
||||
let (r_min, r_max) = {
|
||||
let mut r=range.split('-');
|
||||
let r_min=r.next().unwrap().parse().unwrap();
|
||||
let r_max=r.next().unwrap().parse().unwrap();
|
||||
(r_min,r_max)
|
||||
};
|
||||
Rule {
|
||||
min:r_min,
|
||||
max:r_max,
|
||||
ch,
|
||||
pw
|
||||
}
|
||||
}).collect::<Vec<Rule>>()
|
||||
}
|
||||
|
||||
#[aoc(day2, part1)]
|
||||
pub fn solve_part1(input: &Vec<Rule>) -> usize {
|
||||
let mut total=0;
|
||||
for rule in input {
|
||||
if rule.is_valid_part1() {
|
||||
total+=1;
|
||||
}
|
||||
}
|
||||
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
|
||||
}
|
||||
|
||||
use aoc_runner_derive::{aoc, aoc_generator};
|
||||
|
||||
#[derive(Debug, PartialEq, Eq, Ord, PartialOrd)]
|
||||
pub struct Rule {
|
||||
min: usize,
|
||||
max: usize,
|
||||
ch: char,
|
||||
pw: String,
|
||||
}
|
||||
|
||||
impl Rule {
|
||||
fn is_valid_part1(&self) -> bool {
|
||||
let n = self.pw.chars().filter(|c| *c == self.ch).count();
|
||||
return n >= self.min && n <= self.max;
|
||||
}
|
||||
fn is_valid_part2(&self) -> bool {
|
||||
let idx_1 = self.min - 1;
|
||||
let idx_2 = self.max - 1;
|
||||
let chars: Vec<char> = self.pw.chars().collect();
|
||||
return (chars.get(idx_1) == Some(&self.ch)) ^ (chars.get(idx_2) == Some(&self.ch));
|
||||
}
|
||||
}
|
||||
|
||||
#[aoc_generator(day2)]
|
||||
pub fn input_generator(input: &str) -> Vec<Rule> {
|
||||
input
|
||||
.lines()
|
||||
.map(|l| {
|
||||
let mut ws = l.trim().split_whitespace();
|
||||
let range = ws.next().unwrap();
|
||||
let ch = ws
|
||||
.next()
|
||||
.unwrap()
|
||||
.trim_end_matches(|c| c == ':')
|
||||
.chars()
|
||||
.next()
|
||||
.unwrap();
|
||||
let pw = ws.next().unwrap().to_owned();
|
||||
let (r_min, r_max) = {
|
||||
let mut r = range.split('-');
|
||||
let r_min = r.next().unwrap().parse().unwrap();
|
||||
let r_max = r.next().unwrap().parse().unwrap();
|
||||
(r_min, r_max)
|
||||
};
|
||||
Rule {
|
||||
min: r_min,
|
||||
max: r_max,
|
||||
ch,
|
||||
pw,
|
||||
}
|
||||
})
|
||||
.collect::<Vec<Rule>>()
|
||||
}
|
||||
|
||||
#[aoc(day2, part1)]
|
||||
pub fn solve_part1(input: &Vec<Rule>) -> usize {
|
||||
let mut total = 0;
|
||||
for rule in input {
|
||||
if rule.is_valid_part1() {
|
||||
total += 1;
|
||||
}
|
||||
}
|
||||
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
|
||||
}
|
||||
|
|
111
src/day3.rs
111
src/day3.rs
|
@ -1,54 +1,57 @@
|
|||
use aoc_runner_derive::{aoc,aoc_generator};
|
||||
pub struct Forest {
|
||||
slice: Vec<Vec<bool>>,
|
||||
}
|
||||
|
||||
impl Forest {
|
||||
fn get(&self, x: usize,y: usize) -> Option<bool> {
|
||||
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{
|
||||
slice
|
||||
}
|
||||
}
|
||||
|
||||
#[aoc(day3,part1)]
|
||||
pub fn solve_part1(input: &Forest) -> usize {
|
||||
let mut sum: usize=0;
|
||||
let dx=3;
|
||||
let dy=1;
|
||||
let mut pos=(0,0);
|
||||
while let Some(tree) = input.get(pos.0,pos.1) {
|
||||
if tree {
|
||||
sum+=1;
|
||||
}
|
||||
pos.0+=dx;
|
||||
pos.1+=dy;
|
||||
};
|
||||
sum
|
||||
}
|
||||
|
||||
|
||||
#[aoc(day3,part2)]
|
||||
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)] {
|
||||
let mut sum: usize=0;
|
||||
let mut pos=(0,0);
|
||||
while let Some(tree) = input.get(pos.0,pos.1) {
|
||||
if tree {
|
||||
sum+=1;
|
||||
}
|
||||
pos.0+=dx;
|
||||
pos.1+=dy;
|
||||
};
|
||||
prod*=sum;
|
||||
}
|
||||
prod
|
||||
}
|
||||
use aoc_runner_derive::{aoc, aoc_generator};
|
||||
pub struct Forest {
|
||||
slice: Vec<Vec<bool>>,
|
||||
}
|
||||
|
||||
impl Forest {
|
||||
fn get(&self, x: usize, y: usize) -> Option<bool> {
|
||||
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 { slice }
|
||||
}
|
||||
|
||||
#[aoc(day3, part1)]
|
||||
pub fn solve_part1(input: &Forest) -> usize {
|
||||
let mut sum: usize = 0;
|
||||
let dx = 3;
|
||||
let dy = 1;
|
||||
let mut pos = (0, 0);
|
||||
while let Some(tree) = input.get(pos.0, pos.1) {
|
||||
if tree {
|
||||
sum += 1;
|
||||
}
|
||||
pos.0 += dx;
|
||||
pos.1 += dy;
|
||||
}
|
||||
sum
|
||||
}
|
||||
|
||||
#[aoc(day3, part2)]
|
||||
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)] {
|
||||
let mut sum: usize = 0;
|
||||
let mut pos = (0, 0);
|
||||
while let Some(tree) = input.get(pos.0, pos.1) {
|
||||
if tree {
|
||||
sum += 1;
|
||||
}
|
||||
pos.0 += dx;
|
||||
pos.1 += dy;
|
||||
}
|
||||
prod *= sum;
|
||||
}
|
||||
prod
|
||||
}
|
||||
|
|
10
src/lib.rs
10
src/lib.rs
|
@ -1,5 +1,5 @@
|
|||
use aoc_runner_derive::aoc_lib;
|
||||
pub mod day1;
|
||||
pub mod day2;
|
||||
pub mod day3;
|
||||
aoc_lib! { year = 2020 }
|
||||
use aoc_runner_derive::aoc_lib;
|
||||
pub mod day1;
|
||||
pub mod day2;
|
||||
pub mod day3;
|
||||
aoc_lib! { year = 2020 }
|
||||
|
|
|
@ -1,3 +1,3 @@
|
|||
extern crate advent_of_code_2020;
|
||||
use aoc_runner_derive::aoc_main;
|
||||
aoc_main! { lib = advent_of_code_2020 }
|
||||
aoc_main! { lib = advent_of_code_2020 }
|
||||
|
|
Loading…
Reference in a new issue