1
0
Fork 0

Compare commits

...

4 Commits

Author SHA1 Message Date
Andrew Coleman f571a43259 day 22 part 1 solution 2024-02-21 13:00:36 -05:00
Andrew Coleman 5723296657 divide dependencies 2024-02-21 13:00:04 -05:00
Andrew Coleman 1da7a12927 cargo fmt 2024-02-21 12:59:51 -05:00
Andrew Coleman e9f6c6e50d add example 2024-02-21 12:59:24 -05:00
8 changed files with 209 additions and 18 deletions

View File

@ -25,9 +25,9 @@ aoc-parse = "0.2.18"
# Template dependencies
chrono = { version = "0.4.31", optional = true }
dhat = { version = "0.3.2", optional = true }
# Solution dependencies
num = "0.4.1"
pathfinding = "4.8.2"
pico-args = "0.5.0"
tinyjson = "2.5.1"
# Solution dependencies
tinyjson = "2.5.1"

View File

@ -0,0 +1,7 @@
1,0,1~1,2,1
0,0,2~2,0,2
0,2,3~2,2,3
0,0,4~0,2,4
2,0,5~2,2,5
0,1,6~2,1,6
1,1,8~1,1,9

View File

@ -107,7 +107,9 @@ mod tests {
#[test]
fn test_part_two() {
let result = part_two(&advent_of_code::template::read_file("examples", DAY, 2));
let result = part_two(&advent_of_code::template::read_file_part(
"examples", DAY, 2,
));
assert_eq!(result, Some(281));
}
}

View File

