Compare commits

..

3 Commits

Author SHA1 Message Date
3c72884fac day19 part 2 2022-10-01 22:43:37 +02:00
3992bf2e58 day19 part 1 2022-10-01 21:02:51 +02:00
357c5d42cc refactoring 2022-09-25 10:46:09 +02:00
5 changed files with 110 additions and 13 deletions

1
input/day19.txt Normal file
View File

@@ -0,0 +1 @@
109,424,203,1,21101,0,11,0,1105,1,282,21101,18,0,0,1105,1,259,2101,0,1,221,203,1,21101,31,0,0,1105,1,282,21102,38,1,0,1106,0,259,20101,0,23,2,21201,1,0,3,21101,1,0,1,21101,0,57,0,1105,1,303,2101,0,1,222,21001,221,0,3,21001,221,0,2,21101,259,0,1,21101,0,80,0,1106,0,225,21102,117,1,2,21102,91,1,0,1105,1,303,2101,0,1,223,20102,1,222,4,21102,1,259,3,21101,0,225,2,21102,1,225,1,21101,0,118,0,1105,1,225,21001,222,0,3,21102,1,77,2,21102,133,1,0,1105,1,303,21202,1,-1,1,22001,223,1,1,21102,1,148,0,1105,1,259,2102,1,1,223,21002,221,1,4,20101,0,222,3,21102,20,1,2,1001,132,-2,224,1002,224,2,224,1001,224,3,224,1002,132,-1,132,1,224,132,224,21001,224,1,1,21102,195,1,0,106,0,109,20207,1,223,2,20102,1,23,1,21101,0,-1,3,21101,0,214,0,1106,0,303,22101,1,1,1,204,1,99,0,0,0,0,109,5,1202,-4,1,249,21201,-3,0,1,21201,-2,0,2,22101,0,-1,3,21102,250,1,0,1106,0,225,22101,0,1,-4,109,-5,2105,1,0,109,3,22107,0,-2,-1,21202,-1,2,-1,21201,-1,-1,-1,22202,-1,-2,-2,109,-3,2106,0,0,109,3,21207,-2,0,-1,1206,-1,294,104,0,99,21202,-2,1,-2,109,-3,2105,1,0,109,5,22207,-3,-4,-1,1206,-1,346,22201,-4,-3,-4,21202,-3,-1,-1,22201,-4,-1,2,21202,2,-1,-1,22201,-4,-1,1,21202,-2,1,3,21102,1,343,0,1105,1,303,1106,0,415,22207,-2,-3,-1,1206,-1,387,22201,-3,-2,-3,21202,-2,-1,-1,22201,-3,-1,3,21202,3,-1,-1,22201,-3,-1,2,21202,-4,1,1,21102,384,1,0,1106,0,303,1105,1,415,21202,-4,-1,-4,22201,-4,-3,-4,22202,-3,-2,-2,22202,-2,-4,-4,22202,-3,-2,-3,21202,-4,-1,-2,22201,-3,-2,1,22101,0,1,-4,109,-5,2105,1,0

View File

@@ -1,5 +1,5 @@
mod tasks; mod tasks;
fn main() { fn main() {
tasks::day22::run(); tasks::day19::run();
} }

View File

