cargo fmt
This commit is contained in:
parent
0bb5d960e5
commit
336a03055d
5 changed files with 187 additions and 168 deletions
19
src/day1.rs
19
src/day1.rs
|
@ -2,7 +2,11 @@ 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()
|
||||
input
|
||||
.lines()
|
||||
.map(|l| l.trim().parse())
|
||||
.collect::<Result<Vec<usize>, _>>()
|
||||
.unwrap()
|
||||
}
|
||||
|
||||
#[aoc(day1, part1)]
|
||||
|
@ -10,7 +14,9 @@ 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;}
|
||||
if v1 > &2020 {
|
||||
break;
|
||||
}
|
||||
for v2 in input.iter().skip(i1 + 1) {
|
||||
if (v1 + v2) == 2020 {
|
||||
return v1 * v2;
|
||||
|
@ -20,15 +26,18 @@ pub fn solve_part1(input: &Vec<usize>) -> usize {
|
|||
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;}
|
||||
if v1 > &2020 {
|
||||
break;
|
||||
}
|
||||
for (i2, v2) in input.iter().enumerate().skip(i1 + 1) {
|
||||
if (v1+v2)>2020 {break;}
|
||||
if (v1 + v2) > 2020 {
|
||||
break;
|
||||
}
|
||||
for v3 in input.iter().skip(i2 + 1) {
|
||||
if (v1 + v2 + v3) == 2020 {
|
||||
return v1 * v2 * v3;
|
||||
|
|
23
src/day2.rs
23
src/day2.rs
|
@ -5,7 +5,7 @@ pub struct Rule {
|
|||
min: usize,
|
||||
max: usize,
|
||||
ch: char,
|
||||
pw: String
|
||||
pw: String,
|
||||
}
|
||||
|
||||
impl Rule {
|
||||
|
@ -17,16 +17,24 @@ impl Rule {
|
|||
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))
|
||||
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| {
|
||||
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 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('-');
|
||||
|
@ -38,9 +46,10 @@ pub fn input_generator(input: &str) -> Vec<Rule> {
|
|||
min: r_min,
|
||||
max: r_max,
|
||||
ch,
|
||||
pw
|
||||
pw,
|
||||
}
|
||||
}).collect::<Vec<Rule>>()
|
||||
})
|
||||
.collect::<Vec<Rule>>()
|
||||
}
|
||||
|
||||
#[aoc(day2, part1)]
|
||||
|
@ -54,7 +63,6 @@ pub fn solve_part1(input: &Vec<Rule>) -> usize {
|
|||
total
|
||||
}
|
||||
|
||||
|
||||
#[aoc(day2, part2)]
|
||||
pub fn solve_part2(input: &Vec<Rule>) -> usize {
|
||||
let mut total = 0;
|
||||
|
@ -65,4 +73,3 @@ pub fn solve_part2(input: &Vec<Rule>) -> usize {
|
|||
}
|
||||
total
|
||||
}
|
||||
|
||||
|
|
21
src/day3.rs
21
src/day3.rs
|
@ -5,17 +5,21 @@ pub struct Forest {
|
|||
|
||||
impl Forest {
|
||||
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{
|
||||
slice
|
||||
}
|
||||
let slice = input
|
||||
.lines()
|
||||
.map(|l| l.chars().map(|c| c == '#').collect())
|
||||
.collect();
|
||||
Forest { slice }
|
||||
}
|
||||
|
||||
#[aoc(day3, part1)]
|
||||
|
@ -30,11 +34,10 @@ pub fn solve_part1(input: &Forest) -> usize {
|
|||
}
|
||||
pos.0 += dx;
|
||||
pos.1 += dy;
|
||||
};
|
||||
}
|
||||
sum
|
||||
}
|
||||
|
||||
|
||||
#[aoc(day3, part2)]
|
||||
pub fn solve_part2(input: &Forest) -> usize {
|
||||
let mut prod: usize = 1;
|
||||
|
@ -47,7 +50,7 @@ pub fn solve_part2(input: &Forest) -> usize {
|
|||
}
|
||||
pos.0 += dx;
|
||||
pos.1 += dy;
|
||||
};
|
||||
}
|
||||
prod *= sum;
|
||||
}
|
||||
prod
|
||||
|
|
Loading…
Reference in a new issue