Remove debug prints for D17, cleanup later™️
This commit is contained in:
parent
887499b02b
commit
34fe98bb4d
|
@ -1,9 +1,9 @@
|
||||||
use grid::*;
|
use grid::*;
|
||||||
use intcode::*;
|
use intcode::*;
|
||||||
|
use itertools::Itertools;
|
||||||
use std::char;
|
use std::char;
|
||||||
use std::collections::{HashMap, HashSet};
|
use std::collections::{HashMap, HashSet};
|
||||||
use std::fmt;
|
use std::fmt;
|
||||||
use itertools::Itertools;
|
|
||||||
|
|
||||||
#[derive(Debug, PartialEq, Eq, Hash, Clone)]
|
#[derive(Debug, PartialEq, Eq, Hash, Clone)]
|
||||||
struct Movement {
|
struct Movement {
|
||||||
|
@ -14,7 +14,7 @@ struct Movement {
|
||||||
impl fmt::Display for Movement {
|
impl fmt::Display for Movement {
|
||||||
fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
|
fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
|
||||||
let dir_char = if self.rotation == 1 { 'R' } else { 'L' };
|
let dir_char = if self.rotation == 1 { 'R' } else { 'L' };
|
||||||
write!(f, "{}{}", dir_char, self.distance)
|
write!(f, "{},{}", dir_char, self.distance)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -42,29 +42,6 @@ fn find_commands(field: &HashMap<Position2D, char>) -> Vec<Movement> {
|
||||||
commands
|
commands
|
||||||
}
|
}
|
||||||
|
|
||||||
fn test_input() -> HashMap<Position2D, char> {
|
|
||||||
"#######...#####
|
|
||||||
#.....#...#...#
|
|
||||||
#.....#...#...#
|
|
||||||
......#...#...#
|
|
||||||
......#...###.#
|
|
||||||
......#.....#.#
|
|
||||||
^########...#.#
|
|
||||||
......#.#...#.#
|
|
||||||
......#########
|
|
||||||
........#...#..
|
|
||||||
....#########..
|
|
||||||
....#...#......
|
|
||||||
....#...#......
|
|
||||||
....#...#......
|
|
||||||
....#####......"
|
|
||||||
.lines()
|
|
||||||
.rev()
|
|
||||||
.enumerate()
|
|
||||||
.flat_map(move |(y, s)| s.chars().enumerate().map(move |(x, c)| ((x, y).into(), c)))
|
|
||||||
.collect()
|
|
||||||
}
|
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
// The main reason I use a hashmap here (instead of a 2D vector) is that my abstractions for
|
// The main reason I use a hashmap here (instead of a 2D vector) is that my abstractions for
|
||||||
// ascii stuff all use maps ヽ( ゚ヮ・)ノ
|
// ascii stuff all use maps ヽ( ゚ヮ・)ノ
|
||||||
|
@ -75,12 +52,11 @@ fn main() {
|
||||||
.map(|n| char::from_u32(*n as u32).unwrap())
|
.map(|n| char::from_u32(*n as u32).unwrap())
|
||||||
.collect::<String>()
|
.collect::<String>()
|
||||||
.lines()
|
.lines()
|
||||||
// this rev breaks part 1 but is necessary for part 2
|
// this rev breaks part 1 but is necessary for part 2. remove it to get the part 1 solution
|
||||||
.rev()
|
.rev()
|
||||||
.enumerate()
|
.enumerate()
|
||||||
.flat_map(move |(y, s)| s.chars().enumerate().map(move |(x, c)| ((x, y).into(), c)))
|
.flat_map(move |(y, s)| s.chars().enumerate().map(move |(x, c)| ((x, y).into(), c)))
|
||||||
.collect();
|
.collect();
|
||||||
// let field = test_input();
|
|
||||||
let p1 = field
|
let p1 = field
|
||||||
.iter()
|
.iter()
|
||||||
.filter(|(pos, obj)| {
|
.filter(|(pos, obj)| {
|
||||||
|
@ -92,33 +68,10 @@ fn main() {
|
||||||
})
|
})
|
||||||
.fold(0, |acc, (pos, _)| acc + pos.x * pos.y);
|
.fold(0, |acc, (pos, _)| acc + pos.x * pos.y);
|
||||||
println!("Part 1: {}", p1);
|
println!("Part 1: {}", p1);
|
||||||
println!("{}", draw_ascii(&field, '.'));
|
|
||||||
|
|
||||||
let commands = find_commands(&field);
|
let commands = find_commands(&field);
|
||||||
println!("Commands: {}", commands.len());
|
|
||||||
for c in commands.iter() {
|
|
||||||
println!("{}", c);
|
|
||||||
}
|
|
||||||
let mut pos = 0;
|
let mut pos = 0;
|
||||||
let mut segments = Vec::new();
|
let mut segments = Vec::new();
|
||||||
/*let segments = vec![
|
|
||||||
vec![
|
|
||||||
Movement{ rotation: -1, distance: 8 },
|
|
||||||
Movement{ rotation: 1, distance: 12 },
|
|
||||||
Movement{ rotation: 1, distance: 12 },
|
|
||||||
Movement{ rotation: 1, distance: 10 },
|
|
||||||
],
|
|
||||||
vec![
|
|
||||||
Movement{ rotation: 1, distance: 10 },
|
|
||||||
Movement{ rotation: 1, distance: 12 },
|
|
||||||
Movement{ rotation: 1, distance: 10 },
|
|
||||||
],
|
|
||||||
vec![
|
|
||||||
Movement{ rotation: -1, distance: 10 },
|
|
||||||
Movement{ rotation: 1, distance: 10 },
|
|
||||||
Movement{ rotation: -1, distance: 6 },
|
|
||||||
]
|
|
||||||
];*/
|
|
||||||
while pos < commands.len() - 4 {
|
while pos < commands.len() - 4 {
|
||||||
if let Some((n, mov)) = (2..=4)
|
if let Some((n, mov)) = (2..=4)
|
||||||
.filter_map(|i| {
|
.filter_map(|i| {
|
||||||
|
@ -137,82 +90,43 @@ fn main() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.inspect(|_| println!("hit for {}", pos))
|
|
||||||
.max_by_key(|((x, y), _)| x * y)
|
.max_by_key(|((x, y), _)| x * y)
|
||||||
{
|
{
|
||||||
pos += n.0;
|
pos += n.0;
|
||||||
segments.push(mov.unwrap());
|
segments.push(mov.unwrap());
|
||||||
} else {
|
|
||||||
//panic!("nothing found at position {}", pos);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
let filtered: HashSet<_> = segments
|
let filtered: HashSet<_> = segments.clone().into_iter().collect();
|
||||||
.clone()
|
let mut filtered: Vec<_> = filtered.into_iter().zip(['A', 'B', 'C'].iter()).collect();
|
||||||
.into_iter()
|
|
||||||
/*
|
|
||||||
.map(|mut curr| {
|
|
||||||
for s in &segments {
|
|
||||||
if curr.len() <= s.len() {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
let index_after = curr.len() - s.len();
|
|
||||||
if s == &&curr[index_after..] {
|
|
||||||
curr = &curr[..index_after];
|
|
||||||
}
|
|
||||||
if s == &&curr[..index_after] {
|
|
||||||
curr = &curr[index_after..];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
curr
|
|
||||||
})
|
|
||||||
.filter(|s| !s.is_empty())
|
|
||||||
*/
|
|
||||||
.collect();
|
|
||||||
let mut filtered: Vec<_> = filtered
|
|
||||||
.into_iter()
|
|
||||||
.zip(['A', 'B', 'C'].iter())
|
|
||||||
.collect();
|
|
||||||
filtered.sort_by_key(|(_, c)| c.to_owned());
|
filtered.sort_by_key(|(_, c)| c.to_owned());
|
||||||
dbg!(&segments, segments.len());
|
|
||||||
dbg!(filtered.len());
|
|
||||||
println!(
|
|
||||||
"{}",
|
|
||||||
filtered
|
|
||||||
.iter()
|
|
||||||
.map(|s| s.0.iter().map(|m| m.to_string()).collect::<String>() + "\n")
|
|
||||||
.collect::<String>()
|
|
||||||
);
|
|
||||||
//let filtered = segments;
|
|
||||||
let mut instructions = Vec::new();
|
let mut instructions = Vec::new();
|
||||||
let mut pos = 0;
|
let mut pos = 0;
|
||||||
while pos < commands.len() {
|
while pos < commands.len() {
|
||||||
println!("searching");
|
|
||||||
for i in 1.. {
|
for i in 1.. {
|
||||||
if filtered.iter().any(|(c, _)| c == &&commands[pos..pos + i]) {
|
if filtered.iter().any(|(c, _)| c == &&commands[pos..pos + i]) {
|
||||||
instructions.push(&commands[pos..pos + i]);
|
instructions.push(&commands[pos..pos + i]);
|
||||||
pos += i;
|
pos += i;
|
||||||
println!("match for {}..{}", pos, i);
|
|
||||||
break;
|
break;
|
||||||
} else {
|
|
||||||
println!("no match for {}..{}", pos, i);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
fn dir(rotation: i8) -> char {
|
|
||||||
if rotation == 1 { 'R' } else { 'L' }
|
|
||||||
}
|
|
||||||
input[0] = 2;
|
input[0] = 2;
|
||||||
let raw_path = (instructions.iter().map(|i| filtered.iter().find(|(f, c)| i == f).unwrap().1).join(",")
|
let path: Vec<i64> = (instructions
|
||||||
|
.iter()
|
||||||
|
.map(|i| filtered.iter().find(|(f, _)| i == f).unwrap().1)
|
||||||
|
.join(",")
|
||||||
+ "\n"
|
+ "\n"
|
||||||
+ &filtered.into_iter().map(|(f, _)| f.iter().map(|m| format!("{},{}", dir(m.rotation), m.distance)).join(",")).join("\n")
|
+ &filtered
|
||||||
+ "\nn\n");
|
.into_iter()
|
||||||
let path: Vec<i64> = raw_path
|
.map(|(f, _)| f.iter().map(|m| m.to_string()).join(","))
|
||||||
.chars()
|
.join("\n")
|
||||||
.map(|c| c as i64)
|
+ "\nn\n")
|
||||||
.rev()
|
.chars()
|
||||||
.collect();
|
.map(|c| c as i64)
|
||||||
|
.rev()
|
||||||
|
.collect();
|
||||||
println!(
|
println!(
|
||||||
"Part 2: {:?}",
|
"Part 2: {:?}",
|
||||||
IntComputer::new(input, 0, path).get_all_outputs().pop()
|
IntComputer::new(input, 0, path).get_all_outputs().pop().unwrap()
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
|
@ -77,7 +77,7 @@ impl Position2D {
|
||||||
|
|
||||||
impl Direction {
|
impl Direction {
|
||||||
pub fn turn(&mut self, turn_value: i64) {
|
pub fn turn(&mut self, turn_value: i64) {
|
||||||
*self = *self + turn_value as i8;
|
*self += turn_value as i8;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user