Compare commits

...

2 Commits

Author SHA1 Message Date
7724b14053 added idea to git ignore 2022-10-01 22:58:33 +02:00
6ba67d568c day19 cleanup 2022-10-01 22:56:08 +02:00
2 changed files with 24 additions and 15 deletions

1
.gitignore vendored
View File

@@ -1,3 +1,4 @@
/target /target
**/*.rs.bk **/*.rs.bk
.vscode .vscode
.idea

View File

@@ -4,8 +4,8 @@ use itertools::Itertools;
pub fn run() { pub fn run() {
let program = super::day05::load_ram("input/day19.txt"); let program = super::day05::load_ram("input/day19.txt");
//part1(&program, 50); part1(&program, 50);
part2(&program); // 8151016 too high part2(&program);
} }
fn part1(program: &RAM, max: i128) { fn part1(program: &RAM, max: i128) {
@@ -20,7 +20,7 @@ fn part1(program: &RAM, max: i128) {
.filter(|v| *v) .filter(|v| *v)
.count(); .count();
println!("{result}"); println!("Part 1: {result}");
} }
const W: i128 = 100; const W: i128 = 100;
@@ -35,29 +35,37 @@ fn part2(program: &RAM) {
map.insert((x, y), read_coordinate(program, x, y)); map.insert((x, y), read_coordinate(program, x, y));
}) })
}); });
for starts in 0..=MAX_PERIMETER { for perimeter in 0..=MAX_PERIMETER {
println!("checking perimeter {starts}"); extend_horizon(program, &mut map, perimeter);
extend_horizon(program, &mut map, starts); if let Some((x, y)) = check_all_squares_starting_at(&map, perimeter) {
check_all_squares_at(&map, starts); let result = x * 10_000 + y;
println!("Part 2: starts at {x},{y} -> {}", result);
break;
}
} }
} }
fn check_all_squares_at(map: &HashMap<(i128, i128), bool>, starts: i128) { fn check_all_squares_starting_at(map: &HashMap<(i128, i128), bool>, perimeter: i128) -> Option<(i128, i128)> {
(0..=starts).for_each(|i| { for i in 0..=perimeter {
check_fits(map, i, starts); if let Some(point) = check_fits(map, i, perimeter) {
check_fits(map, starts, i); return Some(point);
}); }
if let Some(point) = check_fits(map, perimeter, i) {
return Some(point);
}
}
None
} }
fn check_fits(map: &HashMap<(i128, i128), bool>, x: i128, y: 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 + W - 1, y)];
let ll = map[&(x, y + W - 1)]; let ll = map[&(x, y + W - 1)];
let lr = map[&(x + W, y + W - 1)]; let lr = map[&(x + W, y + W - 1)];
if ul && ur && ll && lr { if ul && ur && ll && lr {
let result = x * 10_000 + y; return Some((x, y));
panic!("found it! starts at {x},{y} -> {}", result);
} }
None
} }
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) {