Caved on the itertools crate smh
This commit is contained in:
Tyler Beckman 2023-12-10 22:55:09 -07:00
parent 323afc1cbd
commit 0efc64a4ac
Signed by: Ty
GPG key ID: 2813440C772555A4
5 changed files with 138 additions and 3 deletions

16
Cargo.lock generated
View file

@ -22,6 +22,7 @@ name = "advent"
version = "0.1.0"
dependencies = [
"getrandom",
"itertools",
"num-bigint",
"num-irrational",
"num-rational",
@ -318,6 +319,12 @@ version = "0.6.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3a68a4904193147e0a8dec3314640e6db742afd5f6e634f428a6af230d9b3591"
[[package]]
name = "either"
version = "1.9.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a26ae43d7bcc3b814de94796a5e736d4029efb0ee900c12e2d54c993ad1a1e07"
[[package]]
name = "encoding_rs"
version = "0.8.33"
@ -613,6 +620,15 @@ version = "2.9.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8f518f335dce6725a761382244631d86cf0ccb2863413590b31338feb467f9c3"
[[package]]
name = "itertools"
version = "0.12.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "25db6b064527c5d482d0423354fcd07a89a2dfe07b67892e62411946db7f07b0"
dependencies = [
"either",
]
[[package]]
name = "itoa"
version = "1.0.9"

View file

@ -10,7 +10,8 @@ num-rational = { version = "0.4.1", features = ["num-bigint"] }
regex = "1.10.2"
scraper = "0.18.1"
reqwest = "0.11.22"
itertools = "0.12.0"
[dependencies.getrandom]
version = "0.2.11"
features = ["js"]
features = ["js"]

115
lib/src/day11/mod.rs Normal file
View file

@ -0,0 +1,115 @@
use crate::utils::Day;
use itertools::Itertools as _;
#[derive(Debug, Default)]
pub struct Day11 {
pub input: String,
pub rows_expanded: Vec<usize>,
pub columns_expanded: Vec<usize>,
}
impl Day for Day11 {
fn part1(&mut self) -> String {
let mut parsed: Vec<Vec<_>> = self
.input
.lines()
.map(|line| line.chars().collect())
.collect();
for line in std::mem::take(&mut parsed).into_iter() {
if line.iter().all(|&c| c == '.') {
parsed.push(line.clone())
}
parsed.push(line)
}
let parsed_clone = parsed.clone();
let mut added = 0usize;
for column in 0..parsed[0].len() {
if (0..parsed.len()).all(|row| parsed_clone[row][column] == '.') {
parsed
.iter_mut()
.for_each(|line| line.insert(column + added, '.'));
added += 1;
}
}
let galaxies: Vec<_> = parsed
.iter()
.enumerate()
.flat_map(|(i, line)| {
line.iter()
.enumerate()
.filter_map(move |(j, &c)| if c == '#' { Some((i as isize, j as isize)) } else { None })
})
.collect();
let combinations = galaxies
.into_iter()
.combinations(2)
.map(|c| (c[0], c[1]))
.collect::<Vec<_>>();
let result = combinations.into_iter().map(
|(start, end)|
(end.0 - start.0).abs() + (end.1 - start.1).abs()
).sum::<isize>();
result.to_string()
}
fn part2(&mut self) -> String {
let parsed: Vec<Vec<_>> = self
.input
.lines()
.map(|line| line.chars().collect())
.collect();
for (i, line) in parsed.iter().enumerate() {
if line.iter().all(|&c| c == '.') {
self.rows_expanded.push(i);
}
}
let parsed_clone = parsed.clone();
for column in 0..parsed[0].len() {
if (0..parsed.len()).all(|row| parsed_clone[row][column] == '.') {
self.columns_expanded.push(column);
}
}
let galaxies: Vec<_> = parsed
.iter()
.enumerate()
.flat_map(|(i, line)| {
line.iter()
.enumerate()
.filter_map({
let rows_expanded = &self.rows_expanded;
let columns_expanded = &self.columns_expanded;
move |(j, &c)| {
if c == '#' {
Some((
i as i128 + (rows_expanded.iter().filter(|&&row| row < i).count() as i128 * 999999i128),
j as i128 + (columns_expanded.iter().filter(|&&column| column < j).count() as i128 * 999999i128)
))
} else { None }
}
})
})
.collect();
let combinations = galaxies
.into_iter()
.combinations(2)
.map(|c| (c[0], c[1]))
.collect::<Vec<_>>();
let result = combinations.into_iter().map(
|(start, end)|
(end.0 - start.0).abs() + (end.1 - start.1).abs()
).sum::<i128>();
result.to_string()
}
}

View file

@ -2,6 +2,7 @@ use std::collections::HashMap;
use day1::Day1;
use day10::Day10;
use day11::Day11;
use day2::Day2;
use day3::Day3;
use day4::Day4;
@ -17,6 +18,7 @@ pub mod utils;
pub mod day1;
pub mod day10;
pub mod day11;
pub mod day2;
pub mod day3;
pub mod day4;
@ -51,6 +53,7 @@ pub fn get_day(day: u8, input: String) -> Box<dyn Day> {
input,
parsed: vec![],
}),
11 => Box::new(Day11 { input, rows_expanded: vec![], columns_expanded: vec![] }),
_ => panic!("Invalid day #"),
}
}
}

View file

@ -3,4 +3,4 @@ use wasm_bindgen::prelude::*;
#[wasm_bindgen]
pub async fn solve(day: u8, part: u8, auth: String) -> String {
advent::get_day(day, advent::fetcher::fetch_input(auth, day).await).solve(part, false)
}
}