add: files so far
This commit is contained in:
@@ -0,0 +1,22 @@
|
||||
use std::collections::BinaryHeap;
|
||||
use std::io;
|
||||
|
||||
fn main() -> io::Result<()> {
|
||||
let mut l1 = BinaryHeap::<u64>::new();
|
||||
let mut l2 = BinaryHeap::<u64>::new();
|
||||
for line in io::stdin().lines() {
|
||||
let line = line?;
|
||||
let parts: Vec<&str> = line.trim().split_whitespace().collect();
|
||||
let (n1, n2) = (parts[0], parts[1]);
|
||||
l1.push(n1.parse().unwrap());
|
||||
l2.push(n2.parse().unwrap());
|
||||
}
|
||||
let l1 = l1.into_sorted_vec();
|
||||
let l2 = l2.into_sorted_vec();
|
||||
let mut sum = 0;
|
||||
for (n1, n2) in l1.into_iter().zip(l2.into_iter()) {
|
||||
sum += n1.abs_diff(n2);
|
||||
}
|
||||
println!("{}", sum);
|
||||
Ok(())
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
3 4
|
||||
4 3
|
||||
2 5
|
||||
1 3
|
||||
3 9
|
||||
3 3
|
||||
@@ -0,0 +1,21 @@
|
||||
use std::collections::HashMap;
|
||||
use std::io;
|
||||
|
||||
fn main() -> io::Result<()> {
|
||||
let mut l1: Vec<u64> = vec!();
|
||||
let mut l2: HashMap<u64, u64> = HashMap::default();
|
||||
for line in io::stdin().lines() {
|
||||
let line = line?;
|
||||
let parts: Vec<&str> = line.trim().split_whitespace().collect();
|
||||
let (n1, n2) = (parts[0], parts[1]);
|
||||
l1.push(n1.parse().unwrap());
|
||||
let v = l2.entry(n2.parse::<u64>().unwrap()).or_insert(0);
|
||||
*v += 1;
|
||||
}
|
||||
let mut sum = 0;
|
||||
for n1 in l1.into_iter() {
|
||||
sum += n1*l2.get(&n1).unwrap_or(&0);
|
||||
}
|
||||
println!("{}", sum);
|
||||
Ok(())
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
use advent_of_code_2024::{make_main, SResult};
|
||||
|
||||
make_main!();
|
||||
|
||||
// CODE
|
||||
|
||||
#[derive(PartialEq)]
|
||||
enum Direction {
|
||||
Up,
|
||||
Down,
|
||||
}
|
||||
|
||||
impl Direction {
|
||||
fn from(a: usize, b: usize) -> Self {
|
||||
if a > b {
|
||||
Direction::Down
|
||||
} else {
|
||||
Direction::Up
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn solve(lines: Vec<String>) -> SResult<usize> {
|
||||
let mut failed = 0;
|
||||
for line in lines.iter() {
|
||||
let reports: Vec<usize> = line.split_whitespace().map(|v| v.parse::<usize>().unwrap()).collect();
|
||||
match test(&reports) {
|
||||
Ok(_) => (),
|
||||
Err(_) => failed += 1,
|
||||
};
|
||||
}
|
||||
Ok(lines.len() - failed)
|
||||
}
|
||||
|
||||
// Tests the report, and returns indexes around where a failure is detected.
|
||||
fn test(reports: &[usize]) -> Result<(), ()> {
|
||||
let mut i = 0;
|
||||
let direction = Direction::from(reports[0], reports[1]);
|
||||
while i + 1 < reports.len() {
|
||||
let delta = reports[i].abs_diff(reports[i + 1]);
|
||||
if delta < 1 || delta > 3 || direction != Direction::from(reports[i], reports[i + 1]) {
|
||||
return Err(());
|
||||
}
|
||||
i += 1;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
// CODE
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use advent_of_code_2024::input;
|
||||
|
||||
use super::*;
|
||||
#[test]
|
||||
fn sample_input() {
|
||||
let strings: Vec<String> = input!("d2p1.txt");
|
||||
let got = solve(strings).unwrap();
|
||||
assert_eq!(got, 2);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
7 6 4 2 1
|
||||
1 2 7 8 9
|
||||
9 7 6 2 1
|
||||
1 3 2 4 5
|
||||
8 6 4 4 1
|
||||
1 3 6 7 9
|
||||
@@ -0,0 +1,80 @@
|
||||
use advent_of_code_2024::{make_main, SResult};
|
||||
|
||||
make_main!();
|
||||
|
||||
// CODE
|
||||
#[derive(PartialEq)]
|
||||
enum Direction {
|
||||
Up,
|
||||
Down,
|
||||
}
|
||||
|
||||
impl Direction {
|
||||
fn from(a: usize, b: usize) -> Self {
|
||||
if a > b {
|
||||
Direction::Down
|
||||
} else {
|
||||
Direction::Up
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn solve(lines: Vec<String>) -> SResult<usize> {
|
||||
let mut fail_count = 0;
|
||||
for line in lines.iter() {
|
||||
let reports: Vec<usize> = line
|
||||
.split_whitespace()
|
||||
.map(|v| v.parse::<usize>().unwrap())
|
||||
.collect();
|
||||
let problems = match test(&reports) {
|
||||
// Cool, nothing to see here
|
||||
Ok(_) => continue,
|
||||
Err(e) => e,
|
||||
};
|
||||
let mut pass = false;
|
||||
for problem in problems {
|
||||
let mut l1 = reports.clone();
|
||||
l1.remove(problem);
|
||||
if test(&l1).is_ok() {
|
||||
pass = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if !pass {
|
||||
fail_count += 1;
|
||||
}
|
||||
}
|
||||
Ok(lines.len() - fail_count)
|
||||
}
|
||||
|
||||
// Tests the report, and returns indexes around where a failure is detected.
|
||||
fn test(reports: &[usize]) -> Result<(), Vec<usize>> {
|
||||
let mut i = 0;
|
||||
let direction = Direction::from(reports[0], reports[1]);
|
||||
while i + 1 < reports.len() {
|
||||
let delta = reports[i].abs_diff(reports[i + 1]);
|
||||
if delta < 1 || delta > 3 || direction != Direction::from(reports[i], reports[i + 1]) {
|
||||
let mut errs = vec![i, i + 1];
|
||||
if i > 0 {
|
||||
errs.push(i - 1);
|
||||
}
|
||||
return Err(errs);
|
||||
}
|
||||
i += 1;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
// CODE
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use advent_of_code_2024::input;
|
||||
|
||||
use super::*;
|
||||
#[test]
|
||||
fn sample_input() {
|
||||
let strings: Vec<String> = input!("d2p1.txt");
|
||||
let got = solve(strings).unwrap();
|
||||
assert_eq!(got, 4);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user