From d47441975d473846c3f5e4aea389031952179ee2 Mon Sep 17 00:00:00 2001 From: Johannes Date: Thu, 27 Jun 2024 21:54:14 +0200 Subject: [PATCH] Cargo fmt --- src/day10.rs | 5 ++-- src/day11.rs | 33 ++++++++++++++------------ src/day12.rs | 6 ++--- src/day13.rs | 67 ++++++++++++++++++++++++++++++++-------------------- src/day14.rs | 24 ++++++++++++++----- src/day15.rs | 55 +++++++++++++++++++++++++++++++++--------- src/day16.rs | 64 ++++++++++++++++++++++++++----------------------- src/main.rs | 2 +- 8 files changed, 163 insertions(+), 93 deletions(-) diff --git a/src/day10.rs b/src/day10.rs index c3d63c2..cd85dcb 100644 --- a/src/day10.rs +++ b/src/day10.rs @@ -1,5 +1,5 @@ -use std::collections::VecDeque; use aoc_runner_derive::{aoc, aoc_generator}; +use std::collections::VecDeque; #[aoc_generator(day10)] fn parse(input: &str) -> Vec { input.chars().collect() @@ -36,7 +36,6 @@ fn part2(input: &Vec) -> usize { solve(input, 50).len() } - #[cfg(test)] mod tests { use super::*; @@ -45,4 +44,4 @@ mod tests { fn part1_example() { assert_eq!(solve(&parse("111221"), 1), "312211"); } -} \ No newline at end of file +} diff --git a/src/day11.rs b/src/day11.rs index 82d0a5e..026b07d 100644 --- a/src/day11.rs +++ b/src/day11.rs @@ -2,27 +2,30 @@ use aoc_runner_derive::aoc; #[aoc(day11, part1)] fn part1(input: &str) -> String { - PasswordCounter::from(input.to_owned()).find(|p| { - let r1 = rule1(&p); - let r2 = rule2(&p); - let r3 = rule3(&p); - r1 && r2 && r3 - }).unwrap() + PasswordCounter::from(input.to_owned()) + .find(|p| { + let r1 = rule1(&p); + let r2 = rule2(&p); + let r3 = rule3(&p); + r1 && r2 && r3 + }) + .unwrap() } #[aoc(day11, part2)] fn part2(input: &str) -> String { - PasswordCounter::from(input.to_owned()).filter(|p| { - let r1 = rule1(&p); - let r2 = rule2(&p); - let r3 = rule3(&p); - r1 && r2 && r3 - }) + PasswordCounter::from(input.to_owned()) + .filter(|p| { + let r1 = rule1(&p); + let r2 = rule2(&p); + let r3 = rule3(&p); + r1 && r2 && r3 + }) .skip(1) - .next().unwrap() + .next() + .unwrap() } - fn rule1(pw: &str) -> bool { let pw = pw.as_bytes(); for i in 0..pw.len() - 2 { @@ -86,4 +89,4 @@ impl Iterator for PasswordCounter { } #[cfg(test)] -mod tests {} \ No newline at end of file +mod tests {} diff --git a/src/day12.rs b/src/day12.rs index 97d3a6f..47089d3 100644 --- a/src/day12.rs +++ b/src/day12.rs @@ -25,9 +25,9 @@ fn sum(json: &JsonValue) -> i64 { JsonValue::Number(_) => json.as_i64().unwrap(), JsonValue::Boolean(_) => 0, JsonValue::Object(o) => { - if o.iter().any(|(_, value)| { - value.is_string() && Some("red") == value.as_str() - }) { + if o.iter() + .any(|(_, value)| value.is_string() && Some("red") == value.as_str()) + { 0 } else { o.iter().map(|(_key, value)| sum(value)).sum() diff --git a/src/day13.rs b/src/day13.rs index 2310b74..ae055ca 100644 --- a/src/day13.rs +++ b/src/day13.rs @@ -4,7 +4,8 @@ use aoc_runner_derive::{aoc, aoc_generator}; #[aoc_generator(day13)] fn parse(input: &str) -> HashMap<(String, String), i32> { - input.lines() + input + .lines() .map(|line| { let v: Vec<&str> = line[0..line.len() - 1].split(" ").collect(); let from = v[0]; @@ -14,21 +15,26 @@ fn parse(input: &str) -> HashMap<(String, String), i32> { value *= -1; } ((from.into(), to.into()), value) - }).collect() + }) + .collect() } #[aoc(day13, part1)] fn part1(deltas: &HashMap<(String, String), i32>) -> i32 { let names: HashSet<_> = deltas.keys().map(|s| s.0.clone()).collect(); - permutations(names).iter().map(|arrangement| { - let mut happiness = 0; - for i in 0..arrangement.len() { - let right = (i + 1) % arrangement.len(); - happiness += deltas[&(arrangement[right].clone(), arrangement[i].clone())] + - deltas[&(arrangement[i].clone(), arrangement[right].clone())]; - } - happiness - }).max().unwrap() + permutations(names) + .iter() + .map(|arrangement| { + let mut happiness = 0; + for i in 0..arrangement.len() { + let right = (i + 1) % arrangement.len(); + happiness += deltas[&(arrangement[right].clone(), arrangement[i].clone())] + + deltas[&(arrangement[i].clone(), arrangement[right].clone())]; + } + happiness + }) + .max() + .unwrap() } fn permutations(values: HashSet) -> Vec> { @@ -42,30 +48,41 @@ fn p(mut input: Vec) -> Vec> { } let mut results = Vec::new(); for _ in 0..input.len() { - p(input.iter().skip(1).map(|t| t.clone()).collect()).into_iter().for_each(|mut v| { - v.insert(0, input[0].clone()); - results.push(v); - }); + p(input.iter().skip(1).map(|t| t.clone()).collect()) + .into_iter() + .for_each(|mut v| { + v.insert(0, input[0].clone()); + results.push(v); + }); let v = input.remove(0); input.push(v); } results } - #[aoc(day13, part2)] fn part2(deltas: &HashMap<(String, String), i32>) -> i32 { let mut names: HashSet<_> = deltas.keys().map(|s| s.0.clone()).collect(); names.insert("me".to_string()); - permutations(names).iter().map(|arrangement| { - let mut happiness = 0; - for i in 0..arrangement.len() { - let right = (i + 1) % arrangement.len(); - happiness += deltas.get(&(arrangement[right].clone(), arrangement[i].clone())).or(Some(&0)).unwrap() + - deltas.get(&(arrangement[i].clone(), arrangement[right].clone())).or(Some(&0)).unwrap(); - } - happiness - }).max().unwrap() + permutations(names) + .iter() + .map(|arrangement| { + let mut happiness = 0; + for i in 0..arrangement.len() { + let right = (i + 1) % arrangement.len(); + happiness += deltas + .get(&(arrangement[right].clone(), arrangement[i].clone())) + .or(Some(&0)) + .unwrap() + + deltas + .get(&(arrangement[i].clone(), arrangement[right].clone())) + .or(Some(&0)) + .unwrap(); + } + happiness + }) + .max() + .unwrap() } #[cfg(test)] diff --git a/src/day14.rs b/src/day14.rs index 0f887d7..35c21c9 100644 --- a/src/day14.rs +++ b/src/day14.rs @@ -2,7 +2,8 @@ use aoc_runner_derive::{aoc, aoc_generator}; #[aoc_generator(day14)] fn parse(input: &str) -> Vec<(usize, usize, usize)> { - input.lines() + input + .lines() .map(|line| { // Rudolph can fly 22 km/s for 8 seconds, but then must rest for 165 seconds. let parts: Vec<&str> = line.split(" ").collect(); @@ -17,7 +18,8 @@ fn parse(input: &str) -> Vec<(usize, usize, usize)> { #[aoc(day14, part1)] fn part1(input: &Vec<(usize, usize, usize)>) -> usize { let seconds = 2503; - input.into_iter() + input + .into_iter() .map(|&(speed, fly, rest)| { let cycle = fly + rest; let full = seconds / cycle; @@ -47,8 +49,10 @@ fn play(input: &Vec<(usize, usize, usize)>, rounds: usize) -> usize { } let max_distance = state.iter().map(|it| it.0).max().unwrap(); - state.iter_mut().for_each(|it| if it.0 == max_distance { - it.1 += 1; + state.iter_mut().for_each(|it| { + if it.0 == max_distance { + it.1 += 1; + } }); } state.into_iter().map(|it| it.1).max().unwrap() @@ -60,7 +64,15 @@ mod test { #[test] fn example2() { - assert_eq!(689, play(&parse("Comet can fly 14 km/s for 10 seconds, but then must rest for 127 seconds. -Dancer can fly 16 km/s for 11 seconds, but then must rest for 162 seconds."), 1000)) + assert_eq!( + 689, + play( + &parse( + "Comet can fly 14 km/s for 10 seconds, but then must rest for 127 seconds. +Dancer can fly 16 km/s for 11 seconds, but then must rest for 162 seconds." + ), + 1000 + ) + ) } } diff --git a/src/day15.rs b/src/day15.rs index 6517d9d..32a3e14 100644 --- a/src/day15.rs +++ b/src/day15.rs @@ -4,10 +4,14 @@ use aoc_runner_derive::{aoc, aoc_generator}; #[aoc_generator(day15)] fn parse(input: &str) -> Vec { - input.lines() + input + .lines() .map(|line| { // Sprinkles: capacity 5, durability -1, flavor 0, texture 0, calories 5 - let parts: Vec<&str> = line.split(" ").map(|part| part.trim_end_matches(",")).collect(); + let parts: Vec<&str> = line + .split(" ") + .map(|part| part.trim_end_matches(",")) + .collect(); let capacity = parts[2].parse().unwrap(); let durability = parts[4].parse().unwrap(); let flavor = parts[6].parse().unwrap(); @@ -34,10 +38,22 @@ fn part1(input: &[Ingredient]) -> i32 { if a + b + c + d != 100 { continue; } - let mut capacity = a * input[0].capacity + b * input[1].capacity + c * input[2].capacity + d * input[3].capacity; - let mut durability = a * input[0].durability + b * input[1].durability + c * input[2].durability + d * input[3].durability; - let mut flavor = a * input[0].flavor + b * input[1].flavor + c * input[2].flavor + d * input[3].flavor; - let mut texture = a * input[0].texture + b * input[1].texture + c * input[2].texture + d * input[3].texture; + let mut capacity = a * input[0].capacity + + b * input[1].capacity + + c * input[2].capacity + + d * input[3].capacity; + let mut durability = a * input[0].durability + + b * input[1].durability + + c * input[2].durability + + d * input[3].durability; + let mut flavor = a * input[0].flavor + + b * input[1].flavor + + c * input[2].flavor + + d * input[3].flavor; + let mut texture = a * input[0].texture + + b * input[1].texture + + c * input[2].texture + + d * input[3].texture; if capacity < 0 { capacity = 0; } @@ -68,13 +84,30 @@ fn part2(input: &[Ingredient]) -> i32 { if a + b + c + d != 100 { continue; } - if a * input[0].calories + b * input[1].calories + c * input[2].calories + d * input[3].calories != 500 { + if a * input[0].calories + + b * input[1].calories + + c * input[2].calories + + d * input[3].calories + != 500 + { continue; } - let mut capacity = a * input[0].capacity + b * input[1].capacity + c * input[2].capacity + d * input[3].capacity; - let mut durability = a * input[0].durability + b * input[1].durability + c * input[2].durability + d * input[3].durability; - let mut flavor = a * input[0].flavor + b * input[1].flavor + c * input[2].flavor + d * input[3].flavor; - let mut texture = a * input[0].texture + b * input[1].texture + c * input[2].texture + d * input[3].texture; + let mut capacity = a * input[0].capacity + + b * input[1].capacity + + c * input[2].capacity + + d * input[3].capacity; + let mut durability = a * input[0].durability + + b * input[1].durability + + c * input[2].durability + + d * input[3].durability; + let mut flavor = a * input[0].flavor + + b * input[1].flavor + + c * input[2].flavor + + d * input[3].flavor; + let mut texture = a * input[0].texture + + b * input[1].texture + + c * input[2].texture + + d * input[3].texture; if capacity < 0 { capacity = 0; } diff --git a/src/day16.rs b/src/day16.rs index 6006720..89e0219 100644 --- a/src/day16.rs +++ b/src/day16.rs @@ -4,14 +4,18 @@ use aoc_runner_derive::{aoc, aoc_generator}; #[aoc_generator(day16)] fn parse(input: &str) -> Vec { - input.lines() + input + .lines() .map(|line| { let (_, hints) = line.split_once(": ").unwrap(); let hints: Vec<_> = hints.split(", ").collect(); - hints.into_iter().map(|thing| { - let (name, count) = thing.split_once(": ").unwrap(); - (name.to_string(), count.parse().unwrap()) - }).collect() + hints + .into_iter() + .map(|thing| { + let (name, count) = thing.split_once(": ").unwrap(); + (name.to_string(), count.parse().unwrap()) + }) + .collect() }) .collect() } @@ -28,21 +32,24 @@ goldfish: 5 trees: 3 cars: 2 perfumes: 1"; - let known: Sue = known.split("\n") + let known: Sue = known + .split("\n") .map(|line| { let (name, count) = line.split_once(": ").unwrap(); (name.to_string(), count.parse().unwrap()) - }).collect(); + }) + .collect(); - let sue = input.iter() + let sue = input + .iter() .enumerate() .filter(|(_index, sue)| { - known.iter() - .all(|(property, _)| { - !sue.contains_key(property) || known.get(property) == sue.get(property) - }) + known.iter().all(|(property, _)| { + !sue.contains_key(property) || known.get(property) == sue.get(property) + }) }) - .next().unwrap(); + .next() + .unwrap(); sue.0 + 1 } @@ -62,21 +69,19 @@ trees: 3"; goldfish: 5"; let lt = foo(lt); - let sues: Vec<_> = input.iter() + let sues: Vec<_> = input + .iter() .enumerate() .filter(|(_index, sue)| { - exact.iter() - .all(|(property, _)| { - !sue.contains_key(property) || exact.get(property) == sue.get(property) - }) - && gt.iter() - .all(|(property, _)| { - !sue.contains_key(property) || gt.get(property).unwrap() < sue.get(property).unwrap() - }) - && lt.iter() - .all(|(property, _)| { - !sue.contains_key(property) || lt.get(property).unwrap() > sue.get(property).unwrap() - }) + exact.iter().all(|(property, _)| { + !sue.contains_key(property) || exact.get(property) == sue.get(property) + }) && gt.iter().all(|(property, _)| { + !sue.contains_key(property) + || gt.get(property).unwrap() < sue.get(property).unwrap() + }) && lt.iter().all(|(property, _)| { + !sue.contains_key(property) + || lt.get(property).unwrap() > sue.get(property).unwrap() + }) }) .collect(); dbg!(&sues); @@ -87,13 +92,14 @@ goldfish: 5"; // 213 too low fn foo(exact: &str) -> Sue { - let exact: Sue = exact.split("\n") + let exact: Sue = exact + .split("\n") .map(|line| { let (name, count) = line.split_once(": ").unwrap(); (name.to_string(), count.parse().unwrap()) - }).collect(); + }) + .collect(); exact } type Sue = HashMap; - diff --git a/src/main.rs b/src/main.rs index 61404f5..a85c351 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,6 +1,6 @@ extern crate advent_of_code_2015; -extern crate aoc_runner_derive; extern crate aoc_runner; +extern crate aoc_runner_derive; use aoc_runner_derive::aoc_main;