diff --git a/2023/inputs/day16 b/2023/inputs/day16 new file mode 100644 index 0000000..cf39e68 --- /dev/null +++ b/2023/inputs/day16 @@ -0,0 +1,110 @@ +\.....................\.......................|..../......-.........../.|.....\.............|..........|..\... +.../...................\..................../...|.......|........||...................../...........\......... +././....|......./................\............|..\...........-....../............../|........-................ +....\..\...........-/..|......|......\............|............../.................-........./...-...|.......\ +..................................|.\.......|...../.........................-...../......\..|...............\. +.....\.............................................|../.\............../--.......................\............ +|.......-.|........\.................\..............|........./....\.........|...................\-.-...|..... +....\........./....-.........................--.....\..../......................|.\......................\...- +\..-..../.....|..........\......................-|......|...................../...../........./...........-... +........................\.\|......../........../......................\.............|./../.................... +.....\...................|.............................|......-........|.........-./.../.|.................... +..-................./.........................................\..\........-..-........-.......\../............ +-................-............................\................./..--................-............../.|....... +..-.....|.............|................./.......-.-........................-............-...........\......... +..........|..........................-...|..\...............................................\-....\........... +/................../..........|....\..-......................../....\...\..........-....../.........../.....\. +................\....-................-|.\...-...........-.....-........\..........................\.......... +...................................\../..|.../../...........................\.........../.\-......|.........-. +/....|....|..........................\............./.....|/................./....-......................-..... +....-.............../.....|...........|../......\../.............../........................|............./... +......./..............|./../.................-.\.........../.....................|.........../........\....... +...................-...............\/.......|........|....................../.......................-/......\. +................\...../....-/......-.........-.....\/.\...............\....../...-....|......|.........\...... +....-.........-.........................-......................../.......|...............................\.... +......../..................|...../......\..\..-|-..../..............|......../............|.-.......-......... +......./............-..........-............................../.........................|\..-..-\.\......-..\. +.\-.....|......................-..\................/.-................|.\..............\..................|... +-..........................-............../.|.-......\......-..-...../......./................................ +....|.................................................\....|..........\...................-......|........./.. +................../...........-..............\......................................|.-........|.............. +...-............|..............-...........-.\.......................|..|..../......................../.|....- +.......-..-......./...../.........-.......\.....-.../............-./......../.../............-.....|...|...... +...............|.........../..........|/..........|..........-.-...../...../..|..........\.....-./.|./....|... +.........|......................\.|.....|......................-.-........|.............................-...|. +.-...|..\...-...............\..............\......./........-............/.-..\........../...../....-....\.... +...........\.-.......|.......|....-.............|-.|........../..../...\....\............|.|..|\.............. +.....................|.\....|......|../...\......|........................\......|.......-./..../............. +......................\--................................\.................|................-................. +-...............\...-.....................................-.\...../....\...........................|.......... +.....|.......\.......................-/...\..-.........-..../\|..................\........-................... +..|........./....|..-./..\..\................-|..........\../......\...............\............\............| +......-................../.............|.............\................................-..\...../........-..... +....................../......../..\.-.........................|....\..|...\...../../.....|............../..... +...\................................/..............................\........|.........\.-............\........ +/.................../................-...........-.|.............\.................................|.....-.... +.|...-..|/./../.........-./.............................................../..........-.........-.|./.......... +..../..................|......................|../..........\........-......../.........\...|................. +........./..................|..-..........|\............\......-..-..\...|....|.....................|....-.... +.....|/....|........\.........-................../..........|............|...........-.....|......./......//.. +..........\.|../...|........................\.........\.|......||....-...............\\.....|.|..............| +............-................|.....-....||......../.../.......................|..\...........\................ +...............-..../......-..../.......|-.....-.....\..................................../..|................ +/......-...............\/...././..................\..................\..........-......................|...... +...................\.....\.................\...|.................-............/........-..............\./..... +.-.\....\........\........-../.-............../...../..-..|...................../.-.....\.....-........../..\. +|...|...............................|............../.............../....................|...../............... +....../....../...|..............-.-........-..|.../....................\|.....\......................|....../. +...\\.......|...|-............|..........-...........................................\/...../..-.-............ +.........|.............|..|......\............\...............|......\......\.............\...\-.......\.....\ +...........\..-.|.......|\..\..................-........\/...............................-.................... +...\.................\..............................................\...............\.......-.../............. +..\.............\....---....................//.......................................|./.............../\..... +.\...........|..................................\........-......|.........\.................-........-........ +..............-...............-.../....-........|.....-../......................./..../........../../--..\.... +.........-......\......-.........../...\./.|.................\...................--..-........|..\......\....| +....../.........................................|...../........../..............\\...-/.......-......\..\..... +.........../...\.|..................../.\.................|......\.\..|.........../...../.....\.\...........|. +..../\..-...../.............................../..../............................-...-......\...........\...... +..-..\.............................\...........|..........|.........|..........................-......./...... +-..|.................-\.....\...............\/.....|...........................././...\......\.......\...-.... +......\..............|..|..............-............../....................-....\\........\....|.............. +|.............\....\./.....-|-....\.....\..............\/........-.|..|...........-..\......-..../.......|.... +............................-.......................|........................../................|............. +/................|../............\/.....\/....-.....\......\...../|..\..........|............../.............. +.|....../...../....................../..|.....................-................./...../-.........-............ +......................\|........../......./..\........\.........-......../.........../.|/.......\.../......... +...............\......-|............\..\....../.........\.....|...|\............/............\.\..|.......\... +......|...........\....../....\./......./-....\../.......\...-...............\./...\.....-.......-..../....... +...|..|.............................\.....-.|........./......../......................-......./............\.. +.....|........\.........|...-.-.........\..../....../..-.......-...............\..../.....-..../.............. +........./.\/........................-..........\.....................|.......-..............\./.\............ +..../................/................................|.....||..............\........-...............|........ +....\./.........../\-.....-...\............//.\..............\...../................|.....\..-................ +..../|........./............|.....................|......................-..|...............................\. +..................\............................\.......\.........-.............|...../.-.............-......|. +..............|.-...........................\.\./.......\........../...\................/....-................ +.......|................/-............\............/...........-..../...........\......../...\.......\........ +..............|..../......................-...........-..\.................................................-.. +..............................-..-................-.......-.................-..........\.../...............|.. +......-.........-........................./.....................\....|........................................ +.....\........./.....-.................../................|...................|........................|...... +........../.../........................./.|................-...........\...........-....|..................... +.....-.......\././/.......|..............|......../..\...........\.............................-......|../-.-. +........|-....-....................\.....|\...\/.....\........\......./..........|.\-..|...................... +../...-...-...|...../............\.......................-..........\...\.....-...\...............|....../...| +............|..\....|..................\/...................../../....|...................|.............../... +.............-................./............|.......|............\....................................\.-..... +-...../|..............-......\..../..................|..\....-............................................\... +........|.........-........\................/....\..................../.|...................................|. +.....|................./.........-|\...-...................|....-................-......../...............|..- +.|.........|...........|...-.............................\............................|................\...... +....|.....-......|......./.-.|...........\..-........|.........................-....-..-.....|................ +........................................\.....................-.../........................../../..........|.. +-................/....\.......//....-........................../.\.........-......./..|\..........\........... +-............................./......................................../-...........................\|........ +........................................../-.....//......\......\...........\........|./....../.......--...... +../..............-....../......../................../......./....-...........|../.......\.\.......-......./..\ +..............-............../......|.........-...............|................................-..........\... +.....................\..........|..\......|...............................................-................... +...../..|.../.........|.../.......-...................................\.....................................|. diff --git a/2023/setup_day.sh b/2023/setup_day.sh index 9e7b0fb..71d5ad8 100755 --- a/2023/setup_day.sh +++ b/2023/setup_day.sh @@ -1,7 +1,7 @@ #!/bin/sh today=$(date +%d) -aocd > inputs/day$today +aocd "$today" > inputs/day$today echo '#![feature(test)] extern crate test; diff --git a/2023/src/bin/day16.rs b/2023/src/bin/day16.rs new file mode 100644 index 0000000..a0c283c --- /dev/null +++ b/2023/src/bin/day16.rs @@ -0,0 +1,117 @@ +#![feature(test, try_blocks)] +extern crate test; +use aoc2023::{ + boilerplate, + common::*, + direction::Direction::{self, *}, +}; +use fnv::FnvHashSet; +use std::mem::transmute; +use Tile::*; + +const DAY: usize = 16; +type Parsed<'a> = Vec<&'a [Tile]>; + +#[repr(u8)] +#[allow(dead_code)] +#[derive(Debug, PartialEq, Eq)] +enum Tile { + Empty = b'.', + HSplit = b'|', + VSplit = b'-', + Angle1 = b'/', + Angle2 = b'\\', +} + +fn parse_input(raw: &str) -> Parsed { + raw.lines().map(|l| unsafe { transmute(l) }).collect() +} + +fn part1(grid: &Parsed) -> usize { + let mut points = FnvHashSet::default(); + points.insert((0, 0)); + let mut known = FnvHashSet::default(); + // my real input starts with a mirror, + // and I don’t want to rewrite the code to not blindly make the first step. + energized(grid, &mut points, &mut known, 0, 0, if grid[0][0] == Empty { Right } else { Down }); + points.len() +} + +fn angle1(dir: Direction) -> Direction { + match dir { + Up => Right, + Right => Up, + Down => Left, + Left => Down, + } +} + +fn angle2(dir: Direction) -> Direction { + match dir { + Up => Left, + Left => Up, + Down => Right, + Right => Down, + } +} + +fn energized( + grid: &Parsed, + points: &mut FnvHashSet<(usize, usize)>, + known: &mut FnvHashSet<(usize, usize, Direction)>, + mut x: usize, + mut y: usize, + dir: Direction, +) { + if !known.insert((x, y, dir)) { + return; + } + if let Some(tile) = try { + y = y.checked_sub((dir == Up) as usize)? + (dir == Down) as usize; + x = x.checked_sub((dir == Left) as usize)? + (dir == Right) as usize; + grid.get(y)?.get(x)? + } { + points.insert((x, y)); + match (tile, dir) { + (Empty, _) => { + energized(grid, points, known, x, y, dir); + } + (Angle1, _) => energized(grid, points, known, x, y, angle1(dir)), + (Angle2, _) => energized(grid, points, known, x, y, angle2(dir)), + (HSplit, Up | Down) => energized(grid, points, known, x, y, dir), + (HSplit, Left | Right) => { + energized(grid, points, known, x, y, Up); + energized(grid, points, known, x, y, Down); + } + (VSplit, Up | Down) => { + energized(grid, points, known, x, y, Right); + energized(grid, points, known, x, y, Left); + } + (VSplit, Left | Right) => energized(grid, points, known, x, y, dir), + } + } +} + +fn part2(parsed: &Parsed) -> usize { + unimplemented!() +} + +boilerplate! { + TEST_INPUT == r".|...\.... +|.-.\..... +.....|-... +........|. +.......... +.........\ +..../.\\.. +.-.-/..|.. +.|....-|.\ +..//.|...." + for tests: { + part1: { TEST_INPUT => 46 }, + part2: { TEST_INPUT => 0 }, + }, + bench1 == 7517, + bench2 == 0, + bench_parse: Vec::len => 110, +} diff --git a/2023/src/common.rs b/2023/src/common.rs index b5d1ce8..88f2f07 100644 --- a/2023/src/common.rs +++ b/2023/src/common.rs @@ -3,7 +3,7 @@ use std::{ fmt::Display, iter::Step, ops::{Add, Mul}, - str::FromStr, + str::FromStr, mem::transmute, }; pub fn read_file(day: usize) -> String { diff --git a/2023/src/direction.rs b/2023/src/direction.rs index f8a7199..26f484e 100644 --- a/2023/src/direction.rs +++ b/2023/src/direction.rs @@ -7,7 +7,7 @@ use std::{ pub const ALL_DIRECTIONS: [Direction; 4] = [Direction::Up, Direction::Down, Direction::Left, Direction::Right]; #[repr(u8)] -#[derive(Clone, Copy, Debug, PartialEq, Eq)] +#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] pub enum Direction { Right = 0, Down = 1,