days 12 through 15
This commit is contained in:
parent
e4e8e41dfa
commit
1f57dd6464
BIN
day12/__pycache__/day12.cpython-311.pyc
Normal file
BIN
day12/__pycache__/day12.cpython-311.pyc
Normal file
Binary file not shown.
86
day12/day12.py
Normal file
86
day12/day12.py
Normal file
@ -0,0 +1,86 @@
|
||||
import re
|
||||
import argparse
|
||||
def search(line, groups):
|
||||
#print("searching {} for {}".format(line, groups))
|
||||
#find the first ?
|
||||
if not precheck(line, groups):
|
||||
return
|
||||
if check(line, groups):
|
||||
#print("found a solutioN!")
|
||||
yield line
|
||||
return
|
||||
|
||||
if '?' not in line:
|
||||
return
|
||||
|
||||
#print("delving into subtree {}".format(line.replace('?', '#', 1)))
|
||||
yield from search(line.replace('?', "#", 1), groups)
|
||||
yield from search(line.replace('?', ".", 1), groups)
|
||||
|
||||
def precheck(line, groups):
|
||||
expected_hits = sum(groups)
|
||||
if line.count('#') > expected_hits:
|
||||
# if there are more hits in our candidate than the expected number, then this is a bad branch.
|
||||
# return false to skip it.
|
||||
#print("too many #")
|
||||
return False
|
||||
if line.count('.') > len(line) - expected_hits:
|
||||
# if there are moroe confirmed empties in our candidate than len - expected_hits, then this is a bad branch.
|
||||
# return false to skip it.
|
||||
#print("too many .")
|
||||
return False
|
||||
return True
|
||||
|
||||
def check(line, groups):
|
||||
if '?' in line:
|
||||
return False
|
||||
line_list = list(map(lambda x: len(x), line.replace(".", " ").split()))
|
||||
if len(line_list) == len(groups):
|
||||
return all(map(lambda x: x[0] == x[1], zip(line_list, groups)))
|
||||
return False
|
||||
return groups == line_list
|
||||
|
||||
|
||||
|
||||
|
||||
parser = argparse.ArgumentParser()
|
||||
parser.add_argument("filename", help="the file from which to take data")
|
||||
args = parser.parse_args()
|
||||
|
||||
with open(args.filename, "r") as f:
|
||||
line_re = re.compile("([.?#]*) ([0-9,]*)")
|
||||
s = 0
|
||||
for l in f.readlines():
|
||||
m = line_re.match(l)
|
||||
line = m.group(1)
|
||||
groups = list(map(int, m.group(2).split(',')))
|
||||
#print("line is {} and groups are {}".format(line, groups))
|
||||
#for option in search(line, groups):
|
||||
# print(option)
|
||||
|
||||
x = len(list(search(line,groups)))
|
||||
#print("there are {} solutinos".format(x))
|
||||
s += x
|
||||
print("the total solutionss are {}".format(s))
|
||||
|
||||
|
||||
print("and noow for part b")
|
||||
|
||||
|
||||
with open(args.filename, "r") as f:
|
||||
line_re = re.compile("([.?#]*) ([0-9,]*)")
|
||||
s = 0
|
||||
for l in f.readlines():
|
||||
m = line_re.match(l)
|
||||
line = m.group(1)
|
||||
line = line + "?" + line + "?" + line + "?" + line + "?" + line
|
||||
groups = list(map(int, m.group(2).split(',')))
|
||||
groups = groups * 5
|
||||
print("line is {} and groups are {}".format(line, groups))
|
||||
#for option in search(line, groups):
|
||||
# print(option)
|
||||
|
||||
x = len(list(search(line,groups)))
|
||||
print("there are {} solutinos".format(x))
|
||||
s += x
|
||||
print("the total solutionss are {}".format(s))
|
7
day13/Cargo.lock
generated
Normal file
7
day13/Cargo.lock
generated
Normal file
@ -0,0 +1,7 @@
|
||||
# This file is automatically @generated by Cargo.
|
||||
# It is not intended for manual editing.
|
||||
version = 3
|
||||
|
||||
[[package]]
|
||||
name = "day13"
|
||||
version = "0.1.0"
|
8
day13/Cargo.toml
Normal file
8
day13/Cargo.toml
Normal file
@ -0,0 +1,8 @@
|
||||
[package]
|
||||
name = "day13"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
140
day13/src/main.rs
Normal file
140
day13/src/main.rs
Normal file
@ -0,0 +1,140 @@
|
||||
use std::iter::*;
|
||||
use std::fs;
|
||||
use std::env;
|
||||
use std::collections::HashSet;
|
||||
|
||||
fn main() {
|
||||
println!("Hello, AoC day 11!");
|
||||
|
||||
let args: Vec<String> = env::args().collect();
|
||||
if args.len() != 2 {
|
||||
println!("wrong number of arguments!");
|
||||
std::process::exit(1);
|
||||
}
|
||||
|
||||
let file_path = &args[1];
|
||||
|
||||
let contents = fs::read_to_string(file_path)
|
||||
.expect("Should have been able to read the file");
|
||||
|
||||
|
||||
let mut grids: Vec<Vec<String>> = Vec::new();
|
||||
|
||||
// let's build that grid
|
||||
let mut current_grid: Vec<String> = Vec::new();
|
||||
for line in contents.lines() {
|
||||
if line.trim().is_empty() {
|
||||
grids.push(current_grid);
|
||||
current_grid = Vec::new();
|
||||
} else {
|
||||
current_grid.push(line.to_string());
|
||||
}
|
||||
}
|
||||
grids.push(current_grid);
|
||||
|
||||
// now that we've got the whole input file sorted, let's process things!
|
||||
let mut s = 0;
|
||||
|
||||
for g in &grids {
|
||||
println!("starting on a grid");
|
||||
|
||||
let mirrors = find_mirrors(g);
|
||||
let mirrors_vert = find_mirrors(&transpose(g));
|
||||
print_map(g);
|
||||
println!("found mirrors at columns {mirrors:?}");
|
||||
println!("found mirrors at rows {mirrors_vert:?}");
|
||||
s += mirrors.iter().sum::<usize>();
|
||||
s += 100*mirrors_vert.iter().sum::<usize>();
|
||||
}
|
||||
|
||||
println!("sum of mirror offsets is {s}");
|
||||
|
||||
println!("now for part 2...");
|
||||
|
||||
s=0;
|
||||
for g in &grids {
|
||||
let original_mirrors = find_mirrors(g);
|
||||
let original_mirrors_vert = find_mirrors(&transpose(g));
|
||||
for (y, row) in g.iter().enumerate() {
|
||||
for x in 0..row.len() {
|
||||
//println!("smudging at {y}, {x}.");
|
||||
let new_grid = smudge(g, y, x);
|
||||
let mut mirrors: HashSet<usize> = find_mirrors(&new_grid);
|
||||
for x in &original_mirrors {
|
||||
mirrors.remove(&x);
|
||||
}
|
||||
let mut mirrors_vert: HashSet<usize> = find_mirrors(&transpose(&new_grid));
|
||||
for x in &original_mirrors_vert {
|
||||
mirrors_vert.remove(&x);
|
||||
}
|
||||
let n = mirrors.iter().sum::<usize>();
|
||||
let m = mirrors_vert.iter().sum::<usize>();
|
||||
//println!("found {mirrors:?} vertical and {mirrors_vert:?} horizontal mirrors.");
|
||||
s += n + 100*m;
|
||||
}
|
||||
}
|
||||
}
|
||||
s = s/2;
|
||||
println!("the sum was {s}");
|
||||
}
|
||||
|
||||
|
||||
fn smudge(g: &Vec<String>, i: usize, j: usize) -> Vec<String> {
|
||||
let mut output = Vec::new();
|
||||
|
||||
for (y, row) in g.iter().enumerate() {
|
||||
if y != i {
|
||||
output.push(row.clone());
|
||||
} else {
|
||||
let mut new = row.clone();
|
||||
match new.remove(j) {
|
||||
'.' => new.insert(j, '#'),
|
||||
'#' => new.insert(j, '.'),
|
||||
_ => panic!("ahjhhhhfslkjf")
|
||||
};
|
||||
output.push(new);
|
||||
}
|
||||
}
|
||||
|
||||
return output;
|
||||
}
|
||||
|
||||
fn print_map(g: &Vec<String>) {
|
||||
for row in g {
|
||||
println!("{}", row);
|
||||
}
|
||||
}
|
||||
|
||||
fn find_mirrors(mat: &Vec<String>) -> HashSet<usize> {
|
||||
let mut mirrors: HashSet<usize> = mirror_candidates(&mat[0]);
|
||||
for row in mat {
|
||||
let candidates = mirror_candidates(row);
|
||||
mirrors.retain(|x| candidates.contains(x));
|
||||
}
|
||||
return mirrors;
|
||||
}
|
||||
fn mirror_candidates(row: &String) -> HashSet<usize> {
|
||||
let mut output: HashSet<usize> = HashSet::new();
|
||||
for i in 0..row.len()-1 {
|
||||
// try after character i
|
||||
let mut works = true;
|
||||
for x in 0..(row.len()/2) {
|
||||
if (i < x) || (i+1+x >= row.len()) {break;}
|
||||
if row.chars().nth(i-x) != row.chars().nth(i+1+x) {works = false;}
|
||||
}
|
||||
if works {output.insert(i+1);}
|
||||
}
|
||||
return output;
|
||||
}
|
||||
|
||||
fn transpose(mat: &Vec<String>) -> Vec<String> {
|
||||
let mut output = Vec::new();
|
||||
for i in 0..mat[0].len() {
|
||||
let mut l: String = String::from("");
|
||||
for row in mat {
|
||||
l.push_str(&row.chars().nth(i).expect("this had better work").to_string());
|
||||
}
|
||||
output.push(l);
|
||||
}
|
||||
return output;
|
||||
}
|
1
day13/target/.rustc_info.json
Normal file
1
day13/target/.rustc_info.json
Normal file
@ -0,0 +1 @@
|
||||
{"rustc_fingerprint":11672801278650259402,"outputs":{"4614504638168534921":{"success":true,"status":"","code":0,"stdout":"rustc 1.69.0 (84c898d65 2023-04-16)\nbinary: rustc\ncommit-hash: 84c898d65adf2f39a5a98507f1fe0ce10a2b8dbc\ncommit-date: 2023-04-16\nhost: x86_64-apple-darwin\nrelease: 1.69.0\nLLVM version: 15.0.7\n","stderr":""},"15729799797837862367":{"success":true,"status":"","code":0,"stdout":"___\nlib___.rlib\nlib___.dylib\nlib___.dylib\nlib___.a\nlib___.dylib\n/Users/shoofle/.rustup/toolchains/stable-x86_64-apple-darwin\noff\npacked\nunpacked\n___\ndebug_assertions\npanic=\"unwind\"\nproc_macro\ntarget_arch=\"x86_64\"\ntarget_endian=\"little\"\ntarget_env=\"\"\ntarget_family=\"unix\"\ntarget_feature=\"cmpxchg16b\"\ntarget_feature=\"fxsr\"\ntarget_feature=\"sse\"\ntarget_feature=\"sse2\"\ntarget_feature=\"sse3\"\ntarget_feature=\"ssse3\"\ntarget_has_atomic=\"128\"\ntarget_has_atomic=\"16\"\ntarget_has_atomic=\"32\"\ntarget_has_atomic=\"64\"\ntarget_has_atomic=\"8\"\ntarget_has_atomic=\"ptr\"\ntarget_os=\"macos\"\ntarget_pointer_width=\"64\"\ntarget_vendor=\"apple\"\nunix\n","stderr":""}},"successes":{}}
|
3
day13/target/CACHEDIR.TAG
Normal file
3
day13/target/CACHEDIR.TAG
Normal file
@ -0,0 +1,3 @@
|
||||
Signature: 8a477f597d28d172789f06886806bc55
|
||||
# This file is a cache directory tag created by cargo.
|
||||
# For information about cache directory tags see https://bford.info/cachedir/
|
0
day13/target/debug/.cargo-lock
Normal file
0
day13/target/debug/.cargo-lock
Normal file
@ -0,0 +1 @@
|
||||
7e2b4abee0c673f5
|
@ -0,0 +1 @@
|
||||
{"rustc":3659767333214291318,"features":"[]","target":14090744556205886698,"profile":11736316127369858332,"path":1684066648322511884,"deps":[],"local":[{"CheckDepInfo":{"dep_info":"debug/.fingerprint/day13-77b2a47bbbbb8c09/dep-bin-day13"}}],"rustflags":[],"metadata":7797948686568424061,"config":2202906307356721367,"compile_kind":0}
|
Binary file not shown.
@ -0,0 +1 @@
|
||||
This file has an mtime of when this was started.
|
BIN
day13/target/debug/day13
Executable file
BIN
day13/target/debug/day13
Executable file
Binary file not shown.
1
day13/target/debug/day13.d
Normal file
1
day13/target/debug/day13.d
Normal file
@ -0,0 +1 @@
|
||||
/Users/shoofle/Projects/aoc_2023/day13/target/debug/day13: /Users/shoofle/Projects/aoc_2023/day13/src/main.rs
|
BIN
day13/target/debug/deps/day13-77b2a47bbbbb8c09
Executable file
BIN
day13/target/debug/deps/day13-77b2a47bbbbb8c09
Executable file
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
5
day13/target/debug/deps/day13-77b2a47bbbbb8c09.d
Normal file
5
day13/target/debug/deps/day13-77b2a47bbbbb8c09.d
Normal file
@ -0,0 +1,5 @@
|
||||
/Users/shoofle/Projects/aoc_2023/day13/target/debug/deps/day13-77b2a47bbbbb8c09: src/main.rs
|
||||
|
||||
/Users/shoofle/Projects/aoc_2023/day13/target/debug/deps/day13-77b2a47bbbbb8c09.d: src/main.rs
|
||||
|
||||
src/main.rs:
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in New Issue
Block a user