mirror of
https://github.com/bertptrs/adventofcode.git
synced 2025-12-27 05:40:32 +01:00
Compare commits
4 Commits
8c2c3be40c
...
ce008e47da
| Author | SHA1 | Date | |
|---|---|---|---|
| ce008e47da | |||
| 542a8143e2 | |||
| 10174a2915 | |||
| 3522b38394 |
@@ -110,10 +110,15 @@ impl BluePrint {
|
||||
if u32::from(best - got) >= ideal_from_now {
|
||||
continue;
|
||||
}
|
||||
assert!(todo.len() <= 1_000_000, "Safety: got a todo list of len {}, best: {best}",
|
||||
todo.len());
|
||||
assert!(
|
||||
todo.len() <= 1_000_000,
|
||||
"Safety: got a todo list of len {}, best: {best}",
|
||||
todo.len()
|
||||
);
|
||||
for (element, &costs) in self.costs.iter().enumerate() {
|
||||
let Some(min_to_build) = self.until_buildable(costs, resources, machines) else { break };
|
||||
let Some(min_to_build) = self.until_buildable(costs, resources, machines) else {
|
||||
break;
|
||||
};
|
||||
|
||||
// +1 because we need a turn to build
|
||||
let built_after = min_to_build + 1;
|
||||
|
||||
@@ -136,7 +136,9 @@ pub fn part1(input: &[u8]) -> Result<String> {
|
||||
pub fn part2(input: &[u8]) -> Result<String> {
|
||||
let monkeys = parse_input(input, parse_monkeys)?;
|
||||
|
||||
let Monkey::Operation(first, second, _) = &monkeys[&b"root"[..]] else { anyhow::bail!("root is a literal somehow")};
|
||||
let Monkey::Operation(first, second, _) = &monkeys[&b"root"[..]] else {
|
||||
anyhow::bail!("root is a literal somehow")
|
||||
};
|
||||
|
||||
let result = match (evaluate2(&monkeys, first), evaluate2(&monkeys, second)) {
|
||||
(Ok(_), Ok(_)) => anyhow::bail!("both arms succeeded"),
|
||||
|
||||
@@ -128,14 +128,18 @@ fn parse_map(input: &[u8]) -> IResult<&[u8], (Map, Vec<Step>)> {
|
||||
)(input)
|
||||
}
|
||||
|
||||
fn find_starting_x(top_row: &[u8]) -> Result<usize> {
|
||||
top_row
|
||||
.iter()
|
||||
.position(|&b| b == b'.')
|
||||
.context("Could not find starting position")
|
||||
}
|
||||
|
||||
pub fn part1(input: &[u8]) -> Result<String> {
|
||||
let (map, steps) = parse_input(input, parse_map)?;
|
||||
let mut dir = Direction::Right;
|
||||
let mut y = 0;
|
||||
let mut x = map[y]
|
||||
.iter()
|
||||
.position(|&b| b == b'.')
|
||||
.context("Could not find starting position")?;
|
||||
let mut x = find_starting_x(map[y])?;
|
||||
|
||||
for step in steps {
|
||||
match step {
|
||||
@@ -268,13 +272,125 @@ fn break_squares<'a>(map: &[&'a [u8]], side_length: usize) -> [(Map<'a>, usize,
|
||||
}
|
||||
|
||||
pub fn part2(input: &[u8]) -> Result<String> {
|
||||
let (map, _steps) = parse_input(input, parse_map)?;
|
||||
let (map, steps) = parse_input(input, parse_map)?;
|
||||
|
||||
let side_length = side_length_of(&map);
|
||||
|
||||
let _squares = break_squares(&map, side_length);
|
||||
let squares = break_squares(&map, side_length);
|
||||
|
||||
anyhow::bail!("not implemented")
|
||||
let convert_coords = |square: usize, x: usize, y: usize| {
|
||||
let offset_x = squares[square].1 * side_length + x + 1;
|
||||
let offset_y = squares[square].2 * side_length + y + 1;
|
||||
|
||||
(offset_x, offset_y)
|
||||
};
|
||||
|
||||
let mut current_square = 0;
|
||||
let mut y = 0;
|
||||
let mut x = find_starting_x(squares[current_square].0[y])?;
|
||||
let mut dir = Direction::Right;
|
||||
|
||||
for step in steps {
|
||||
match step {
|
||||
Step::Left => dir = dir.turn_left(),
|
||||
Step::Right => dir = dir.turn_right(),
|
||||
Step::Forward(mut amount) => {
|
||||
'outer: while amount > 0 {
|
||||
let map = &squares[current_square].0;
|
||||
let coord = match dir {
|
||||
Direction::Up => {
|
||||
while amount > 0 {
|
||||
if y == 0 {
|
||||
break;
|
||||
} else if map[y - 1][x] == b'#' {
|
||||
break 'outer;
|
||||
} else {
|
||||
y -= 1;
|
||||
amount -= 1;
|
||||
}
|
||||
}
|
||||
|
||||
x
|
||||
}
|
||||
Direction::Down => {
|
||||
while amount > 0 {
|
||||
if y + 1 >= side_length {
|
||||
break;
|
||||
} else if map[y + 1][x] == b'#' {
|
||||
break 'outer;
|
||||
} else {
|
||||
amount -= 1;
|
||||
y += 1;
|
||||
}
|
||||
}
|
||||
|
||||
x
|
||||
}
|
||||
Direction::Left => {
|
||||
while amount > 0 {
|
||||
if x == 0 {
|
||||
break;
|
||||
} else if map[y][x - 1] == b'#' {
|
||||
break 'outer;
|
||||
} else {
|
||||
x -= 1;
|
||||
amount -= 1;
|
||||
}
|
||||
}
|
||||
|
||||
y
|
||||
}
|
||||
Direction::Right => {
|
||||
while amount > 0 {
|
||||
if x + 1 >= side_length {
|
||||
break;
|
||||
} else if map[y][x + 1] == b'#' {
|
||||
break 'outer;
|
||||
} else {
|
||||
amount -= 1;
|
||||
x += 1;
|
||||
}
|
||||
}
|
||||
|
||||
y
|
||||
}
|
||||
};
|
||||
|
||||
if amount > 0 {
|
||||
let (new_dir, new_square, invert) =
|
||||
TRANSITIONS[current_square][dir as usize];
|
||||
|
||||
let flipped_coord = if invert {
|
||||
side_length - 1 - coord
|
||||
} else {
|
||||
coord
|
||||
};
|
||||
|
||||
let (new_x, new_y) = match new_dir {
|
||||
Direction::Up => (flipped_coord, side_length - 1),
|
||||
Direction::Down => (flipped_coord, 0),
|
||||
Direction::Left => (side_length - 1, flipped_coord),
|
||||
Direction::Right => (0, flipped_coord),
|
||||
};
|
||||
|
||||
if squares[new_square].0[new_y][new_x] == b'#' {
|
||||
break 'outer;
|
||||
}
|
||||
|
||||
x = new_x;
|
||||
y = new_y;
|
||||
current_square = new_square;
|
||||
dir = new_dir;
|
||||
amount -= 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let (real_x, real_y) = convert_coords(current_square, x, y);
|
||||
|
||||
Ok((1000 * real_y + 4 * real_x + dir as usize).to_string())
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
|
||||
Reference in New Issue
Block a user