This commit is contained in:
StNicolay 2024-12-02 20:42:43 +03:00
parent c51d28eb75
commit d42778af2a
Signed by: StNicolay
GPG Key ID: 9693D04DCD962B0D
3 changed files with 118 additions and 32 deletions

20
Cargo.lock generated
View File

@ -1,7 +1,25 @@
# This file is automatically @generated by Cargo. # This file is automatically @generated by Cargo.
# It is not intended for manual editing. # It is not intended for manual editing.
version = 3 version = 4
[[package]]
name = "either"
version = "1.13.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "60b1af1c220855b6ceac025d3f6ecdd2b7c4894bfe9cd9bda4fbb4bc7c0d4cf0"
[[package]]
name = "itertools"
version = "0.13.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "413ee7dfc52ee1a4949ceeb7dbc8a33f2d6c088194d9f922fb8318faf1f01186"
dependencies = [
"either",
]
[[package]] [[package]]
name = "testing" name = "testing"
version = "0.1.0" version = "0.1.0"
dependencies = [
"itertools",
]

View File

@ -11,3 +11,4 @@ pedantic = "warn"
all = "warn" all = "warn"
[dependencies] [dependencies]
itertools = "0.13.0"

View File

@ -1,16 +1,24 @@
use std::{ use std::{
collections::HashMap,
fs::File, fs::File,
io::{BufRead, BufReader}, io::{BufRead as _, BufReader},
}; };
fn get_lines(name: &str) -> impl Iterator<Item = String> {
BufReader::new(File::open(name).unwrap())
.lines()
.map(|line| line.unwrap())
}
mod day1 {
use std::collections::HashMap;
use crate::get_lines;
#[allow(dead_code)] #[allow(dead_code)]
fn day1_1() { pub fn task1() {
let reader = BufReader::new(File::open("input.txt").unwrap());
let mut a = Vec::<i32>::new(); let mut a = Vec::<i32>::new();
let mut b = Vec::<i32>::new(); let mut b = Vec::<i32>::new();
for line in reader.lines() { for line in get_lines("input.txt") {
let line = line.unwrap();
let (a1, b1) = line.trim_end().split_once(" ").unwrap(); let (a1, b1) = line.trim_end().split_once(" ").unwrap();
a.push(a1.parse().unwrap()); a.push(a1.parse().unwrap());
b.push(b1.parse().unwrap()); b.push(b1.parse().unwrap());
@ -22,12 +30,10 @@ fn day1_1() {
} }
#[allow(dead_code)] #[allow(dead_code)]
fn day1_2() { pub fn task2() {
let reader = BufReader::new(File::open("input.txt").unwrap());
let mut a = Vec::<u32>::new(); let mut a = Vec::<u32>::new();
let mut b = HashMap::<u32, u32>::new(); let mut b = HashMap::<u32, u32>::new();
for line in reader.lines() { for line in get_lines("input.txt") {
let line = line.unwrap();
let (a1, b1) = line.trim_end().split_once(" ").unwrap(); let (a1, b1) = line.trim_end().split_once(" ").unwrap();
a.push(a1.parse().unwrap()); a.push(a1.parse().unwrap());
*b.entry(b1.parse().unwrap()).or_insert(0) += 1; *b.entry(b1.parse().unwrap()).or_insert(0) += 1;
@ -35,8 +41,69 @@ fn day1_2() {
let result: u32 = a.into_iter().map(|a| a * *b.get(&a).unwrap_or(&0)).sum(); let result: u32 = a.into_iter().map(|a| a * *b.get(&a).unwrap_or(&0)).sum();
println!("{result}"); println!("{result}");
} }
}
mod day2 {
use itertools::Itertools;
use crate::get_lines;
fn safe(iter: impl Iterator<Item = i32>) -> bool {
let mut order = None;
for (a, b) in iter.tuple_windows() {
if !(1..4).contains(&(a - b).abs()) {
return false;
}
if order.is_none() {
order = Some(a.cmp(&b));
} else if order != Some(a.cmp(&b)) {
return false;
}
}
true
}
fn parse_line(line: &str) -> impl Iterator<Item = i32> + '_ {
line.trim()
.split(' ')
.map(|num| num.parse::<i32>().unwrap())
}
#[allow(dead_code)]
pub fn task1() {
let result = get_lines("input.txt")
.filter(|line| safe(parse_line(line)))
.count();
println!("{result}");
}
#[allow(dead_code)]
pub fn task2() {
let result = get_lines("input.txt")
.filter(|line| {
let numbers = parse_line(line).collect_vec();
if safe(numbers.iter().copied()) {
return true;
}
let mut test = Vec::with_capacity(numbers.len() - 1);
for i in 0..numbers.len() {
test.extend_from_slice(&numbers[..i]);
test.extend_from_slice(&numbers[i + 1..]);
if safe(test.iter().copied()) {
return true;
}
test.clear();
}
false
})
.count();
println!("{result}");
}
}
fn main() { fn main() {
day1_1(); // day1::task1();
day1_2(); // day1::task1();
day2::task1();
day2::task2();
} }