1
0

Compare commits

..

6 Commits

Author SHA1 Message Date
cb41e4db56 disable day 14 part 2 because it requires manual intervention 2024-12-14 13:30:19 +01:00
f273c1719c clippy 2024-12-14 13:29:03 +01:00
267816b70e day 14 part 2 2024-12-14 10:30:17 +01:00
43003def6b day 14 part 1 2024-12-14 08:57:16 +01:00
0d910e8724 day 13 part 2 2024-12-13 15:32:45 +01:00
43bf93773f day 13 part 1 2024-12-13 13:26:52 +01:00
5 changed files with 348 additions and 17 deletions

View File

@@ -56,12 +56,12 @@ fn part2(input: &str) -> RiddleResult {
} }
let mut area = 0; let mut area = 0;
let mut further = vec![p]; let mut further = vec![p];
let mut convex_corners = 0; let mut group = HashSet::new();
let mut concave_corners = 0;
while let Some(current) = further.pop() { while let Some(current) = further.pop() {
if processed.contains(&current) { if processed.contains(&current) {
continue; continue;
} }
group.insert(current);
area += 1; area += 1;
for next in NEIGHBORS.iter().map(|d| (current.0 + d.0, current.1 + d.1)) { for next in NEIGHBORS.iter().map(|d| (current.0 + d.0, current.1 + d.1)) {
let next_c = garden.get(next); let next_c = garden.get(next);
@@ -71,28 +71,88 @@ fn part2(input: &str) -> RiddleResult {
} }
} }
processed.insert(current); processed.insert(current);
for (da, db) in NEIGHBORS.iter().circular_tuple_windows() {
let a = (current.0 + da.0, current.1 + da.1);
let b = (current.0 + db.0, current.1 + db.1);
if garden.get(a) != Some(c) && garden.get(b) != Some(c) {
convex_corners += 1;
} else if garden.get(a) == Some(c)
&& garden.get(b) == Some(c)
&& garden.get((current.0 + da.0 + db.0, current.1 + da.1 + db.1)) != Some(c)
{
concave_corners += 1;
}
}
} }
let var_name = convex_corners + concave_corners; result += area * sides(group);
let var_name = area * (var_name);
result += var_name;
} }
result result
} }
fn sides(group: HashSet<(i64, i64)>) -> RiddleResult {
let x_range = group.iter().min_by_key(|it| it.0).unwrap().0
..=group.iter().max_by_key(|it| it.0).unwrap().0;
let y_range = group.iter().min_by_key(|it| it.1).unwrap().1
..=group.iter().max_by_key(|it| it.1).unwrap().1;
let mut result = 0;
for x in x_range {
// 1 for the ending of the last group, +1 for each time a line "stops", e.g. between the current point and next there is a gap
let points = group
.iter()
.filter(|p| p.0 == x)
.filter(|p| !group.contains(&(p.0 - 1, p.1)))
.sorted_by_key(|p| p.1)
.collect_vec();
if !points.is_empty() {
let count = points
.into_iter()
.tuple_windows()
.filter(|(a, b)| a.1 + 1 != b.1)
.count();
result += 1 + count;
}
let points = group
.iter()
.filter(|p| p.0 == x)
.filter(|p| !group.contains(&(p.0 + 1, p.1)))
.sorted_by_key(|p| p.1)
.collect_vec();
if !points.is_empty() {
let count = points
.into_iter()
.tuple_windows()
.filter(|(a, b)| a.1 + 1 != b.1)
.count();
result += 1 + count;
}
}
for y in y_range {
let points = group
.iter()
.filter(|p| p.1 == y)
.filter(|p| !group.contains(&(p.0, p.1 - 1)))
.sorted_by_key(|p| p.0)
.collect_vec();
if !points.is_empty() {
let count = points
.into_iter()
.tuple_windows()
.filter(|(a, b)| a.0 + 1 != b.0)
.count();
result += 1 + count;
}
let points = group
.iter()
.filter(|p| p.1 == y)
.filter(|p| !group.contains(&(p.0, p.1 + 1)))
.sorted_by_key(|p| p.0)
.collect_vec();
if !points.is_empty() {
let count = points
.into_iter()
.tuple_windows()
.filter(|(a, b)| a.0 + 1 != b.0)
.count();
result += 1 + count;
}
}
result as RiddleResult
}
#[cfg(test)] #[cfg(test)]
mod test { mod test {
use std::collections::HashSet;
use crate::day12::sides;
use super::{part1, part2}; use super::{part1, part2};
const TEST_INPUT: &str = r"AAAA const TEST_INPUT: &str = r"AAAA
@@ -110,4 +170,16 @@ EEEC
fn test2() { fn test2() {
assert_eq!(part2(TEST_INPUT), 80); assert_eq!(part2(TEST_INPUT), 80);
} }
#[test]
fn sides_one() {
let group = HashSet::from_iter([(3, 5)]);
assert_eq!(sides(group), 4);
}
#[test]
fn sides_plus() {
let group = HashSet::from_iter([(3, 3), (3, 4), (3, 2), (2, 3), (4, 3)]);
assert_eq!(sides(group), 12);
}
} }

