Compare commits
3 Commits
cb41e4db56
...
dc1e4d074d
| Author | SHA1 | Date | |
|---|---|---|---|
| dc1e4d074d | |||
| 0fd7b96791 | |||
| 8c63bd2341 |
61
src/day09.rs
61
src/day09.rs
@@ -1,4 +1,4 @@
|
|||||||
use std::fs::read_to_string;
|
use std::{collections::BTreeMap, fs::read_to_string};
|
||||||
|
|
||||||
pub fn day_main() {
|
pub fn day_main() {
|
||||||
let input = read_to_string("input/day09.txt").unwrap();
|
let input = read_to_string("input/day09.txt").unwrap();
|
||||||
@@ -53,48 +53,41 @@ fn part1(input: &str) -> RiddleResult {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn part2(input: &str) -> RiddleResult {
|
fn part2(input: &str) -> RiddleResult {
|
||||||
let mut disk = Vec::with_capacity(input.len() * 10);
|
let mut free: BTreeMap<usize, usize> = BTreeMap::new();
|
||||||
// (start_index, len, file_id)
|
// (start_index, len, file_id)
|
||||||
let mut files: Vec<(usize, u32, usize)> = Vec::with_capacity(input.len() / 2 + 1);
|
let mut files: Vec<(usize, usize, usize)> = Vec::with_capacity(input.len() / 2 + 1);
|
||||||
|
let mut head = 0;
|
||||||
for (i, l) in input.chars().enumerate() {
|
for (i, l) in input.chars().enumerate() {
|
||||||
let l = l.to_digit(10).unwrap();
|
let l = l.to_digit(10).unwrap() as usize;
|
||||||
let content = if i % 2 == 0 {
|
|
||||||
// file
|
|
||||||
Some(i / 2) // id based on order of apperance and every second one is a file
|
|
||||||
} else {
|
|
||||||
None
|
|
||||||
};
|
|
||||||
if i % 2 == 0 {
|
if i % 2 == 0 {
|
||||||
files.push((disk.len(), l, i / 2));
|
files.push((head, l, i / 2));
|
||||||
}
|
} else {
|
||||||
for _ in 0..l {
|
free.insert(head, l);
|
||||||
disk.push(content);
|
|
||||||
}
|
}
|
||||||
|
head += l as usize;
|
||||||
}
|
}
|
||||||
while let Some((start_index, length, file_id)) = files.pop() {
|
for file in files.iter_mut().rev() {
|
||||||
let mut seeker = 0;
|
let (start_index, length, _file_id) = *file;
|
||||||
let mut found = None;
|
let found = free
|
||||||
while disk[seeker] != Some(file_id) {
|
.iter()
|
||||||
if disk[seeker..seeker + length as usize]
|
.take_while(|f| *f.0 < start_index)
|
||||||
.iter()
|
.find(|f| *f.1 >= length);
|
||||||
.all(|v| v.is_none())
|
|
||||||
{
|
if let Some((&free_start, &free_length)) = found.clone() {
|
||||||
found = Some(seeker);
|
free.remove(&free_start);
|
||||||
break;
|
free.insert(start_index, length);
|
||||||
}
|
file.0 = free_start;
|
||||||
seeker += 1;
|
if length < free_length {
|
||||||
}
|
free.insert(free_start + length, free_length - length);
|
||||||
if let Some(empty_start) = found {
|
|
||||||
for i in 0..length as usize {
|
|
||||||
disk[empty_start + i] = Some(file_id);
|
|
||||||
disk[start_index + i] = None;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
disk.into_iter()
|
files
|
||||||
.enumerate()
|
.iter()
|
||||||
.map(|(i, v)| if let Some(value) = v { i * value } else { 0 })
|
.map(|(start_index, l, file_id)| {
|
||||||
|
*file_id * (*start_index..start_index + l).sum::<RiddleResult>()
|
||||||
|
})
|
||||||
.sum()
|
.sum()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
29
src/day14.rs
29
src/day14.rs
@@ -7,11 +7,13 @@ use std::{
|
|||||||
|
|
||||||
use itertools::Itertools;
|
use itertools::Itertools;
|
||||||
|
|
||||||
|
use crate::utils::grid::Grid;
|
||||||
|
|
||||||
pub fn day_main() {
|
pub fn day_main() {
|
||||||
let input = read_to_string("input/day14.txt").unwrap();
|
let input = read_to_string("input/day14.txt").unwrap();
|
||||||
let input = input.trim();
|
let input = input.trim();
|
||||||
println!(" part1: {}", part1(input));
|
println!(" part1: {}", part1(input));
|
||||||
// println!(" part2: {}", part2(input));
|
println!(" part2: {}", part2(input));
|
||||||
}
|
}
|
||||||
|
|
||||||
type RiddleResult = usize;
|
type RiddleResult = usize;
|
||||||
@@ -77,22 +79,27 @@ fn part2(input: &str) -> RiddleResult {
|
|||||||
if seen.contains(&robots) {
|
if seen.contains(&robots) {
|
||||||
panic!("Loop after {second} rounds, but no christmas tree!");
|
panic!("Loop after {second} rounds, but no christmas tree!");
|
||||||
}
|
}
|
||||||
|
let mut grid: Grid<u8> = Grid::from_default(101, 103);
|
||||||
seen.insert(robots.clone());
|
seen.insert(robots.clone());
|
||||||
robots.iter_mut().for_each(|((px, py), (vx, vy))| {
|
robots.iter_mut().for_each(|((px, py), (vx, vy))| {
|
||||||
*px = (*px + width + *vx) % width;
|
*px = (*px + width + *vx) % width;
|
||||||
*py = (*py + height + *vy) % height;
|
*py = (*py + height + *vy) % height;
|
||||||
|
grid[(*px, *py)] += 1;
|
||||||
|
if let Some(v) = grid.get_mut((*px + 1, *py)) {
|
||||||
|
*v += 1
|
||||||
|
}
|
||||||
|
if let Some(v) = grid.get_mut((*px - 1, *py)) {
|
||||||
|
*v += 1
|
||||||
|
}
|
||||||
|
if let Some(v) = grid.get_mut((*px, *py + 1)) {
|
||||||
|
*v += 1
|
||||||
|
}
|
||||||
|
if let Some(v) = grid.get_mut((*px, *py - 1)) {
|
||||||
|
*v += 1
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
if robots
|
if robots.iter().filter(|(s, _)| grid[*s] > 1).count() > robots.len() * 70 / 100 {
|
||||||
.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);
|
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
|
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 stdin = io::stdin();
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ use std::{
|
|||||||
};
|
};
|
||||||
|
|
||||||
use itertools::Itertools;
|
use itertools::Itertools;
|
||||||
|
use regex::bytes::Replacer;
|
||||||
|
|
||||||
/// A grid structure, indexed by (x, y) tuples. The top-left coordinate is (0, 0).
|
/// A grid structure, indexed by (x, y) tuples. The top-left coordinate is (0, 0).
|
||||||
#[derive(Eq, PartialEq, Debug, Clone)]
|
#[derive(Eq, PartialEq, Debug, Clone)]
|
||||||
@@ -99,6 +100,21 @@ impl<T> Grid<T> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl<T> Grid<T>
|
||||||
|
where
|
||||||
|
T: Default,
|
||||||
|
{
|
||||||
|
pub fn from_default(width: i64, height: i64) -> Grid<T> {
|
||||||
|
let mut content: Vec<T> = Vec::with_capacity((width * height) as usize);
|
||||||
|
content.resize_with((width * height) as usize, Default::default);
|
||||||
|
Grid {
|
||||||
|
content_width: width,
|
||||||
|
content_height: height,
|
||||||
|
content,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl Grid<char> {
|
impl Grid<char> {
|
||||||
pub fn parse(input: &str) -> Grid<char> {
|
pub fn parse(input: &str) -> Grid<char> {
|
||||||
let content_width = input.lines().next().unwrap().len();
|
let content_width = input.lines().next().unwrap().len();
|
||||||
|
|||||||
Reference in New Issue
Block a user