d11 + d12

This commit is contained in:
JonOfUs 2023-12-13 00:26:48 +01:00
parent db441a2940
commit eac5a9dff7
9 changed files with 1369 additions and 1 deletions

7
.idea/misc.xml Normal file
View file

@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="Black">
<option name="sdkName" value="Python 3.11" />
</component>
<component name="ProjectRootManager" version="2" project-jdk-name="Python 3.11" project-jdk-type="Python SDK" />
</project>

15
python/day11.py Normal file
View file

@ -0,0 +1,15 @@
path = 'res/01/input'
space = []
with open(path, 'r') as file:
for line in file:
space.append([1 if c is '#' else 0 for c in line])
if '#' not in line:
space.append([0 for _ in line])
ind = set()
for i in range(len(space[0])):
if '#' not in [space[i][j] for j in range(len(space))]:
ind.add(i)

10
res/11/example Normal file
View file

@ -0,0 +1,10 @@
...#......
.......#..
#.........
..........
......#...
.#........
.........#
..........
.......#..
#...#.....

140
res/11/input Normal file
View file

@ -0,0 +1,140 @@
................................................#.........#............................................#....................................
..#.............#......#.............................................#...........................................................#..........
..............................................................#..............................#..............................................
.................................................................................................................#..........................
...........#...........................#...............................................#..................................#.............#...
............................#..................#.....#..........................#...........................................................
..................................#................................#........................................................................
...........................................................................#................................................................
.....#...................................#......................................................#.......................#...........#.....#.
.....................#........#....................#....................................................#...................................
......................................................................#.....................................................................
...........................................................#.................................#..............................................
........#..................#....................#....................................................#......................................
#.............#....................................................#..............#.............................#...........................
...................#.............#...................................................................................#...............#......
.............................................................................#.............................................................#
.........................................................................................................#..................................
....#...........................................................................................#...........................#.....#.........
.....................#.......................#.........#....................................................................................
#............#.........................................................................#....................#...............................
................................#..................#...........#......#............................#...................#.......#............
.......................................................................................................................................#....
...................................................................................#........................................................
.........................#.............#...................................................................................#................
............................................................................................................................................
..........#..............................................................#......#.........#..........#......................................
............................................................................................................................................
.................................................................................................#............................#.............
................#...............................#............#.....................#........................................................
.#........................#..........................#......................................................#.......................#.......
.......#....................................#................................#..............................................................
.................................#.....................................#..................................................................#.
........................................................#.............................#...............................#........#............
.....................#...................#.....#...............#............................................................................
.............#...................................................................................................#..........................
................................................................................................#.....#...............................#.....
.................#................#................#........................................................................................
.#..........................#.............................#.......#.......#................................................................#
.............................................#.............................................................#................................
...........#.................................................................................#...............................#..............
.....................#.................................#.......#..................................................................#.....#...
.......#..............................#..............................................................#......................................
#...............................#.....................................#...............#........................#.......#....................
...........................................................................#.....#.........................................................#
...........................................................................................#................................................
........................................................#................................................#.......................#..........
.........#............#............................................#.................................................#.....#................
............................................................................................................................................
......................................#......#........................................................#.........#...........................
.............#.....#......#..........................#......................................................................................
......#................................................................................................................................#....
.............................................................................................................#..............................
..#.......#.............................#.....................#..................#........#.................................................
............................................................................................................................................
......................................................................................#...............................#.....................
..................#...........................#...........................#.........................#.........................#.............
............................................................................................................................................
......................................#..................................................#.........................................#........
.......................#.....#................................................................#.............#............#..................
.....#.............................................#.............#..........................................................................
.............#...........................................................#.......#..................................#...........#...........
.................................#..........................................................................................................
.............................................#...........#..................................................................................
.......#......................................................#............................#.........#........#.............................
.....................#................................................#...........................................................#.........
...............#...................#...............#.......................................................................#................
#..........................#......................................................................#.........................................
.............................................................................#..............................................................
...............................#......#.................#..............................................#..........#.........................
...........................................................................................................................................#
.................#......#.......................#....................................#.....#..............................#.................
.........#.............................................................#..................................#.................................
....................................................#............#............#.........................................................#...
..#.........................................................#........................................................#..........#...........
............................................................................................................................................
............................................................................................................................................
.......#...........................#..............................................................#.........................................
....................#...................................#................................................................#..................
..............................#.............................................................................................................
..#......................#.............................................#........#..........#................................................
..........#.............................#......................#................................................................#...........
.....................................................................................................#......................................
......................#.............................................................................................#......................#
#................#..................................#........................................#.................#............................
...................................#......#.......................#...........#..........................................#.............#....
............................................................................................................................................
..........#..............................................#..................................................................................
............................................................................................................................................
....................#......#..........#.......................#......#....................#..........#.......#..................#...........
...#.........................................................................#..............................................................
..................................................................................#..............#..........................................
........#.......................#..........#................................................................................................
..............#...........................................................................................#.................................
................................................................#.......................................................................#...
.......................#................#.....................................................................#.............................
....#............#............#.................................................................#.....#.....................................
...................................................................................................................#........................
...........................................#..............................................................................#.................
.......#.........................................#.......#.......#.....#...............................................................#....
................................................................................#...........#.....................................#.........
...........#.....................................................................................#..........................................
..............................................................#.............................................................................
...............#.........#..........#........................................................................#..............................
...........................................#......................................................................#......#..................
....#...............#.......................................................................................................................
......................................................................................#..............................................#......
...............................................#...............#..............#.................#...........................................
.#.........................................................................................#.........................#......................
................#......#................#................................#..........................#...........#...........................
......#......................#.....................#.......#................................................................................
.............................................#...........................................................#...............#.....#........#...
...........#.........................................................................#......................................................
............................................................................#...............................................................
.........................................................................................#............................#.....................
...................#...................#.....................#....................................#.............#...........................
.................................#..........................................................................................................
.......#......#...........#..................................................................................................#.....#........
.......................................................................#..............#........#.............#..............................
......................#..................#......#..............#..............#.....................................#..................#....
..........................................................................................#.............#...................................
.........#..........................................#..............#.......................................................#................
.............................#..............................................................................................................
.............................................#...................................#..........................................................
................#..................#........................................#......................................#........................
....#.................#.................#.............................................#.................................#..................#
............................................................#.........#..................................#..................................
......................................................#...................................#.................................................
.................................#..................................................................#.......................................
.................................................#...................................................................................#......
............................................................................................................................................
..................#.............................................#.............#.........................#................#..................
.........#.................................................#...........#.............#........#..............#............................#.
...................................#.........................................................................................#..............
............................................................................................................................................
#.....................#...................................................#.......#...............................#.........................
..............................#.................#........#..........................................................................#.......
..............#........................................................................#....................................................
...........................................#.............................................................#..............................#...
.................................#...................................................................................#...........#..........
...........#.......#.......#...........................#.....#................#......................#........#.............................

