Cargo fmt

This commit is contained in:
2024-06-27 21:54:14 +02:00
parent f18e7797ed
commit d47441975d
8 changed files with 163 additions and 93 deletions

View File

@@ -1,5 +1,5 @@
use std::collections::VecDeque;
use aoc_runner_derive::{aoc, aoc_generator}; use aoc_runner_derive::{aoc, aoc_generator};
use std::collections::VecDeque;
#[aoc_generator(day10)] #[aoc_generator(day10)]
fn parse(input: &str) -> Vec<char> { fn parse(input: &str) -> Vec<char> {
input.chars().collect() input.chars().collect()
@@ -36,7 +36,6 @@ fn part2(input: &Vec<char>) -> usize {
solve(input, 50).len() solve(input, 50).len()
} }
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::*; use super::*;
@@ -45,4 +44,4 @@ mod tests {
fn part1_example() { fn part1_example() {
assert_eq!(solve(&parse("111221"), 1), "312211"); assert_eq!(solve(&parse("111221"), 1), "312211");
} }
} }

View File

@@ -2,27 +2,30 @@ use aoc_runner_derive::aoc;
#[aoc(day11, part1)] #[aoc(day11, part1)]
fn part1(input: &str) -> String { fn part1(input: &str) -> String {
PasswordCounter::from(input.to_owned()).find(|p| { PasswordCounter::from(input.to_owned())
let r1 = rule1(&p); .find(|p| {
let r2 = rule2(&p); let r1 = rule1(&p);
let r3 = rule3(&p); let r2 = rule2(&p);
r1 && r2 && r3 let r3 = rule3(&p);
}).unwrap() r1 && r2 && r3
})
.unwrap()
} }
#[aoc(day11, part2)] #[aoc(day11, part2)]
fn part2(input: &str) -> String { fn part2(input: &str) -> String {
PasswordCounter::from(input.to_owned()).filter(|p| { PasswordCounter::from(input.to_owned())
let r1 = rule1(&p); .filter(|p| {
let r2 = rule2(&p); let r1 = rule1(&p);
let r3 = rule3(&p); let r2 = rule2(&p);
r1 && r2 && r3 let r3 = rule3(&p);
}) r1 && r2 && r3
})
.skip(1) .skip(1)
.next().unwrap() .next()
.unwrap()
} }
fn rule1(pw: &str) -> bool { fn rule1(pw: &str) -> bool {
let pw = pw.as_bytes(); let pw = pw.as_bytes();
for i in 0..pw.len() - 2 { for i in 0..pw.len() - 2 {
@@ -86,4 +89,4 @@ impl Iterator for PasswordCounter {
} }
#[cfg(test)] #[cfg(test)]
mod tests {} mod tests {}

View File

@@ -25,9 +25,9 @@ fn sum(json: &JsonValue) -> i64 {
JsonValue::Number(_) => json.as_i64().unwrap(), JsonValue::Number(_) => json.as_i64().unwrap(),
JsonValue::Boolean(_) => 0, JsonValue::Boolean(_) => 0,
JsonValue::Object(o) => { JsonValue::Object(o) => {
if o.iter().any(|(_, value)| { if o.iter()
value.is_string() && Some("red") == value.as_str() .any(|(_, value)| value.is_string() && Some("red") == value.as_str())
}) { {
0 0
} else { } else {
o.iter().map(|(_key, value)| sum(value)).sum() o.iter().map(|(_key, value)| sum(value)).sum()

View File

@@ -4,7 +4,8 @@ use aoc_runner_derive::{aoc, aoc_generator};
#[aoc_generator(day13)] #[aoc_generator(day13)]
fn parse(input: &str) -> HashMap<(String, String), i32> { fn parse(input: &str) -> HashMap<(String, String), i32> {
input.lines() input
.lines()
.map(|line| { .map(|line| {
let v: Vec<&str> = line[0..line.len() - 1].split(" ").collect(); let v: Vec<&str> = line[0..line.len() - 1].split(" ").collect();
let from = v[0]; let from = v[0];
@@ -14,21 +15,26 @@ fn parse(input: &str) -> HashMap<(String, String), i32> {
value *= -1; value *= -1;
} }
((from.into(), to.into()), value) ((from.into(), to.into()), value)
}).collect() })
.collect()
} }
#[aoc(day13, part1)] #[aoc(day13, part1)]
fn part1(deltas: &HashMap<(String, String), i32>) -> i32 { fn part1(deltas: &HashMap<(String, String), i32>) -> i32 {
let names: HashSet<_> = deltas.keys().map(|s| s.0.clone()).collect(); let names: HashSet<_> = deltas.keys().map(|s| s.0.clone()).collect();
permutations(names).iter().map(|arrangement| { permutations(names)
let mut happiness = 0; .iter()
for i in 0..arrangement.len() { .map(|arrangement| {
let right = (i + 1) % arrangement.len(); let mut happiness = 0;
happiness += deltas[&(arrangement[right].clone(), arrangement[i].clone())] + for i in 0..arrangement.len() {
deltas[&(arrangement[i].clone(), arrangement[right].clone())]; let right = (i + 1) % arrangement.len();
} happiness += deltas[&(arrangement[right].clone(), arrangement[i].clone())]
happiness + deltas[&(arrangement[i].clone(), arrangement[right].clone())];
}).max().unwrap() }
happiness
})
.max()
.unwrap()
} }
fn permutations(values: HashSet<String>) -> Vec<Vec<String>> { fn permutations(values: HashSet<String>) -> Vec<Vec<String>> {
@@ -42,30 +48,41 @@ fn p(mut input: Vec<String>) -> Vec<Vec<String>> {
} }
let mut results = Vec::new(); let mut results = Vec::new();
for _ in 0..input.len() { for _ in 0..input.len() {
p(input.iter().skip(1).map(|t| t.clone()).collect()).into_iter().for_each(|mut v| { p(input.iter().skip(1).map(|t| t.clone()).collect())
v.insert(0, input[0].clone()); .into_iter()
results.push(v); .for_each(|mut v| {
}); v.insert(0, input[0].clone());
results.push(v);
});
let v = input.remove(0); let v = input.remove(0);
input.push(v); input.push(v);
} }
results results
} }
#[aoc(day13, part2)] #[aoc(day13, part2)]
fn part2(deltas: &HashMap<(String, String), i32>) -> i32 { fn part2(deltas: &HashMap<(String, String), i32>) -> i32 {
let mut names: HashSet<_> = deltas.keys().map(|s| s.0.clone()).collect(); let mut names: HashSet<_> = deltas.keys().map(|s| s.0.clone()).collect();
names.insert("me".to_string()); names.insert("me".to_string());
permutations(names).iter().map(|arrangement| { permutations(names)
let mut happiness = 0; .iter()
for i in 0..arrangement.len() { .map(|arrangement| {
let right = (i + 1) % arrangement.len(); let mut happiness = 0;
happiness += deltas.get(&(arrangement[right].clone(), arrangement[i].clone())).or(Some(&0)).unwrap() + for i in 0..arrangement.len() {
deltas.get(&(arrangement[i].clone(), arrangement[right].clone())).or(Some(&0)).unwrap(); let right = (i + 1) % arrangement.len();
} happiness += deltas
happiness .get(&(arrangement[right].clone(), arrangement[i].clone()))
}).max().unwrap() .or(Some(&0))
.unwrap()
+ deltas
.get(&(arrangement[i].clone(), arrangement[right].clone()))
.or(Some(&0))
.unwrap();
}
happiness
})
.max()
.unwrap()
} }
#[cfg(test)] #[cfg(test)]

View File

@@ -2,7 +2,8 @@ use aoc_runner_derive::{aoc, aoc_generator};
#[aoc_generator(day14)] #[aoc_generator(day14)]
fn parse(input: &str) -> Vec<(usize, usize, usize)> { fn parse(input: &str) -> Vec<(usize, usize, usize)> {
input.lines() input
.lines()
.map(|line| { .map(|line| {
// Rudolph can fly 22 km/s for 8 seconds, but then must rest for 165 seconds. // Rudolph can fly 22 km/s for 8 seconds, but then must rest for 165 seconds.
let parts: Vec<&str> = line.split(" ").collect(); let parts: Vec<&str> = line.split(" ").collect();
@@ -17,7 +18,8 @@ fn parse(input: &str) -> Vec<(usize, usize, usize)> {
#[aoc(day14, part1)] #[aoc(day14, part1)]
fn part1(input: &Vec<(usize, usize, usize)>) -> usize { fn part1(input: &Vec<(usize, usize, usize)>) -> usize {
let seconds = 2503; let seconds = 2503;
input.into_iter() input
.into_iter()
.map(|&(speed, fly, rest)| { .map(|&(speed, fly, rest)| {
let cycle = fly + rest; let cycle = fly + rest;
let full = seconds / cycle; 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(); let max_distance = state.iter().map(|it| it.0).max().unwrap();
state.iter_mut().for_each(|it| if it.0 == max_distance { state.iter_mut().for_each(|it| {
it.1 += 1; if it.0 == max_distance {
it.1 += 1;
}
}); });
} }
state.into_iter().map(|it| it.1).max().unwrap() state.into_iter().map(|it| it.1).max().unwrap()
@@ -60,7 +64,15 @@ mod test {
#[test] #[test]
fn example2() { fn example2() {
assert_eq!(689, play(&parse("Comet can fly 14 km/s for 10 seconds, but then must rest for 127 seconds. assert_eq!(
Dancer can fly 16 km/s for 11 seconds, but then must rest for 162 seconds."), 1000)) 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
)
)
} }
} }

View File

@@ -4,10 +4,14 @@ use aoc_runner_derive::{aoc, aoc_generator};
#[aoc_generator(day15)] #[aoc_generator(day15)]
fn parse(input: &str) -> Vec<Ingredient> { fn parse(input: &str) -> Vec<Ingredient> {
input.lines() input
.lines()
.map(|line| { .map(|line| {
// Sprinkles: capacity 5, durability -1, flavor 0, texture 0, calories 5 // 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 capacity = parts[2].parse().unwrap();
let durability = parts[4].parse().unwrap(); let durability = parts[4].parse().unwrap();
let flavor = parts[6].parse().unwrap(); let flavor = parts[6].parse().unwrap();
@@ -34,10 +38,22 @@ fn part1(input: &[Ingredient]) -> i32 {
if a + b + c + d != 100 { if a + b + c + d != 100 {
continue; continue;
} }
let mut capacity = a * input[0].capacity + b * input[1].capacity + c * input[2].capacity + d * input[3].capacity; let mut capacity = a * input[0].capacity
let mut durability = a * input[0].durability + b * input[1].durability + c * input[2].durability + d * input[3].durability; + b * input[1].capacity
let mut flavor = a * input[0].flavor + b * input[1].flavor + c * input[2].flavor + d * input[3].flavor; + c * input[2].capacity
let mut texture = a * input[0].texture + b * input[1].texture + c * input[2].texture + d * input[3].texture; + 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 { if capacity < 0 {
capacity = 0; capacity = 0;
} }
@@ -68,13 +84,30 @@ fn part2(input: &[Ingredient]) -> i32 {
if a + b + c + d != 100 { if a + b + c + d != 100 {
continue; 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; continue;
} }
let mut capacity = a * input[0].capacity + b * input[1].capacity + c * input[2].capacity + d * input[3].capacity; let mut capacity = a * input[0].capacity
let mut durability = a * input[0].durability + b * input[1].durability + c * input[2].durability + d * input[3].durability; + b * input[1].capacity
let mut flavor = a * input[0].flavor + b * input[1].flavor + c * input[2].flavor + d * input[3].flavor; + c * input[2].capacity
let mut texture = a * input[0].texture + b * input[1].texture + c * input[2].texture + d * input[3].texture; + 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 { if capacity < 0 {
capacity = 0; capacity = 0;
} }

View File

@@ -4,14 +4,18 @@ use aoc_runner_derive::{aoc, aoc_generator};
#[aoc_generator(day16)] #[aoc_generator(day16)]
fn parse(input: &str) -> Vec<Sue> { fn parse(input: &str) -> Vec<Sue> {
input.lines() input
.lines()
.map(|line| { .map(|line| {
let (_, hints) = line.split_once(": ").unwrap(); let (_, hints) = line.split_once(": ").unwrap();
let hints: Vec<_> = hints.split(", ").collect(); let hints: Vec<_> = hints.split(", ").collect();
hints.into_iter().map(|thing| { hints
let (name, count) = thing.split_once(": ").unwrap(); .into_iter()
(name.to_string(), count.parse().unwrap()) .map(|thing| {
}).collect() let (name, count) = thing.split_once(": ").unwrap();
(name.to_string(), count.parse().unwrap())
})
.collect()
}) })
.collect() .collect()
} }
@@ -28,21 +32,24 @@ goldfish: 5
trees: 3 trees: 3
cars: 2 cars: 2
perfumes: 1"; perfumes: 1";
let known: Sue = known.split("\n") let known: Sue = known
.split("\n")
.map(|line| { .map(|line| {
let (name, count) = line.split_once(": ").unwrap(); let (name, count) = line.split_once(": ").unwrap();
(name.to_string(), count.parse().unwrap()) (name.to_string(), count.parse().unwrap())
}).collect(); })
.collect();
let sue = input.iter() let sue = input
.iter()
.enumerate() .enumerate()
.filter(|(_index, sue)| { .filter(|(_index, sue)| {
known.iter() known.iter().all(|(property, _)| {
.all(|(property, _)| { !sue.contains_key(property) || known.get(property) == sue.get(property)
!sue.contains_key(property) || known.get(property) == sue.get(property) })
})
}) })
.next().unwrap(); .next()
.unwrap();
sue.0 + 1 sue.0 + 1
} }
@@ -62,21 +69,19 @@ trees: 3";
goldfish: 5"; goldfish: 5";
let lt = foo(lt); let lt = foo(lt);
let sues: Vec<_> = input.iter() let sues: Vec<_> = input
.iter()
.enumerate() .enumerate()
.filter(|(_index, sue)| { .filter(|(_index, sue)| {
exact.iter() exact.iter().all(|(property, _)| {
.all(|(property, _)| { !sue.contains_key(property) || exact.get(property) == sue.get(property)
!sue.contains_key(property) || exact.get(property) == sue.get(property) }) && gt.iter().all(|(property, _)| {
}) !sue.contains_key(property)
&& gt.iter() || gt.get(property).unwrap() < sue.get(property).unwrap()
.all(|(property, _)| { }) && lt.iter().all(|(property, _)| {
!sue.contains_key(property) || gt.get(property).unwrap() < sue.get(property).unwrap() !sue.contains_key(property)
}) || lt.get(property).unwrap() > sue.get(property).unwrap()
&& lt.iter() })
.all(|(property, _)| {
!sue.contains_key(property) || lt.get(property).unwrap() > sue.get(property).unwrap()
})
}) })
.collect(); .collect();
dbg!(&sues); dbg!(&sues);
@@ -87,13 +92,14 @@ goldfish: 5";
// 213 too low // 213 too low
fn foo(exact: &str) -> Sue { fn foo(exact: &str) -> Sue {
let exact: Sue = exact.split("\n") let exact: Sue = exact
.split("\n")
.map(|line| { .map(|line| {
let (name, count) = line.split_once(": ").unwrap(); let (name, count) = line.split_once(": ").unwrap();
(name.to_string(), count.parse().unwrap()) (name.to_string(), count.parse().unwrap())
}).collect(); })
.collect();
exact exact
} }
type Sue = HashMap<String, u8>; type Sue = HashMap<String, u8>;

View File

@@ -1,6 +1,6 @@
extern crate advent_of_code_2015; extern crate advent_of_code_2015;
extern crate aoc_runner_derive;
extern crate aoc_runner; extern crate aoc_runner;
extern crate aoc_runner_derive;
use aoc_runner_derive::aoc_main; use aoc_runner_derive::aoc_main;