Compare commits

...

3 commits

Author SHA1 Message Date
BlackDemonFire 9d8c5708fc
Day 9 2023-12-12 08:25:33 +01:00
BlackDemonFire 38ccaeef6e
Day 8 2023-12-12 08:24:46 +01:00
BlackDemonFire 1d338eea9d
Day 7 2023-12-12 08:23:52 +01:00
20 changed files with 520 additions and 0 deletions

24
Cargo.lock generated
View file

@ -49,3 +49,27 @@ version = "0.1.0"
[[package]]
name = "d06t2"
version = "0.1.0"
[[package]]
name = "d07t1"
version = "0.1.0"
[[package]]
name = "d07t2"
version = "0.1.0"
[[package]]
name = "d08t1"
version = "0.1.0"
[[package]]
name = "d08t2"
version = "0.1.0"
[[package]]
name = "d09t1"
version = "0.1.0"
[[package]]
name = "d09t2"
version = "0.1.0"

View file

@ -14,4 +14,10 @@ members = [
"d05t2",
"d06t1",
"d06t2",
"d07t1",
"d07t2",
"d08t1",
"d08t2",
"d09t1",
"d09t2",
]

8
d07t1/Cargo.toml Normal file
View file

@ -0,0 +1,8 @@
[package]
name = "d07t1"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]

5
d07t1/demo_input.txt Normal file
View file

@ -0,0 +1,5 @@
32T3K 765
T55J5 684
KK677 28
KTJJT 220
QQQJA 483

85
d07t1/src/main.rs Normal file
View file

@ -0,0 +1,85 @@
use std::{cmp::Ordering, collections::HashMap, fs::read_to_string};
fn main() {
let input = read_to_string("input.txt").unwrap();
println!("d07t1: {}", d07t1(&input));
}
pub struct Card(String, usize);
pub fn d07t1(input: &str) -> usize {
let mut cards = input
.lines()
.map(|line| line.split_once(' '))
.map(Option::unwrap)
.map(|(cards, bet)| Card(cards.to_owned(), bet.parse::<usize>().unwrap()))
.collect::<Vec<_>>();
cards.sort_by(sort_card);
cards
.iter()
.enumerate()
.map(|(idx, card)| (idx + 1) * card.1)
.sum()
}
pub fn sort_card(a: &Card, b: &Card) -> Ordering {
let set_rating_a = set_rating(&a.0);
let set_rating_b = set_rating(&b.0);
if set_rating_a.cmp(&set_rating_b) != Ordering::Equal {
return set_rating_a.cmp(&set_rating_b);
}
for (a, b) in a.0.chars().zip(b.0.chars()) {
let rank = card_ranking(a).cmp(&card_ranking(b));
if rank != Ordering::Equal {
return rank;
}
}
Ordering::Equal
}
fn set_rating(card: &str) -> usize {
let mut map: HashMap<char, usize> = HashMap::new();
let chars = card.chars().collect::<Vec<_>>();
let mut unique_chars = chars.clone();
unique_chars.dedup();
for uc in &unique_chars {
map.insert(*uc, chars.iter().filter(|c| *c == uc).count());
}
match map.len() {
5 => 0,
4 => 1,
3 => *map.values().max().unwrap(),
2 => *map.values().max().unwrap() + 1,
1 => 6,
_ => unimplemented!(),
}
}
fn card_ranking(c: char) -> usize {
match c {
'2' => 1,
'3' => 2,
'4' => 3,
'5' => 4,
'6' => 5,
'7' => 6,
'8' => 7,
'9' => 8,
'T' => 9,
'J' => 10,
'Q' => 11,
'K' => 12,
'A' => 13,
_ => unimplemented!(),
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_d07t1() {
let input = include_str!("../demo_input.txt");
assert_eq!(d07t1(input), 6440);
}
}

8
d07t2/Cargo.toml Normal file
View file

@ -0,0 +1,8 @@
[package]
name = "d07t2"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]

5
d07t2/demo_input.txt Normal file
View file

@ -0,0 +1,5 @@
32T3K 765
T55J5 684
KK677 28
KTJJT 220
QQQJA 483

115
d07t2/src/main.rs Normal file
View file