@ -39,12 +39,14 @@ fn parse(input: &str) -> Vec<Race> {
}
pub fn part_one(input: &str) -> Option<u64> {
Some(parse(input)
.iter()
.map(|race| race.winning_times().len() as u64)
.collect::<Vec<u64>>()
.iter()
.product())
Some(
parse(input)
.iter()
.map(|race| race.winning_times().len() as u64)
.collect::<Vec<u64>>()
.iter()
.product(),
)
}
pub fn part_two(input: &str) -> Option<u64> {

View File

@ -256,7 +256,10 @@ fn parse(input: &str) -> Vec<Hand> {
pub fn part_one(input: &str) -> Option<u32> {
let mut sum: u64 = 0;
let mut hands = parse(input).iter().map(|h| h.clone()).collect::<Vec<Hand>>();
let mut hands = parse(input)
.iter()
.map(|h| h.clone())
.collect::<Vec<Hand>>();
hands.sort();
for (index, hand) in hands.iter().rev().enumerate() {
sum += hand.bid * (index as u64 + 1);

View File

@ -38,7 +38,14 @@ fn next_number(s: &Seq) -> i32 {
}
pub fn part_one(input: &str) -> Option<u32> {
Some(parse(input).iter().map(|s| next_number(&s)).sum::<i32>().try_into().unwrap())
Some(
parse(input)
.iter()
.map(|s| next_number(&s))
.sum::<i32>()
.try_into()
.unwrap(),
)
}
fn previous_number(s: &Seq) -> i32 {
@ -51,7 +58,14 @@ fn previous_number(s: &Seq) -> i32 {
}
pub fn part_two(input: &str) -> Option<u32> {
Some(parse(input).iter().map(|s| previous_number(&s)).sum::<i32>().try_into().unwrap())
Some(
parse(input)
.iter()
.map(|s| previous_number(&s))
.sum::<i32>()
.try_into()
.unwrap(),
)
}
#[cfg(test)]

View File

@ -144,7 +144,6 @@ pub fn part_one(input: &str) -> Option<u64> {
Some(parts.iter().map(|p| filter_part(&rules, &p)).sum())
}
fn combinations(weights: [(u64, u64); 4]) -> u64 {
(0..4)
.into_iter()

View File

@ -1,10 +1,116 @@
advent_of_code::solution!(22);
pub fn part_one(input: &str) -> Option<u32> {
None
use aoc_parse::{parser, prelude::*};
use std::collections::{HashMap, HashSet};
#[derive(Clone, Copy, Debug, PartialOrd, Ord, Eq, PartialEq)]
struct Coord {
z: usize,
x: usize,
y: usize,
}
pub fn part_two(input: &str) -> Option<u32> {
#[derive(Debug, Clone, PartialOrd, Ord, Eq, PartialEq)]
struct Block {
id: usize,
one: Coord,
two: Coord,
}
impl Block {
fn intersect_xy(&self, other: &Self) -> bool {
self.one.x <= other.two.x
&& other.one.x <= self.two.x
&& self.one.y <= other.two.y
&& other.one.y <= self.two.y
}
fn below(&self, other: &Self) -> bool {
// self.id != other.id && self.intersect_xy(other) && self.two.z == other.one.z - 1
self.intersect_xy(other) && self.two.z == other.one.z - 1
}
fn above(&self, other: &Self) -> bool {
// self.id != other.id && self.intersect_xy(other) && self.one.z == other.two.z + 1
self.intersect_xy(other) && self.one.z == other.two.z + 1
}
}
fn parse(input: &str) -> Vec<Block> {
let p = parser!(lines(usize "," usize "," usize "~" usize "," usize "," usize));
let mut blocks = p
.parse(input)
.unwrap()
.iter()
.enumerate()
.map(|(index, r)| Block {
id: index + 1,
one: Coord {
z: r.2,
x: r.0,
y: r.1,
},
two: Coord {
z: r.5,
x: r.3,
y: r.4,
},
})
.collect::<Vec<_>>();
blocks.sort_by_key(|b| b.one);
blocks
}
fn drop_blocks(blocks: &[Block]) -> Vec<Block> {
let mut btm: Vec<Block> = Vec::with_capacity(blocks.len());
for b in blocks.iter() {
let mut block = b.clone();
for _i in 1..b.one.z {
if let Some(_below) = btm.iter().find(|d| d.below(&block)) {
break;
}
block.one.z -= 1;
block.two.z -= 1;
}
btm.push(block);
}
btm
}
fn pyprint(b: &Block) {
println!(
"(({}, {}, {}), ({}, {}, {}))",
b.one.x, b.one.y, b.one.z, b.two.x, b.two.y, b.two.z
);
}
pub fn part_one(input_str: &str) -> Option<usize> {
let input = parse(input_str);
let btm = drop_blocks(&input);
let mut b: HashMap<usize, Vec<usize>> = HashMap::new();
for outside in btm.iter() {
let mut below: Vec<usize> = vec![];
for inside in btm.iter() {
if inside.below(&outside) {
below.push(inside.id);
}
}
b.insert(outside.id, below);
}
let mut s: HashSet<usize> = HashSet::new();
for id in 1..=btm.len() {
if let Some(below) = b.get(&id) {
if below.len() == 1 {
for &x in below {
s.insert(x);
}
}
}
}
Some(btm.len() - s.len())
}
pub fn part_two(input_str: &str) -> Option<usize> {
None
}
@ -15,7 +121,7 @@ mod tests {
#[test]
fn test_part_one() {
let result = part_one(&advent_of_code::template::read_file("examples", DAY));
assert_eq!(result, None);
assert_eq!(result, Some(5));
}
#[test]
@ -23,4 +129,62 @@ mod tests {
let result = part_two(&advent_of_code::template::read_file("examples", DAY));
assert_eq!(result, None);
}
fn perform_test_intersect(
one: &(usize, usize, usize, usize, usize, usize),
two: &(usize, usize, usize, usize, usize, usize),
res: bool,
) {
let b1 = Block {
id: 1,
one: Coord {
x: one.0,
y: one.1,
z: one.2,
},
two: Coord {
x: one.3,
y: one.4,
z: one.5,
},
};
let b2 = Block {
id: 2,
one: Coord {
x: two.0,
y: two.1,
z: two.2,
},
two: Coord {
x: two.3,
y: two.4,
z: two.5,
},
};
assert_eq!(b1.intersect_xy(&b2), res);
}
#[test]
fn test_intersect_xy() {
let false_coords = [
[(0, 0, 1, 0, 0, 1), (1, 0, 1, 4, 0, 1)],
[(0, 0, 1, 0, 0, 1), (0, 1, 1, 0, 4, 1)],
[(0, 0, 1, 10, 0, 1), (1, 1, 1, 1, 1, 1)],
[(0, 0, 1, 10, 0, 1), (1, 1, 1, 1, 10, 1)],
];
for row in false_coords.iter() {
let one = row[0];
let two = row[1];
perform_test_intersect(&one, &two, false);
}
let true_coords = [
[(0, 0, 1, 0, 0, 1), (0, 0, 1, 4, 0, 1)],
[(0, 0, 1, 10, 0, 1), (5, 0, 1, 5, 5, 1)],
];
for row in true_coords.iter() {
let one = row[0];
let two = row[1];
perform_test_intersect(&one, &two, true);
}
}
}