day19 cleanup

This commit is contained in:
2022-10-01 23:00:21 +02:00
parent 7724b14053
commit 1686c8a28c

View File

@@ -8,10 +8,10 @@ pub fn run() {
part2(&program); part2(&program);
} }
fn part1(program: &RAM, max: i128) { fn part1(program: &RAM, max_perimeter: i128) {
let result = (0..max) let result = (0..max_perimeter)
.flat_map(|x| { .flat_map(|x| {
(0..max) (0..max_perimeter)
.map(|y| { .map(|y| {
read_coordinate(program, x, y) read_coordinate(program, x, y)
}) })
@@ -23,19 +23,18 @@ fn part1(program: &RAM, max: i128) {
println!("Part 1: {result}"); println!("Part 1: {result}");
} }
const W: i128 = 100; const SHIP_SIZE: i128 = 100;
const MAX_PERIMETER: i128 = 1_000_000_000;
fn part2(program: &RAM) { fn part2(program: &RAM) {
let mut map: HashMap<(i128, i128), bool> = HashMap::new(); let mut map: HashMap<(i128, i128), bool> = HashMap::new();
(0..W) (0..SHIP_SIZE)
.for_each(|x| { .for_each(|x| {
(0..W) (0..SHIP_SIZE)
.for_each(|y| { .for_each(|y| {
map.insert((x, y), read_coordinate(program, x, y)); map.insert((x, y), read_coordinate(program, x, y));
}) })
}); });
for perimeter in 0..=MAX_PERIMETER { for perimeter in 0.. {
extend_horizon(program, &mut map, perimeter); extend_horizon(program, &mut map, perimeter);
if let Some((x, y)) = check_all_squares_starting_at(&map, perimeter) { if let Some((x, y)) = check_all_squares_starting_at(&map, perimeter) {
let result = x * 10_000 + y; let result = x * 10_000 + y;
@@ -59,9 +58,9 @@ fn check_all_squares_starting_at(map: &HashMap<(i128, i128), bool>, perimeter: i
fn check_fits(map: &HashMap<(i128, i128), bool>, x: i128, y: i128) -> Option<(i128, i128)> { fn check_fits(map: &HashMap<(i128, i128), bool>, x: i128, y: i128) -> Option<(i128, i128)> {
let ul = map[&(x, y)]; let ul = map[&(x, y)];
let ur = map[&(x + W - 1, y)]; let ur = map[&(x + SHIP_SIZE - 1, y)];
let ll = map[&(x, y + W - 1)]; let ll = map[&(x, y + SHIP_SIZE - 1)];
let lr = map[&(x + W, y + W - 1)]; let lr = map[&(x + SHIP_SIZE, y + SHIP_SIZE - 1)];
if ul && ur && ll && lr { if ul && ur && ll && lr {
return Some((x, y)); return Some((x, y));
} }
@@ -69,8 +68,8 @@ fn check_fits(map: &HashMap<(i128, i128), bool>, x: i128, y: i128) -> Option<(i1
} }
fn extend_horizon(program: &RAM, map: &mut HashMap<(i128, i128), bool>, starts: i128) { fn extend_horizon(program: &RAM, map: &mut HashMap<(i128, i128), bool>, starts: i128) {
(0..=W + starts).for_each(|i| { (0..=SHIP_SIZE + starts).for_each(|i| {
let j = W + starts; let j = SHIP_SIZE + starts;
map.insert((j, i), read_coordinate(program, j, i)); map.insert((j, i), read_coordinate(program, j, i));
map.insert((i, j), read_coordinate(program, i, j)); map.insert((i, j), read_coordinate(program, i, j));
}); });