@ -0,0 +1,115 @@
use std::{cmp::Ordering, collections::HashMap, fs::read_to_string};
fn main() {
let input = read_to_string("input.txt").unwrap();
println!("d07t2: {}", d07t2(&input));
}
#[derive(Debug)]
pub struct Cards {
cards: String,
bet: usize,
rating: usize,
}
pub fn d07t2(input: &str) -> usize {
let mut cards = input
.lines()
.map(|line| line.split_once(' '))
.map(Option::unwrap)
.map(|(cards, bet)| Cards {
cards: cards.to_owned(),
bet: bet.parse::<usize>().unwrap(),
rating: set_rating(cards),
})
.collect::<Vec<_>>();
cards.sort_by(sort_cards);
cards
.iter()
.enumerate()
.map(|(idx, card)| (idx + 1) * card.bet)
.sum()
}
pub fn sort_cards(a: &Cards, b: &Cards) -> Ordering {
if a.rating.cmp(&b.rating) != Ordering::Equal {
return a.rating.cmp(&b.rating);
}
for (a, b) in a.cards.chars().zip(b.cards.chars()) {
let rank = card_ranking(a).cmp(&card_ranking(b));
if rank != Ordering::Equal {
return rank;
}
}
Ordering::Equal
}
fn set_rating(card: &str) -> usize {
let mut optimal_card = String::from(card);
for i in 0..5 {
let c = optimal_card.chars().nth(i).unwrap();
let options = match c {
'J' => optimal_card.chars().collect(),
c => vec![c],
};
let top_c = options
.into_iter()
.map(|opt| {
let mut check_card = optimal_card.clone();
check_card.replace_range(i..=i, opt.to_string().as_str());
(opt, rank_set(&check_card))
})
.max_by_key(|(_, rank)| *rank)
.unwrap()
.0;
optimal_card.replace_range(i..=i, top_c.to_string().as_str());
}
rank_set(&optimal_card)
}
fn rank_set(card: &str) -> usize {
let mut chars: HashMap<char, usize> = HashMap::new();
for c in card.chars() {
chars.insert(c, *chars.get(&c).unwrap_or(&1));
}
let mut order: Vec<usize> = chars.clone().values().copied().collect();
order.sort_by(|a, b| b.cmp(a));
match chars.len() {
1 => 6,
2 => order[0] + 1,
3 => order[0],
4 => 1,
5 => 0,
a => unimplemented!("{a}"),
}
}
fn card_ranking(c: char) -> usize {
match c {
'J' => 1,
'2' => 2,
'3' => 3,
'4' => 4,
'5' => 5,
'6' => 6,
'7' => 7,
'8' => 8,
'9' => 9,
'T' => 10,
'Q' => 11,
'K' => 12,
'A' => 13,
a => unimplemented!("{a}"),
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_d07t2() {
let input = include_str!("../demo_input.txt");
assert_eq!(d07t2(input), 5905);
}
}

8
d08t1/Cargo.toml Normal file
View file

@ -0,0 +1,8 @@
[package]
name = "d08t1"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]

5
d08t1/demo_input.txt Normal file
View file

@ -0,0 +1,5 @@
LLR
AAA = (BBB, BBB)
BBB = (AAA, ZZZ)
ZZZ = (ZZZ, ZZZ)

40
d08t1/src/main.rs Normal file
View file

@ -0,0 +1,40 @@
use std::{collections::HashMap, fs::read_to_string};
fn main() {
let input = read_to_string("input.txt").unwrap();
println!("d08t1: {}", d08t1(&input));
}
#[must_use]
pub fn d08t1(input: &str) -> usize {
let mut lines = input.lines();
let mut directions = lines.next().unwrap().chars().cycle();
let mut locations: HashMap<String, (String, String)> = HashMap::new();
for line in lines.skip(1) {
let (source, destination) = line.split_once(" = ").unwrap();
let destination = &destination.replace(['(', ')'], "");
let (left, right) = destination.split_once(", ").unwrap();
locations.insert(source.to_string(), (left.to_string(), right.to_string()));
}
let mut result = 0;
let mut current_position = "AAA";
while current_position != "ZZZ" {
result += 1;
current_position = if directions.next().unwrap() == 'L' {
&locations.get(current_position).unwrap().0
} else {
&locations.get(current_position).unwrap().1
};
}
result
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_d08t1() {
let input = include_str!("../demo_input.txt");
assert_eq!(d08t1(input), 6);
}
}

8
d08t2/Cargo.toml Normal file
View file

@ -0,0 +1,8 @@
[package]
name = "d08t2"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]

10
d08t2/demo_input.txt Normal file
View file

@ -0,0 +1,10 @@
LR
11A = (11B, XXX)
11B = (XXX, 11Z)
11Z = (11B, XXX)
22A = (22B, XXX)
22B = (22C, 22C)
22C = (22Z, 22Z)
22Z = (22B, 22B)
XXX = (XXX, XXX)

65
d08t2/src/main.rs Normal file
View file

