day2
This commit is contained in:
parent
8ef11b2293
commit
583988cabf
@ -1,22 +1,9 @@
|
||||
use std::collections::HashSet;
|
||||
|
||||
pub struct Day1 {
|
||||
pub input: String,
|
||||
}
|
||||
|
||||
impl Day1 {
|
||||
pub fn new() -> Day1 {
|
||||
let input = include_bytes!("input");
|
||||
let input = String::from_utf8_lossy(input);
|
||||
Day1 {
|
||||
input: input.to_string(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn part_one(&self) {
|
||||
pub fn one(input: String) {
|
||||
let mut left: Vec<i32> = Vec::new();
|
||||
let mut right: Vec<i32> = Vec::new();
|
||||
self.input.lines().for_each(|line| {
|
||||
input.lines().for_each(|line| {
|
||||
let mut parts = line.split_whitespace();
|
||||
left.push(parts.next().unwrap().parse().unwrap());
|
||||
right.push(parts.next().unwrap().parse().unwrap());
|
||||
@ -28,24 +15,16 @@ impl Day1 {
|
||||
let res = left
|
||||
.iter()
|
||||
.zip(right.iter())
|
||||
.map(|(l, r)| Day1::abs(*l, *r))
|
||||
.map(|(l, r)| abs(*l, *r))
|
||||
.sum::<i32>();
|
||||
|
||||
println!("{}", res);
|
||||
}
|
||||
}
|
||||
|
||||
fn abs(a: i32, b: i32) -> i32 {
|
||||
if a > b {
|
||||
a - b
|
||||
} else {
|
||||
b - a
|
||||
}
|
||||
}
|
||||
|
||||
pub fn part_two(&self) {
|
||||
pub fn two(input: String) {
|
||||
let mut left: HashSet<i32> = HashSet::new();
|
||||
let mut right: Vec<i32> = Vec::new();
|
||||
self.input.lines().for_each(|line| {
|
||||
input.lines().for_each(|line| {
|
||||
let mut parts = line.split_whitespace();
|
||||
left.insert(parts.next().unwrap().parse().unwrap());
|
||||
right.push(parts.next().unwrap().parse().unwrap());
|
||||
@ -59,5 +38,13 @@ impl Day1 {
|
||||
}
|
||||
|
||||
println!("{}", acc);
|
||||
}
|
||||
|
||||
fn abs(a: i32, b: i32) -> i32 {
|
||||
if a > b {
|
||||
a - b
|
||||
} else {
|
||||
b - a
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
1000
src/day2/input
Normal file
1000
src/day2/input
Normal file
File diff suppressed because it is too large
Load Diff
66
src/day2/mod.rs
Normal file
66
src/day2/mod.rs
Normal file
@ -0,0 +1,66 @@
|
||||
pub fn one(input: String) {
|
||||
let count = input
|
||||
.lines()
|
||||
.map(|line| to_numbers(line))
|
||||
.filter(|numbers| is_safe(numbers))
|
||||
.count();
|
||||
println!("{}", count);
|
||||
}
|
||||
|
||||
pub fn two(input: String) {
|
||||
let count = input
|
||||
.lines()
|
||||
.map(|line| to_numbers(line))
|
||||
.filter(|numbers| is_safe_corrected(numbers))
|
||||
.count();
|
||||
println!("{}", count);
|
||||
}
|
||||
|
||||
fn to_numbers(line: &str) -> Vec<i32> {
|
||||
line.split_whitespace()
|
||||
.map(|n| n.parse().unwrap()) // All elements are numbers
|
||||
.collect::<Vec<i32>>()
|
||||
}
|
||||
|
||||
fn is_safe(numbers: &Vec<i32>) -> bool {
|
||||
let first = numbers.first().unwrap();
|
||||
let second = numbers.iter().nth(1).unwrap();
|
||||
let incr = if second - first > 0 { 1 } else { -1 };
|
||||
let mut cur = first;
|
||||
for i in numbers.iter().skip(1) {
|
||||
if !is_safe_interval(*cur, *i, incr) {
|
||||
return false;
|
||||
}
|
||||
|
||||
cur = i;
|
||||
}
|
||||
|
||||
true
|
||||
}
|
||||
|
||||
fn is_safe_corrected(numbers: &Vec<i32>) -> bool {
|
||||
if is_safe(numbers) {
|
||||
return true;
|
||||
}
|
||||
|
||||
let count = numbers.len();
|
||||
for i in 0..count {
|
||||
let mut copy = numbers.clone();
|
||||
copy.remove(i);
|
||||
if is_safe(©) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
false
|
||||
}
|
||||
|
||||
fn is_safe_interval(first: i32, second: i32, incr: i32) -> bool {
|
||||
let diff = second - first;
|
||||
if diff == 0 {
|
||||
return false;
|
||||
}
|
||||
|
||||
let norm = diff * incr;
|
||||
norm > 0 && norm < 4
|
||||
}
|
||||
14
src/main.rs
14
src/main.rs
@ -1,5 +1,11 @@
|
||||
use day1::Day1;
|
||||
mod day1;
|
||||
mod day2;
|
||||
|
||||
macro_rules! aoc_input {
|
||||
($day:expr) => {
|
||||
String::from_utf8_lossy(include_bytes!(concat!("day", $day, "/input"))).to_string()
|
||||
};
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let args = std::env::args().collect::<Vec<String>>();
|
||||
@ -11,8 +17,10 @@ fn main() {
|
||||
|
||||
let problem = args[1].as_str();
|
||||
match problem {
|
||||
"1.1" => Day1::new().part_one(),
|
||||
"1.2" => Day1::new().part_two(),
|
||||
"1.1" => day1::one(aoc_input!("1")),
|
||||
"1.2" => day1::two(aoc_input!("2")),
|
||||
"2.1" => day2::one(aoc_input!("1")),
|
||||
"2.2" => day2::two(aoc_input!("2")),
|
||||
_ => {
|
||||
println!("Bad input: {}", problem);
|
||||
std::process::exit(1);
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user