Skip to content

Commit

Permalink
reorg cont'd
Browse files Browse the repository at this point in the history
  • Loading branch information
rene-d committed Jan 20, 2025
1 parent 9eddb53 commit ded0f26
Show file tree
Hide file tree
Showing 87 changed files with 1,346 additions and 1,197 deletions.
2 changes: 1 addition & 1 deletion 2015/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ Puzzle
[Day 15: Science for Hungry People](https://adventofcode.com/2015/day/15) | ⭐⭐ | [![Rust](../scripts/assets/rust.png)](../2015/day15/day15.rs)
[Day 16: Aunt Sue](https://adventofcode.com/2015/day/16) | ⭐⭐ | [![Rust](../scripts/assets/rust.png)](../2015/day16/day16.rs) [![Python](../scripts/assets/python.png)](../2015/day16/day16.py)
[Day 17: No Such Thing as Too Much](https://adventofcode.com/2015/day/17) | ⭐⭐ | [![Rust](../scripts/assets/rust.png)](../2015/day17/day17.rs)
[Day 18: Like a GIF For Your Yard](https://adventofcode.com/2015/day/18) | ⭐⭐ | [![Rust](../scripts/assets/rust.png)](../2015/day18/day18.rs)
[Day 18: Like a GIF For Your Yard](https://adventofcode.com/2015/day/18) | ⭐⭐ | [![Rust](../scripts/assets/rust.png)](../2015/day18/day18.rs) [🎁](../2015/day18/README.md)
[Day 19: Medicine for Rudolph](https://adventofcode.com/2015/day/19) | ⭐⭐ | [![Rust](../scripts/assets/rust.png)](../2015/day19/day19.rs)
[Day 20: Infinite Elves and Infinite Houses](https://adventofcode.com/2015/day/20) | ⭐⭐ | [![Rust](../scripts/assets/rust.png)](../2015/day20/day20.rs)
[Day 21: RPG Simulator 20XX](https://adventofcode.com/2015/day/21) | ⭐⭐ | [![Rust](../scripts/assets/rust.png)](../2015/day21/day21.rs) [![Python](../scripts/assets/python.png)](../2015/day21/day21.py)
Expand Down
36 changes: 26 additions & 10 deletions 2015/day11/day11.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
use std::fmt;

#[derive(Clone)]
struct Password {
pwd: Vec<char>,
loops: usize,
Expand Down Expand Up @@ -68,11 +69,12 @@ impl Password {
false
}

fn next_valid(&mut self) {
fn next_valid(&mut self) -> Self {
self.next();
while !self.is_valid() {
self.next();
}
self.clone()
}
}

Expand All @@ -88,18 +90,32 @@ impl fmt::Display for Password {
}
}

fn solve(data: &str) -> (Password, Password) {
let mut pwd: Password = Password::new(data.trim_ascii());

(pwd.next_valid(), pwd.next_valid())
}

/// main function
fn main() {
let args = aoc::parse_args();
let data = args.input.trim_ascii();

let mut pwd: Password = Password::new(data);
let mut args = aoc::parse_args();
args.run(solve);
}

// println!("init: {}", pwd);
#[cfg(test)]
mod test {
use super::*;

pwd.next_valid();
println!("{pwd}");
#[test]
fn test1() {
assert_eq!(
format!("{}", Password::new("abcdefgh").next_valid()),
"abcdffaa"
);

pwd.next_valid();
println!("{pwd}");
assert_eq!(
format!("{}", Password::new("ghijklmn").next_valid()),
"ghjaabcc"
);
}
}
80 changes: 52 additions & 28 deletions 2015/day12/day12.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,30 @@
//! [Day 12: JSAbacusFramework.io](https://adventofcode.com/2015/day/12)
use regex::Regex;
use serde_json::Value;

fn sum(v: &serde_json::Value) -> i32 {
/// main function
fn main() {
let mut args = aoc::parse_args();
args.run(|data| (part1(data), part2(data)));
}

fn part1(data: &str) -> i32 {
let re = Regex::new(r"(\-?\d+)").unwrap();
data.lines()
.map(|line| {
re.find_iter(line)
.map(|m| m.as_str().parse::<i32>().unwrap())
.sum::<i32>()
})
.sum::<i32>()
}

fn sum(v: &Value) -> i32 {
match v {
serde_json::Value::Number(n) => n.as_i64().unwrap().try_into().unwrap(),
serde_json::Value::Array(a) => a.iter().map(sum).sum(),
serde_json::Value::Object(o) => {
Value::Number(n) => n.as_i64().unwrap().try_into().unwrap(),
Value::Array(a) => a.iter().map(sum).sum(),
Value::Object(o) => {
// Ignore any object (and all of its children) which has any property with the value "red".
for v in o.values() {
if v.as_str() == Some("red") {
Expand All @@ -19,29 +37,35 @@ fn sum(v: &serde_json::Value) -> i32 {
}
}

/// main function
fn main() {
let args = aoc::parse_args();
let data = args
.input
.lines()
.map(std::string::ToString::to_string)
.collect::<Vec<String>>();

// part 1
let re = Regex::new(r"(\-?\d+)").unwrap();
let part1 = &data
.iter()
.map(|line| {
re.find_iter(line)
.map(|m| m.as_str().parse::<i32>().unwrap())
.sum::<i32>()
})
.sum::<i32>();
println!("{part1}");
fn part2(data: &str) -> i32 {
let json: Value = serde_json::from_str(data).expect("JSON was not well-formatted");
sum(&json)
}

// part 2
let json: serde_json::Value =
serde_json::from_str(&data[0]).expect("JSON was not well-formatted");
println!("{}", sum(&json));
#[cfg(test)]
mod test {
use super::*;

#[test]
fn test1() {
assert_eq!(part1("[1,2,3]"), 6);
assert_eq!(part1(r#"{"a":2,"b":4}"#), 6);

assert_eq!(part1("[[[3]]]"), 3);
assert_eq!(part1(r#"{"a":{"b":4},"c":-1}"#), 3);

assert_eq!(part1(r#"{"a":[-1,1]}"#), 0);
assert_eq!(part1(r#"[-1,{"a":1}]"#), 0);

assert_eq!(part1("[]"), 0);
assert_eq!(part1("{}"), 0);
}

#[test]
fn test2() {
assert_eq!(part2("[1,2,3]"), 6);
assert_eq!(part2(r#"[1,{"c":"red","b":2},3]"#), 4);
assert_eq!(part2(r#"{"d":"red","e":[1,2,3,4],"f":5}"#), 0);
assert_eq!(part2(r#"[1,"red",5]"#), 6);
}
}
2 changes: 1 addition & 1 deletion 2015/day13/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ edition = "2021"
[dependencies]
aoc = { path = "../../aoc" }
regex = "1"
permutator = "0.4"
rustc-hash = "2.1.0"
itertools = "0.14.0"

[[bin]]
name = "day13"
Expand Down
93 changes: 52 additions & 41 deletions 2015/day13/day13.rs
Original file line number Diff line number Diff line change
@@ -1,71 +1,82 @@
//! [Day 13: Knights of the Dinner Table](https://adventofcode.com/2015/day/13)
use permutator::HeapPermutationIterator;
use itertools::Itertools;
use regex::Regex;
use rustc_hash::{FxHashMap, FxHashSet};

fn calc(names: &FxHashSet<String>, happiness: &FxHashMap<(String, String), i32>) -> i32 {
let perm_names = &mut names.iter().collect::<Vec<&String>>();
let permutator = HeapPermutationIterator::new(perm_names);
fn calc<'a>(names: &FxHashSet<&'a str>, happiness: &FxHashMap<(&'a str, &'a str), i32>) -> i32 {
let length = names.len();

let mut happiness_max = i32::MIN;
names
.iter()
.permutations(names.len())
.map(|permutated| {
(0..length)
.map(|i| {
let n1 = permutated[i];
let n2 = permutated[(i + 1) % length];

for permutated in permutator {
let mut happiness_sum = 0;
for i in 0..permutated.len() {
let n1 = permutated[i];
let n2 = permutated[(i + 1) % permutated.len()];

happiness_sum += happiness.get(&(n1.to_string(), n2.to_string())).unwrap();
happiness_sum += happiness.get(&(n2.to_string(), n1.to_string())).unwrap();
}

if happiness_max < happiness_sum {
happiness_max = happiness_sum;
}
}
happiness_max
happiness.get(&(n1, n2)).unwrap() + happiness.get(&(n2, n1)).unwrap()
})
.sum()
})
.max()
.unwrap()
}

/// main function
fn main() {
let args = aoc::parse_args();
let data = args
.input
.lines()
.map(std::string::ToString::to_string)
.collect::<Vec<String>>();

let mut names: FxHashSet<String> = FxHashSet::default();
let mut happiness: FxHashMap<(String, String), i32> = FxHashMap::default();
fn solve<'a>(data: &'a str) -> (i32, i32) {
let mut names: FxHashSet<&'a str> = FxHashSet::default();
let mut happiness: FxHashMap<(&'a str, &'a str), i32> = FxHashMap::default();

let re =
Regex::new(r"^(.+) would (gain|lose) (\d+) happiness units by sitting next to (.+)\.$")
.unwrap();

for line in &data {
for line in data.lines() {
if let Some(op) = re.captures(line) {
names.insert(op[1].to_string());
names.insert(op[4].to_string());
let name = op.get(1).unwrap().as_str();
let neighbor = op.get(4).unwrap().as_str();

names.insert(name);
names.insert(neighbor);

let mut gain: i32 = op[3].parse().unwrap();
if op[2].to_string() == "lose" {
if &op[2] == "lose" {
gain = -gain;
}

happiness.insert((op[1].to_string(), op[4].to_string()), gain);
happiness.insert((name, neighbor), gain);
}
}

// part 1
println!("{}", calc(&names, &happiness));
let part1 = calc(&names, &happiness);

// part 2
for name in &names {
happiness.insert((name.to_string(), "me".to_string()), 0);
happiness.insert(("me".to_string(), name.to_string()), 0);
happiness.insert((name, "me"), 0);
happiness.insert(("me", name), 0);
}
names.insert("me".to_string());
names.insert("me");
let part2 = calc(&names, &happiness);

println!("{}", calc(&names, &happiness));
(part1, part2)
}

/// main function
fn main() {
let mut args = aoc::parse_args();
args.run(solve);
}

#[cfg(test)]
mod test {
use super::*;

const TEST_INPUT: &str = include_str!("test.txt");

#[test]
fn test1() {
assert_eq!(solve(TEST_INPUT).0, 330);
}
}
12 changes: 12 additions & 0 deletions 2015/day13/test.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
Alice would gain 54 happiness units by sitting next to Bob.
Alice would lose 79 happiness units by sitting next to Carol.
Alice would lose 2 happiness units by sitting next to David.
Bob would gain 83 happiness units by sitting next to Alice.
Bob would lose 7 happiness units by sitting next to Carol.
Bob would lose 63 happiness units by sitting next to David.
Carol would lose 62 happiness units by sitting next to Alice.
Carol would gain 60 happiness units by sitting next to Bob.
Carol would gain 55 happiness units by sitting next to David.
David would gain 46 happiness units by sitting next to Alice.
David would lose 7 happiness units by sitting next to Bob.
David would gain 41 happiness units by sitting next to Carol.
41 changes: 25 additions & 16 deletions 2015/day14/day14.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
use regex::Regex;

const DURATION: u32 = 2503;

#[derive(Debug)]
struct Reinder {
// name: String,
Expand All @@ -12,22 +10,15 @@ struct Reinder {
rest: u32,
}

/// main function
fn main() {
let args = aoc::parse_args();
let data = args
.input
.lines()
.map(std::string::ToString::to_string)
.collect::<Vec<String>>();

fn solve(data: &str, max_duration: u32) -> (u32, u32) {
let mut reinders = Vec::new();

let re = Regex::new(
r"(\w+) can fly (\d+) km/s for (\d+) seconds, but then must rest for (\d+) seconds.",
)
.unwrap();
for line in &data {

for line in data.lines() {
re.captures(line).map(|cap| {
let reinder = Reinder {
// name: cap[1].to_string(),
Expand All @@ -45,7 +36,7 @@ fn main() {
let max_distance = reinders
.iter()
.map(|reinder: &Reinder| -> u32 {
let mut seconds = DURATION;
let mut seconds = max_duration;
let mut distance = 0;
while seconds >= reinder.duration + reinder.rest {
seconds -= reinder.duration + reinder.rest;
Expand All @@ -57,13 +48,12 @@ fn main() {
})
.max()
.unwrap();
println!("{max_distance}");

// part 2
let mut scores: Vec<u32> = vec![0; reinders.len()];
let mut distances: Vec<u32> = vec![0; reinders.len()];

for elapsed in 1..DURATION {
for elapsed in 1..max_duration {
for i in 0..reinders.len() {
let reinder = &reinders[i];

Expand All @@ -86,5 +76,24 @@ fn main() {
}
}
}
println!("{:?}", scores.iter().max().unwrap());

(max_distance, *scores.iter().max().unwrap())
}

/// Main function.
fn main() {
let mut args = aoc::parse_args();
args.run(|data| solve(data, 2503));
}

#[cfg(test)]
mod test {
use super::*;

const TEST_INPUT: &str = include_str!("test.txt");

#[test]
fn test_solve() {
assert_eq!(solve(TEST_INPUT, 1000), (1120, 689));
}
}
File renamed without changes.
Loading

0 comments on commit ded0f26

Please sign in to comment.