@ -0,0 +1,65 @@
use std::{collections::HashMap, fs::read_to_string};
fn main() {
let input = read_to_string("input.txt").unwrap();
println!("d08t2: {}", d08t2(&input));
}
pub fn d08t2(input: &str) -> usize {
let mut lines = input.lines();
let directions = lines.next().unwrap().chars().cycle();
let mut locations: HashMap<String, (String, String)> = HashMap::new();
for line in lines.skip(1) {
let (source, destination) = line.split_once(" = ").unwrap();
let destination = &destination.replace(['(', ')'], "");
let (left, right) = destination.split_once(", ").unwrap();
locations.insert(source.to_string(), (left.to_string(), right.to_string()));
}
let keys = locations.keys();
keys.filter(|k| k.ends_with('A'))
.map(Clone::clone)
.map(|start| {
let mut dirs = directions.clone();
let mut res = 0;
let mut current_position = start.clone();
while !current_position.ends_with('Z') {
res += 1;
current_position = if dirs.next().unwrap() == 'L' {
&locations.get(&current_position).unwrap().0
} else {
&locations.get(&current_position).unwrap().1
}
.clone();
}
res
})
.reduce(lcm)
.unwrap()
}
fn lcm(first: usize, second: usize) -> usize {
first * second / gcd(first, second)
}
fn gcd(first: usize, second: usize) -> usize {
let mut max = first.max(second);
let mut min = first.min(second);
loop {
let res = max % min;
if res == 0 {
return min;
}
max = min;
min = res;
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_d08t2() {
let input = include_str!("../demo_input.txt");
assert_eq!(d08t2(input), 6);
}
}

8
d09t1/Cargo.toml Normal file
View file

@ -0,0 +1,8 @@
[package]
name = "d09t1"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]

3
d09t1/demo_input.txt Normal file
View file

@ -0,0 +1,3 @@
0 3 6 9 12 15
1 3 6 10 15 21
10 13 16 21 30 45

50
d09t1/src/main.rs Normal file
View file

@ -0,0 +1,50 @@
use std::fs::read_to_string;
fn main() {
let input = read_to_string("input.txt").unwrap();
println!("d09t1: {}", d09t1(&input));
}
pub fn d09t1(input: &str) -> isize {
input
.lines()
.map(str::split_whitespace)
.map(|line| {
line.map(str::parse::<isize>)
.map(Result::unwrap)
.collect::<Vec<_>>()
})
.map(find_next_in_sequence)
.sum()
}
pub fn find_next_in_sequence(sequence: Vec<isize>) -> isize {
let deltas = get_deltas(&sequence);
if *deltas.last().unwrap() == 0 {
*sequence.last().unwrap()
} else {
*sequence.last().unwrap() + find_next_in_sequence(deltas)
}
}
fn get_deltas(sequence: &Vec<isize>) -> Vec<isize> {
sequence
.windows(2)
.map(|window| window[1] - window[0])
.collect::<Vec<_>>()
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_d09t1() {
let input = include_str!("../demo_input.txt");
assert_eq!(d09t1(input), 114);
}
#[test]
fn test_deltas() {
assert_eq!(get_deltas(&vec![0, 3, 6, 9, 12, 15]), vec![3, 3, 3, 3, 3])
}
}

8
d09t2/Cargo.toml Normal file
View file

@ -0,0 +1,8 @@
[package]
name = "d09t2"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]

3
d09t2/demo_input.txt Normal file
View file

@ -0,0 +1,3 @@
0 3 6 9 12 15
1 3 6 10 15 21
10 13 16 21 30 45

56
d09t2/src/main.rs Normal file
View file

@ -0,0 +1,56 @@
use std::fs::read_to_string;
fn main() {
let input = read_to_string("input.txt").unwrap();
println!("d09t2: {}", d09t2(&input));
}
pub fn d09t2(input: &str) -> isize {
input
.lines()
.map(str::split_whitespace)
.map(|line| {
line.map(str::parse::<isize>)
.map(Result::unwrap)
.rev()
.collect::<Vec<_>>()
})
.map(find_next_in_sequence)
.sum()
}
pub fn find_next_in_sequence(sequence: Vec<isize>) -> isize {
let deltas = get_deltas(&sequence);
if deltas.iter().all(|&e| e == 0) {
*sequence.last().unwrap()
} else {
*sequence.last().unwrap() + find_next_in_sequence(deltas)
}
}
fn get_deltas(sequence: &Vec<isize>) -> Vec<isize> {
sequence
.windows(2)
.map(|window| window[1] - window[0])
.collect::<Vec<_>>()
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_d09t2() {
let input = include_str!("../demo_input.txt");
assert_eq!(d09t2(input), 2);
}
#[test]
fn test_deltas() {
assert_eq!(get_deltas(&vec![0, 3, 6, 9, 12, 15]), vec![3, 3, 3, 3, 3])
}
#[test]
fn test_line_1() {
assert_eq!(d09t2("10 13 16 21 30 45"), 5)
}
}