99
src/day13.rs Normal file
View File

@@ -0,0 +1,99 @@
use std::fs::read_to_string;
use itertools::Itertools;
use regex::Regex;
pub fn day_main() {
let input = read_to_string("input/day13.txt").unwrap();
let input = input.trim();
println!(" part1: {}", part1(input));
println!(" part2: {}", part2(input));
}
type RiddleResult = i64;
fn part1(input: &str) -> RiddleResult {
// Button A: X+30, Y+84
// Button B: X+74, Y+60
// Prize: X=2358, Y=2628
let r = Regex::new(
r"Button A: X\+(\d+), Y\+(\d+)
Button B: X\+(\d+), Y\+(\d+)
Prize: X=(\d+), Y=(\d+)",
)
.unwrap();
r.captures_iter(input)
.map(|block| {
let (ax, ay, bx, by, px, py) = block
.iter()
.skip(1)
.map(|it| it.unwrap().as_str().parse::<i64>().unwrap())
.collect_tuple()
.unwrap();
solve((ax, ay, bx, by, px, py)).unwrap_or(0)
})
.sum()
}
fn part2(input: &str) -> RiddleResult {
let r = Regex::new(
r"Button A: X\+(\d+), Y\+(\d+)
Button B: X\+(\d+), Y\+(\d+)
Prize: X=(\d+), Y=(\d+)",
)
.unwrap();
r.captures_iter(input)
.map(|block| {
let (ax, ay, bx, by, px, py) = block
.iter()
.skip(1)
.map(|it| it.unwrap().as_str().parse::<i64>().unwrap())
.collect_tuple()
.unwrap();
let solution = solve((ax, ay, bx, by, px + 10000000000000, py + 10000000000000));
solution.unwrap_or(0)
})
.sum()
}
fn solve((ax, ay, bx, by, px, py): (i64, i64, i64, i64, i64, i64)) -> Option<RiddleResult> {
let b = (ay * px - ax * py) / (ay * bx - ax * by);
let a = (px - b * bx) / ax;
if a >= 0 && b >= 0 && a * ax + b * bx == px && a * ay + b * by == py {
Some(3 * a + b)
} else {
None
}
}
#[cfg(test)]
mod test {
use super::{part1, part2};
const TEST_INPUT: &str = r"Button A: X+94, Y+34
Button B: X+22, Y+67
Prize: X=8400, Y=5400
Button A: X+26, Y+66
Button B: X+67, Y+21
Prize: X=12748, Y=12176
Button A: X+17, Y+86
Button B: X+84, Y+37
Prize: X=7870, Y=6450
Button A: X+69, Y+23
Button B: X+27, Y+71
Prize: X=18641, Y=10279
";
#[test]
fn test1() {
assert_eq!(part1(TEST_INPUT), 480);
}
#[test]
fn test2() {
assert_eq!(part2(TEST_INPUT), 875318608908);
}
}

156
src/day14.rs Normal file
View File