6
res/12/example Normal file
View file

@ -0,0 +1,6 @@
???.### 1,1,3
.??..??...?##. 1,1,3
?#?#?#?#?#?#?#? 1,3,1,6
????.#...#... 4,1,1
????.######..#####. 1,6,5
?###???????? 3,2,1

1000
res/12/input Normal file

File diff suppressed because it is too large Load diff

68
src/days/d11.rs Normal file
View file

@ -0,0 +1,68 @@
use std::fs;
pub fn solve() {
let path = "res/11/input";
let contents = fs::read_to_string(path).expect("I/O error, wrong path?");
//let contents = BufReader::new(fs::File::open(path).expect("I/O error, wrong path?"));
let mut galaxies: Vec<(usize, usize)> = Vec::new();
for (i, line) in contents.lines().enumerate() {
for (j, c) in line.chars().enumerate() {
if c == '#' {
galaxies.push((i, j))
}
}
}
let mut result = 0;
for i in 0..galaxies.len() {
for j in i + 1..galaxies.len() {
let (x1, y1) = galaxies[i];
let (x2, y2) = galaxies[j];
result += x1.abs_diff(x2) + y1.abs_diff(y2);
}
}
for (i, row) in contents.lines().enumerate() {
if !row.contains('#') {
let top = galaxies.iter().filter(|(x, _)| *x < i).count();
let bot = galaxies.len() - top;
result += top * bot;
}
}
let space: Vec<Vec<char>> = contents
.lines()
.map(|l| l.chars().collect::<Vec<_>>())
.collect();
for j in 0..space[0].len() {
let col: Vec<char> = (0..space.len()).map(|i| space[i][j]).collect();
if !col.contains(&'#') {
let left = galaxies.iter().filter(|(_, y)| *y < j).count();
let right = galaxies.len() - left;
result += right * left;
}
}
println!("Result 1: {result}");
for (i, row) in contents.lines().enumerate() {
if !row.contains('#') {
let top = galaxies.iter().filter(|(x, _)| *x < i).count();
let bot = galaxies.len() - top;
result += top * bot * 999998;
}
}
for j in 0..space[0].len() {
let col: Vec<char> = (0..space.len()).map(|i| space[i][j]).collect();
if !col.contains(&'#') {
let left = galaxies.iter().filter(|(_, y)| *y < j).count();
let right = galaxies.len() - left;
result += right * left * 999998;
}
}
println!("Result 2: {result}");
}

