Skip to content

Commit

Permalink
Optimization
Browse files Browse the repository at this point in the history
  • Loading branch information
komu committed Nov 17, 2023
1 parent 2926dc0 commit ea7cf29
Show file tree
Hide file tree
Showing 7 changed files with 206 additions and 66 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ All rows with solutions over a millisecond are marked with 😔.
| [21](https://adventofcode.com/2022/day/21) | [21.rs](src/bin/21.rs) | 325.30µs | 235.40µs | - |
| [22](https://adventofcode.com/2022/day/22) | [22.rs](src/bin/22.rs) | 139.31µs | 127.73µs | - |
| [23](https://adventofcode.com/2022/day/23) | [23.rs](src/bin/23.rs) | 15.51ms | 831.17ms | 😔 |
| [24](https://adventofcode.com/2022/day/24) | [24.rs](src/bin/24.rs) | 4.21s | 39.31s | 😔 |
| [24](https://adventofcode.com/2022/day/24) | [24.rs](src/bin/24.rs) | 80.10ms | 1.16s | 😔 |
| [25](https://adventofcode.com/2022/day/25) | [25.rs](src/bin/25.rs) | 17.04µs | - | - |

In the end, days 15, 19, 23 and 24 blew the 100 ms budget by themselves, but ignoring those the total time for the rest
Expand Down
24 changes: 16 additions & 8 deletions src/bin/19.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,23 @@ use regex::Regex;
pub fn part_one(input: &str) -> Option<u16> {
let blueprints = parse_lines::<Blueprint>(input).collect::<Vec<_>>();

Some(blueprints.par_iter().map(|b| b.id as u16 * b.max_geodes(24) as u16).sum())
Some(
blueprints
.par_iter()
.map(|b| b.id as u16 * b.max_geodes(24) as u16)
.sum(),
)
}

pub fn part_two(input: &str) -> Option<u16> {
let blueprints = parse_lines::<Blueprint>(input).take(3).collect::<Vec<_>>();

Some(blueprints.par_iter().map(|b| b.max_geodes(32) as u16).product())
Some(
blueprints
.par_iter()
.map(|b| b.max_geodes(32) as u16)
.product(),
)
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
Expand Down Expand Up @@ -80,10 +90,11 @@ impl SearchState {
5..=5 => 2,
6..=9 => 3,
10..=13 => 4,
_ => 5
_ => 5,
};

self.geodes + self.remaining_minutes as GeodeCount * (self.geode_robots as GeodeCount + extra)
self.geodes
+ self.remaining_minutes as GeodeCount * (self.geode_robots as GeodeCount + extra)
}

fn collect_materials(&mut self) {
Expand Down Expand Up @@ -145,15 +156,14 @@ impl Blueprint {
cache: &mut HashMap<SearchState, GeodeCount>,
best: &mut GeodeCount,
) -> GeodeCount {

let cacheable = state.remaining_minutes < 25;
if state.remaining_minutes == 0 {
if state.geodes > *best {
*best = state.geodes;
}
return state.geodes;
} else if state.geode_estimate() < *best {
return 0
return 0;
} else if cacheable {
if let Some(result) = cache.get(&state) {
return *result;
Expand All @@ -174,14 +184,12 @@ impl Blueprint {
if can_build_geode_robot {
state.build_robot(Material::Geode, self);
result = result.max(self.recurse(state, cache, best));

} else if can_build_obsidian_robot {
let without_robot = state.clone();

state.build_robot(Material::Obsidian, self);
result = result.max(self.recurse(state, cache, best));
result = result.max(self.recurse(without_robot, cache, best));

} else {
if can_build_clay_robot {
let mut new_state = state.clone();
Expand Down
99 changes: 81 additions & 18 deletions src/bin/23.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use aoc::point::{CardinalDirection, CompassDirection};
use enum_iterator::all;
use hashbrown::{HashMap, HashSet};
use itertools::Itertools;
use aoc::point::{CardinalDirection, CompassDirection};

pub fn part_one(input: &str) -> Option<u32> {
Some(run(input, 10, false))
Expand All @@ -15,10 +15,30 @@ pub fn run(input: &str, rounds: u32, round_count: bool) -> u32 {
let mut elves = ElfMap::parse(input);

let directions_vecs = vec![
vec![CardinalDirection::N, CardinalDirection::S, CardinalDirection::W, CardinalDirection::E],
vec![CardinalDirection::S, CardinalDirection::W, CardinalDirection::E, CardinalDirection::N],
vec![CardinalDirection::W, CardinalDirection::E, CardinalDirection::N, CardinalDirection::S],
vec![CardinalDirection::E, CardinalDirection::N, CardinalDirection::S, CardinalDirection::W],
vec![
CardinalDirection::N,
CardinalDirection::S,
CardinalDirection::W,
CardinalDirection::E,
],
vec![
CardinalDirection::S,
CardinalDirection::W,
CardinalDirection::E,
CardinalDirection::N,
],
vec![
CardinalDirection::W,
CardinalDirection::E,
CardinalDirection::N,
CardinalDirection::S,
],
vec![
CardinalDirection::E,
CardinalDirection::N,
CardinalDirection::S,
CardinalDirection::W,
],
];

for r in 0..rounds {
Expand Down Expand Up @@ -61,8 +81,20 @@ pub fn run(input: &str, rounds: u32, round_count: bool) -> u32 {
panic!("no result");
}

let (x_min, x_max) = elves.elves.iter().map(|e| e.x).minmax().into_option().unwrap();
let (y_min, y_max) = elves.elves.iter().map(|e| e.y).minmax().into_option().unwrap();
let (x_min, x_max) = elves
.elves
.iter()
.map(|e| e.x)
.minmax()
.into_option()
.unwrap();
let (y_min, y_max) = elves
.elves
.iter()
.map(|e| e.y)
.minmax()
.into_option()
.unwrap();

let w = (x_max - x_min + 1) as u32;
let h = (y_max - y_min + 1) as u32;
Expand Down Expand Up @@ -105,10 +137,26 @@ impl ElfMap {

fn is_free(&self, p: &Point, cd: CardinalDirection) -> bool {
let adjacent = match cd {
CardinalDirection::N => vec![CompassDirection::N, CompassDirection::NE, CompassDirection::NW],
CardinalDirection::S => vec![CompassDirection::S, CompassDirection::SE, CompassDirection::SW],
CardinalDirection::W => vec![CompassDirection::W, CompassDirection::NW, CompassDirection::SW],
CardinalDirection::E => vec![CompassDirection::E, CompassDirection::NE, CompassDirection::SE],
CardinalDirection::N => vec![
CompassDirection::N,
CompassDirection::NE,
CompassDirection::NW,
],
CardinalDirection::S => vec![
CompassDirection::S,
CompassDirection::SE,
CompassDirection::SW,
],
CardinalDirection::W => vec![
CompassDirection::W,
CompassDirection::NW,
CompassDirection::SW,
],
CardinalDirection::E => vec![
CompassDirection::E,
CompassDirection::NE,
CompassDirection::SE,
],
};

for d in adjacent {
Expand All @@ -123,8 +171,20 @@ impl ElfMap {

#[allow(dead_code)]
fn dump(&self) {
let (x_min, x_max) = self.elves.iter().map(|e| e.x).minmax().into_option().unwrap();
let (y_min, y_max) = self.elves.iter().map(|e| e.y).minmax().into_option().unwrap();
let (x_min, x_max) = self
.elves
.iter()
.map(|e| e.x)
.minmax()
.into_option()
.unwrap();
let (y_min, y_max) = self
.elves
.iter()
.map(|e| e.y)
.minmax()
.into_option()
.unwrap();

for y in y_min..=y_max {
for x in x_min..=x_max {
Expand All @@ -143,7 +203,9 @@ impl ElfMap {

impl ElfMap {
fn new() -> Self {
ElfMap { elves: HashSet::new() }
ElfMap {
elves: HashSet::new(),
}
}

fn parse(s: &str) -> Self {
Expand All @@ -152,14 +214,15 @@ impl ElfMap {
for (y, line) in s.lines().enumerate() {
for (x, c) in line.chars().enumerate() {
if c == '#' {
elves.insert(Point { x: x as i32, y: y as i32 });
elves.insert(Point {
x: x as i32,
y: y as i32,
});
}
}
}

ElfMap {
elves
}
ElfMap { elves }
}
}

Expand Down
Loading

0 comments on commit ea7cf29

Please sign in to comment.