@@ -0,0 +1,156 @@
use std::{
collections::HashSet,
fs::read_to_string,
io::{self, BufRead},
ops::Sub,
};
use itertools::Itertools;
pub fn day_main() {
let input = read_to_string("input/day14.txt").unwrap();
let input = input.trim();
println!(" part1: {}", part1(input));
// println!(" part2: {}", part2(input));
}
type RiddleResult = usize;
fn part1(input: &str) -> RiddleResult {
solve_part1(input, 100, 101, 103)
}
fn solve_part1(input: &str, rounds: i64, width: i64, height: i64) -> RiddleResult {
let mut robots = parse(input);
for _ in 0..rounds {
robots.iter_mut().for_each(|((px, py), (vx, vy))| {
*px = (*px + width + *vx) % width;
*py = (*py + height + *vy) % height;
});
}
let upper_left = robots
.iter()
.filter(|&&((px, py), _)| px < width / 2 && py < height / 2)
.count();
let lower_left = robots
.iter()
.filter(|&&((px, py), _)| px < width / 2 && py > height / 2)
.count();
let upper_right = robots
.iter()
.filter(|&&((px, py), _)| px > width / 2 && py < height / 2)
.count();
let lower_right = robots
.iter()
.filter(|&&((px, py), _)| px > width / 2 && py > height / 2)
.count();
upper_left * lower_left * upper_right * lower_right
}
fn parse(input: &str) -> Vec<((i64, i64), (i64, i64))> {
input
.lines()
.map(|line| {
line.strip_prefix("p=")
.unwrap()
.split(" v=")
.map(|s| {
s.split(",")
.map(|d| d.parse::<i64>().unwrap())
.collect_tuple::<(i64, i64)>()
.unwrap()
})
.collect_tuple()
.unwrap()
})
.collect_vec()
}
#[allow(unused)]
fn part2(input: &str) -> RiddleResult {
let width = 101;
let height = 103;
let mut robots = parse(input);
let mut seen = HashSet::new();
for second in 0.. {
if seen.contains(&robots) {
panic!("Loop after {second} rounds, but no christmas tree!");
}
seen.insert(robots.clone());
robots.iter_mut().for_each(|((px, py), (vx, vy))| {
*px = (*px + width + *vx) % width;
*py = (*py + height + *vy) % height;
});
if robots
.iter()
.filter(|(s, _)| {
robots
.iter()
.any(|(t, _)| t != s && (t.0.sub(s.0).abs() + t.1.sub(s.1).abs()) <= 2)
})
.count()
> robots.len() * 70 / 100
{
printr(&robots, width, height);
println!("after {} seconds. Press enter to continue or type 'merry christmas' if you can spot a tree!", second + 1); //+1 because we look at it after they have changed
let stdin = io::stdin();
let line = stdin.lock().lines().next().unwrap().unwrap();
if line.as_str().eq_ignore_ascii_case("merry christmas") {
return second + 1;
}
}
}
unreachable!()
}
type Robot = ((i64, i64), (i64, i64));
fn printr(robots: &[Robot], width: i64, height: i64) {
for y in 0..height {
for x in 0..width {
let c = robots
.iter()
.filter(|&&((px, py), _)| px == x && py == y)
.count();
if c == 0 {
print!(".");
} else {
print!("{c}");
}
}
println!();
}
}
#[cfg(test)]
mod test {
use crate::day14::solve_part1;
use super::part2;
const TEST_INPUT: &str = r"p=0,4 v=3,-3
p=6,3 v=-1,-3
p=10,3 v=-1,2
p=2,0 v=2,-1
p=0,0 v=1,3
p=3,0 v=-2,-2
p=7,6 v=-1,-3
p=3,0 v=-1,-2
p=9,3 v=2,3
p=7,3 v=-1,2
p=2,4 v=2,-3
p=9,5 v=-3,-3
";
#[test]
fn test1() {
assert_eq!(solve_part1(TEST_INPUT, 100, 11, 7), 12);
}
#[test]
fn test2() {
assert_eq!(part2(TEST_INPUT), 0);
}
}

View File

@@ -10,5 +10,7 @@ pub mod day09;
pub mod day10; pub mod day10;
pub mod day11; pub mod day11;
pub mod day12; pub mod day12;
pub mod day13;
pub mod day14;
// PLACEHOLDER // PLACEHOLDER
pub mod utils; pub mod utils;

View File

@@ -17,6 +17,8 @@ fn main() {
(10, day10::day_main), (10, day10::day_main),
(11, day11::day_main), (11, day11::day_main),
(12, day12::day_main), (12, day12::day_main),
(13, day13::day_main),
(14, day14::day_main),
// PLACEHOLDER // PLACEHOLDER
]); ]);
let day: Option<u8> = args().nth(1).and_then(|a| a.parse().ok()); let day: Option<u8> = args().nth(1).and_then(|a| a.parse().ok());