Solved day 6 pt 1, I'll do pt 2 tomorrow
This commit is contained in:
130
input/day06.txt
Normal file
130
input/day06.txt
Normal file
@@ -0,0 +1,130 @@
|
||||
........#........................................#......#........#................................................................
|
||||
....................................#......#.....#............#.............#..........#..........................................
|
||||
......................#.......................................................#...................................................
|
||||
.......#..#..#....#...#...#....#..............#......#.......#...#................#.......#.......................................
|
||||
......................#....##...#.......#....#.......................................#....................#.......................
|
||||
...#............................#........................................#..........................#.....................#.......
|
||||
....................#............#...............#......#.........#...........#...................................................
|
||||
............................#......#...#................#.............#...........................................................
|
||||
.....#..#.........#....................#......................................................#........................#.........#
|
||||
.........#..##.#.........#.............................................#...........#........#....................##...............
|
||||
...............#....#.........................##......#.....................#..............................................#......
|
||||
..................##...................................#...........#........#....#.............#..................#........#.#....
|
||||
....................................#...................#..............................#............#.............................
|
||||
.........#.....#................#..........................................#...................................#..............#...
|
||||
...#....................#...................................#..##...................#.......#......................###.........#..
|
||||
....................#............#....#.##....#.........#......#...#........................#.......................#..........#..
|
||||
..............#...................................................................................#....................#..........
|
||||
.........#................#..............................#............................#...#.................#...............#.....
|
||||
............................................................................................#...#............................#....
|
||||
............#....................#................................#....#...............................#....#.....................
|
||||
........................................#.........#..................................................#..#..................#......
|
||||
.............#.#............................#..#.....#............................................#....#..........................
|
||||
................................................................................................#.........#..#..............#..#..
|
||||
...........#..........#.#..#................#.#..#...#.........#..........................................#..........#..#.........
|
||||
.............................#....................#.......#....#.....#....#......#....................#..#...............##.......
|
||||
..........#..............................................................................#....#.........#..#................#.....
|
||||
..#..#...............................................................#.......#........#...........................#...............
|
||||
...........................................................................#...##....................#.#....##....................
|
||||
.......................................#...............................................#.....................#.........#..........
|
||||
.......................................................#.......#....#................#.....................................#......
|
||||
.............#................#...................#.................#....................#..................#.#.........#.........
|
||||
.....................................................................#.....................................#......................
|
||||
........................#..........................................#....#..#.#..........................................#.........
|
||||
............#.......#..................#.................................................#..............#.......................#.
|
||||
.........................................................#...............#...#....#...........#.................................#.
|
||||
.........................#..........................#..#........................................................#.................
|
||||
...............#....................................#.......#......................................#............#.................
|
||||
.#......................................................................................................#.........................
|
||||
#...#........................................................#.................#....#....................#...........#............
|
||||
..#.........#................................................................#................................#.............#.....
|
||||
..................................#.........................................................................#................#....
|
||||
..............#..............................................#........#..................................#........................
|
||||
......#............................................#.................................#............................#...............
|
||||
.......##..#.......................##............#...#...................#.#..........................................##..........
|
||||
.#......#.....................................................................#..#..........................#......#.............#
|
||||
.................#.....................#........##..#.........#........#................#.........................................
|
||||
...........#.....#..........#........#............................................................................................
|
||||
.........................#......#.......................................#..............................#..........................
|
||||
............#............................................................#..............#..............................#..........
|
||||
..................#.........#...........................................................................................#.........
|
||||
#.#..................................#....................#......................#.............#.................................#
|
||||
....#................#.................#...................#...........#......................................#...................
|
||||
................#........................................................................................#....#.#.......#.....##..
|
||||
..........#...#.......................................#........#.......................................#...#......................
|
||||
.......#..##........................#......##.........................#.........#.......#.............................#.....#.....
|
||||
................#...............................................#....#..........#.....#.........#.........#.........#.............
|
||||
...............................#............#....................................#......#......................................#..
|
||||
.#..#..................#............................................#....#............#...............##...#..........#...........
|
||||
....#.............................................................................................................................
|
||||
.............................................................#...........................#..........#.............................
|
||||
.#........#..................#.....#.............#.....................................................#...#........#...........#.
|
||||
.......................#........................................#.....#............................#.#..................#.........
|
||||
................#....#................#.......#............................#.......#.................#............................
|
||||
....#.........#....#........#.....................#........................#............#.........................................
|
||||
.......#.......#.....................................................................##...........#...............................
|
||||
...........#.........................................................#..........#............#....................................
|
||||
..................#.............................#.......................................................................#.........
|
||||
................#.....#........#.....#...#..........#.....................................#.....#........................#........
|
||||
..........................................#.........#...........#.................................................................
|
||||
...#.......................................................................................................#......................
|
||||
....#..............#...........#..................................................#.................#.................#...........
|
||||
.#................#.....#.#.................................................................#.........................#...........
|
||||
............#.........................##....................................#..............#......................................
|
||||
...##...........#...#............#..........................................................................#.....................
|
||||
............................................#......................#......#.......................................................
|
||||
............#............................................................................................................#........
|
||||
...................##..............#.#....#.##...................................................#..................#.............
|
||||
..#...................................................................................#.........#.........................#.....#.
|
||||
........................#..............................................#......#................................................#..
|
||||
................#............#............................#.#...................#.....................#...........................
|
||||
..................#......#................#.#......................#...................#...#......................................
|
||||
..#................................................##...................................................................#.........
|
||||
...........................#......................................................................#......#...#......#.............
|
||||
........................#...#........#......#.......#..........#.............................#........#.....#.....................
|
||||
.................................................#..............................................#......#.....#....................
|
||||
.....#....#.................#......#........#.#..............^...........................#...................#..#.................
|
||||
.............................................................................#................................#...................
|
||||
#..........................#..#..............#.......#..........................#.................................................
|
||||
............#.............................................................................#...................#...................
|
||||
..................#.............................#.........................................#................#.........#......#.....
|
||||
...#...............................#.....#......#............###.#.#.....................................#....#.............#.....
|
||||
...........#...........#...........................#..............................................................................
|
||||
............#..........................................#.....#.............#..........................#....................#.....#
|
||||
........................#......#..#............................#.......................................................#..........
|
||||
..#...#...#.......#.#..........................................#.............#.........#....#..................#...........#......
|
||||
..................#.......#.....................................................................................#........#........
|
||||
......##................#...........................................#..............##.................................#...........
|
||||
.................#................................................................................#.#....................#........
|
||||
....................#.........#..........#...............#...#...#.#.#............................................................
|
||||
#..................#.#..........#..#...................................................................................#..........
|
||||
..........#........................................................#..........##..........................#..##...................
|
||||
...........#...................................................................#..................................................
|
||||
..................#........#............................................#..................#....#.......................#..#......
|
||||
............#...................#......#..........................................................................................
|
||||
...........................#.....##..........#.#..............#......................#.............#.......#...........#..........
|
||||
............#..................................................#.......#.........#.#..................#..............##...........
|
||||
#..................................................#...#......#..#......................#.............#........#............#.....
|
||||
....#..................#..........#.........#.........................................#..................#................#.......
|
||||
...#.................#.............................................................................#....................#.........
|
||||
..........#.................................................................#............#....................#.#.#.....#.........
|
||||
.....................#......................#...........#........#................................................................
|
||||
.....#.......................................................................#......................#...#......#..................
|
||||
.............##........#.....................#...........##........#............#.....#.....................#...............#.....
|
||||
.....#.........#....#.................#....#...........#.......##..........#.........#.#......................................#...
|
||||
.......#..................##.......#...#.#...#...................................#.......................................#.....#..
|
||||
...............#.......#.................#........................................................................................
|
||||
......#.......#.....#...............................#...........#.......#......................##...#....#........................
|
||||
.##..........................##..................##................#..#....#...##.................................................
|
||||
........#..............................#.#......#........#...............#....#........#.#........................................
|
||||
...............................#..#.....................#.#...................#...................................................
|
||||
.....................#.................................................##...#.......#.................##...............#.......#..
|
||||
.............#..........................#.................................#..............#.......#..........#........#............
|
||||
.........................................#...................#.........................................................#..........
|
||||
...............................#..........#..............................#............#.....#.....................................
|
||||
.............#..........................#....................#................................#...#............#..................
|
||||
....#......#........#.......#......#................................................#...#......................................##.
|
||||
..#...................................#........#.....................................#...#......#.........#..#..........#.........
|
||||
.......#.........#................................................................................................................
|
||||
...........#...............................##.........................................#..#....................#.....#.#.......##..
|
||||
.........................#..#...............#............................#.............#..........................#..............#
|
||||
10
input/day06_example.txt
Normal file
10
input/day06_example.txt
Normal file
@@ -0,0 +1,10 @@
|
||||
....#.....
|
||||
.........#
|
||||
..........
|
||||
..#.......
|
||||
.......#..
|
||||
..........
|
||||
.#..^.....
|
||||
........#.
|
||||
#.........
|
||||
......#...
|
||||
@@ -10,7 +10,7 @@ pub struct Day4 {
|
||||
impl Day4 {
|
||||
|
||||
pub fn create(input: String) -> Self {
|
||||
Day4 { puzzle: Grid::parse(input, '.')}
|
||||
Day4 { puzzle: Grid::parse(input, Some('.'))}
|
||||
}
|
||||
|
||||
fn check_with_step(&self, word: &str, start_x: usize, start_y: usize, step_x: isize, step_y: isize) -> bool {
|
||||
|
||||
78
src/day6.rs
Normal file
78
src/day6.rs
Normal file
@@ -0,0 +1,78 @@
|
||||
use std::collections::HashSet;
|
||||
use crate::day_solver::DaySolver;
|
||||
use crate::util::{Coord, Grid};
|
||||
#[cfg(test)]
|
||||
use crate::util::read_file;
|
||||
|
||||
pub struct Day6 {
|
||||
maze: Grid<char>
|
||||
}
|
||||
|
||||
const DIRECTIONS: [Coord<isize>; 4] = [
|
||||
Coord { x: 0, y: -1},
|
||||
Coord { x: 1, y: 0 },
|
||||
Coord { x:0, y: 1 },
|
||||
Coord { x: -1, y: 0 }
|
||||
];
|
||||
|
||||
impl Day6 {
|
||||
|
||||
pub fn create(input: String) -> Self {
|
||||
// let lines = input.lines();
|
||||
|
||||
// Put the input into the day struct
|
||||
Day6 {
|
||||
maze: Grid::parse(input, None)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl DaySolver for Day6 {
|
||||
|
||||
|
||||
fn solve_part1(&mut self) -> String {
|
||||
let mut cur_pos = self.maze.find(&'^').unwrap();
|
||||
let mut cur_direction_index = 0;
|
||||
let mut cur_direction = DIRECTIONS[cur_direction_index];
|
||||
let mut next_pos = cur_pos.add_signed(cur_direction);
|
||||
|
||||
let mut visited_positions = HashSet::new();
|
||||
|
||||
while self.maze.in_bounds(next_pos.x, next_pos.y) {
|
||||
|
||||
// println!("pos: {:?}", cur_pos);
|
||||
let c = self.maze.get(next_pos.x, next_pos.y);
|
||||
if c == &'#' {
|
||||
cur_direction_index = (cur_direction_index + 1) % DIRECTIONS.len();
|
||||
cur_direction = DIRECTIONS[cur_direction_index];
|
||||
next_pos = cur_pos.add_signed(cur_direction);
|
||||
} else {
|
||||
cur_pos = next_pos;
|
||||
visited_positions.insert(cur_pos);
|
||||
next_pos = cur_pos.add_signed(cur_direction);
|
||||
}
|
||||
}
|
||||
|
||||
visited_positions.len().to_string()
|
||||
}
|
||||
|
||||
fn solve_part2(&mut self) -> String {
|
||||
// Idea so far:
|
||||
// When walking the path as in part 1, for each position, check if the "next position" has been visited before, but while walking 1 rotation to the left
|
||||
// If that is the case, then if we block the next position, the guard would rotate to go back into his original path
|
||||
|
||||
return 0.to_string();
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_part1() {
|
||||
let mut day = Day6::create(read_file("input/day06_example.txt"));
|
||||
assert_eq!("41", day.solve_part1());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_part2() {
|
||||
let mut day = Day6::create(read_file("input/day06_example.txt"));
|
||||
assert_eq!("6", day.solve_part2());
|
||||
}
|
||||
@@ -6,6 +6,7 @@ use crate::day2::Day2;
|
||||
use crate::day3::Day3;
|
||||
use crate::day4::Day4;
|
||||
use crate::day5::Day5;
|
||||
use crate::day6::Day6;
|
||||
use crate::day_solver::DaySolver;
|
||||
use crate::util::read_file;
|
||||
|
||||
@@ -16,6 +17,7 @@ mod day2;
|
||||
mod day3;
|
||||
mod day4;
|
||||
mod day5;
|
||||
mod day6;
|
||||
|
||||
const DEFAULT_BENCHMARK_AMOUNT: u32 = 100;
|
||||
|
||||
@@ -92,7 +94,7 @@ fn build_day_solver(day: u8, input: String) -> Option<Box<dyn DaySolver>> {
|
||||
3 => Some(Box::new(Day3::create(input))),
|
||||
4 => Some(Box::new(Day4::create(input))),
|
||||
5 => Some(Box::new(Day5::create(input))),
|
||||
// 6 => Some(Box::new(Day6::create(input))),
|
||||
6 => Some(Box::new(Day6::create(input))),
|
||||
// 7 => Some(Box::new(Day7::create(input))),
|
||||
// 8 => Some(Box::new(Day8::create(input))),
|
||||
// 9 => Some(Box::new(Day9::create(input))),
|
||||
|
||||
10
src/util.rs
10
src/util.rs
@@ -118,9 +118,9 @@ impl <T: PartialEq> Grid<T> {
|
||||
}
|
||||
|
||||
impl Grid<char> {
|
||||
pub fn parse(input: String, default: char) -> Grid<char> {
|
||||
pub fn parse(input: String, default: Option<char>) -> Grid<char> {
|
||||
if input.is_empty() {
|
||||
return Grid { data: Vec::new(), width: 0, default: Some(default) }
|
||||
return Grid { data: Vec::new(), width: 0, default }
|
||||
}
|
||||
let width = input.lines().next().unwrap().len();
|
||||
assert!(input.lines().all(|l| l.len() == width), "All lines in the grid should be the same length");
|
||||
@@ -128,7 +128,7 @@ impl Grid<char> {
|
||||
Grid {
|
||||
data: input.lines().join("").chars().collect(),
|
||||
width,
|
||||
default: Some(default),
|
||||
default,
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -192,4 +192,8 @@ impl Coord<usize> {
|
||||
|
||||
res
|
||||
}
|
||||
|
||||
pub fn add_signed(&self, rhs: Coord<isize>) -> Coord<usize> {
|
||||
Coord::new(self.x.wrapping_add_signed(rhs.x), self.y.wrapping_add_signed(rhs.y))
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user