122
src/days/d12.rs Normal file
View file

@ -0,0 +1,122 @@
use std::{collections::HashMap, fs};
use rayon::prelude::*;
pub fn solve() {
let path = "res/12/input";
let contents = fs::read_to_string(path).expect("I/O error, wrong path?");
//let contents = BufReader::new(fs::File::open(path).expect("I/O error, wrong path?"));
let result: usize = contents
.par_lines()
.map(|line| {
let (springs_str, nums_str) = line.split_once(" ").unwrap();
let nums: Vec<usize> = nums_str
.split(",")
.map(|n| n.parse::<usize>().unwrap())
.collect();
let mut springs: Vec<char> = springs_str.chars().collect();
springs.push('.');
let mut cache = HashMap::new();
count_spring_arrangements(&mut cache, springs_str.as_bytes(), None, &nums)
})
.sum();
println!("Result 1: {result}");
let result: usize = contents
.lines()
.map(|line| {
let (springs_str, nums_str) = line.split_once(" ").unwrap();
let nums_tmp: Vec<usize> = nums_str
.split(",")
.map(|n| n.parse::<usize>().unwrap())
.collect();
let nums: Vec<usize> = nums_tmp
.iter()
.cycle()
.take(nums_tmp.len() * 5)
.map(|i| *i)
.collect();
let springs: String = std::iter::repeat(String::from(springs_str))
.take(5)
.collect::<Vec<String>>()
.join("?");
let mut cache = HashMap::new();
count_spring_arrangements(&mut cache, springs.as_bytes(), None, &nums)
})
.sum();
println!("Result 2: {result}");
}
fn count_spring_arrangements(
cache: &mut HashMap<(usize, usize, usize), usize>,
springs: &[u8],
in_broken_group: Option<usize>,
remaining_broken: &[usize],
) -> usize {
if springs.is_empty() {
return match (in_broken_group, remaining_broken.len()) {
(None, 0) => 1,
(Some(x), 1) if x == remaining_broken[0] => 1,
_ => 0,
};
}
if in_broken_group.is_some() && remaining_broken.is_empty() {
return 0;
}
let key = (
springs.len(),
in_broken_group.unwrap_or(0),
remaining_broken.len(),
);
if let Some(&result) = cache.get(&key) {
return result;
}
let ways = match (springs[0], in_broken_group) {
(b'.', Some(x)) if x != remaining_broken[0] => 0,
(b'.', Some(_)) => {
count_spring_arrangements(cache, &springs[1..], None, &remaining_broken[1..])
}
(b'.', None) => count_spring_arrangements(cache, &springs[1..], None, remaining_broken),
(b'#', Some(_)) => count_spring_arrangements(
cache,
&springs[1..],
in_broken_group.map(|x| x + 1),
remaining_broken,
),
(b'#', None) => count_spring_arrangements(cache, &springs[1..], Some(1), remaining_broken),
(b'?', Some(x)) => {
let mut ans = count_spring_arrangements(
cache,
&springs[1..],
in_broken_group.map(|x| x + 1),
remaining_broken,
);
if x == remaining_broken[0] {
ans +=
count_spring_arrangements(cache, &springs[1..], None, &remaining_broken[1..]);
}
ans
}
(b'?', None) => {
count_spring_arrangements(cache, &springs[1..], Some(1), remaining_broken)
+ count_spring_arrangements(cache, &springs[1..], None, remaining_broken)
}
_ => unreachable!(),
};
cache.insert(key, ways);
ways
}

View file

@ -3,7 +3,7 @@ pub mod days;
fn main() { fn main() {
days::d10::solve() days::d12::solve()
//_all_days() //_all_days()
} }