@@ -2,11 +2,11 @@ use itertools::Itertools;
use num_integer::Integer; use num_integer::Integer;
use std::iter; use std::iter;
const INPUT: &str = "59750530221324194853012320069589312027523989854830232144164799228029162830477472078089790749906142587998642764059439173975199276254972017316624772614925079238407309384923979338502430726930592959991878698412537971672558832588540600963437409230550897544434635267172603132396722812334366528344715912756154006039512272491073906389218927420387151599044435060075148142946789007756800733869891008058075303490106699737554949348715600795187032293436328810969288892220127730287766004467730818489269295982526297430971411865028098708555709525646237713045259603175397623654950719275982134690893685598734136409536436003548128411943963263336042840301380655801969822";
#[allow(dead_code)] #[allow(dead_code)]
pub fn run() { pub fn run() {
let input = "03036732577212944063491565474664"; let input = to_digits(INPUT);
// let input = "59750530221324194853012320069589312027523989854830232144164799228029162830477472078089790749906142587998642764059439173975199276254972017316624772614925079238407309384923979338502430726930592959991878698412537971672558832588540600963437409230550897544434635267172603132396722812334366528344715912756154006039512272491073906389218927420387151599044435060075148142946789007756800733869891008058075303490106699737554949348715600795187032293436328810969288892220127730287766004467730818489269295982526297430971411865028098708555709525646237713045259603175397623654950719275982134690893685598734136409536436003548128411943963263336042840301380655801969822";
let input = to_digits(input);
task1(input.clone()); task1(input.clone());
task2(input.clone()); task2(input.clone());
} }
@@ -90,18 +90,38 @@ mod test {
use super::*; use super::*;
#[test] #[test]
fn test_part_1() { fn test_part_1_examples() {
assert_eq!(task1(to_digits("80871224585914546619083218645595")), "24176176"); assert_eq!(
assert_eq!(task1(to_digits("19617804207202209144916044189917")), "73745418"); task1(to_digits("80871224585914546619083218645595")),
assert_eq!(task1(to_digits("69317163492948606335995924319873")), "52432133"); "24176176"
);
assert_eq!(
task1(to_digits("19617804207202209144916044189917")),
"73745418"
);
assert_eq!(
task1(to_digits("69317163492948606335995924319873")),
"52432133"
);
}
#[test]
fn test_part_1_solution() {
assert_eq!(task1(to_digits(INPUT)), "84487724");
} }
#[test] #[test]
fn test_part_2() { fn test_part_2() {
assert_eq!(
assert_eq!(task2(to_digits("02935109699940807407585447034323")), "78725270"); task2(to_digits("02935109699940807407585447034323")),
assert_eq!(task2(to_digits("03036732577212944063491565474664")), "84462026"); "78725270"
assert_eq!(task2(to_digits("03081770884921959731165446850517")), "53553731"); );
assert_eq!(
task2(to_digits("03036732577212944063491565474664")),
"84462026"
);
assert_eq!(
task2(to_digits("03081770884921959731165446850517")),
"53553731"
);
} }
} }

75
src/tasks/day19.rs Normal file
View File

@@ -0,0 +1,75 @@
use std::collections::HashMap;
use super::day05::{IntCodeComputer, RAM};
use itertools::Itertools;
pub fn run() {
let program = super::day05::load_ram("input/day19.txt");
//part1(&program, 50);
part2(&program); // 8151016 too high
}
fn part1(program: &RAM, max: i128) {
let result = (0..max)
.flat_map(|x| {
(0..max)
.map(|y| {
read_coordinate(program, x, y)
})
.collect_vec()
})
.filter(|v| *v)
.count();
println!("{result}");
}
const W: i128 = 100;
const MAX_PERIMETER: i128 = 1_000_000_000;
fn part2(program: &RAM) {
let mut map: HashMap<(i128, i128), bool> = HashMap::new();
(0..W)
.for_each(|x| {
(0..W)
.for_each(|y| {
map.insert((x, y), read_coordinate(program, x, y));
})
});
for starts in 0..=MAX_PERIMETER {
println!("checking perimeter {starts}");
extend_horizon(program, &mut map, starts);
check_all_squares_at(&map, starts);
}
}
fn check_all_squares_at(map: &HashMap<(i128, i128), bool>, starts: i128) {
(0..=starts).for_each(|i| {
check_fits(map, i, starts);
check_fits(map, starts, i);
});
}
fn check_fits(map: &HashMap<(i128, i128), bool>, x: i128, y: i128) {
let ul = map[&(x, y)];
let ur = map[&(x + W - 1, y)];
let ll = map[&(x, y + W - 1)];
let lr = map[&(x + W, y + W - 1)];
if ul && ur && ll && lr {
let result = x * 10_000 + y;
panic!("found it! starts at {x},{y} -> {}", result);
}
}
fn extend_horizon(program: &RAM, map: &mut HashMap<(i128, i128), bool>, starts: i128) {
(0..=W + starts).for_each(|i| {
let j = W + starts;
map.insert((j, i), read_coordinate(program, j, i));
map.insert((i, j), read_coordinate(program, i, j));
});
}
fn read_coordinate(program: &RAM, x: i128, y: i128) -> bool {
let mut computer = IntCodeComputer::new(vec![x, y], program.clone());
computer.run_until_end();
*computer.get_output().first().unwrap() == 1
}

View File

@@ -15,6 +15,7 @@ pub mod day15;
pub mod day16; pub mod day16;
pub mod day17; pub mod day17;
pub mod day18; pub mod day18;
pub mod day19;
pub mod day21; pub mod day21;
#[allow(dead_code)] #[allow(dead_code)]
pub mod day22; pub mod day22;