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