-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
works on my input, whatever this awful problem statement is supposed to mean.
- Loading branch information
Showing
10 changed files
with
242 additions
and
99 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
............ | ||
........0... | ||
.....0...... | ||
.......0.... | ||
....0....... | ||
......A..... | ||
............ | ||
............ | ||
........A... | ||
.........A.. | ||
............ | ||
............ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
use rustc_hash::FxHashMap; | ||
|
||
use crate::util::{GridLoc, GridView}; | ||
|
||
fn disjoint_pairs<T>(slice: &[T]) -> impl Iterator<Item = [&T; 2]> { | ||
(0..slice.len()).flat_map(move |left| { | ||
(left + 1..slice.len()).map(move |right| [&slice[left], &slice[right]]) | ||
}) | ||
} | ||
|
||
pub fn p1_naive(input: String) -> u32 { | ||
let grid = GridView::new(input.into_bytes()); | ||
|
||
let mut freqs = FxHashMap::<u8, Vec<u32>>::default(); | ||
for (index, &ch) in grid.input.iter().enumerate() { | ||
if ch != b'.' && ch != b'\n' { | ||
freqs.entry(ch).or_default().push(index as u32); | ||
} | ||
} | ||
|
||
let mut matches = 0; | ||
for indices in freqs.values() { | ||
for pair_index in disjoint_pairs(indices) { | ||
let pair_loc = | ||
pair_index.map(|&index| grid.shape.index_to_loc(index as usize).unwrap()); | ||
let vector = pair_loc[1] - pair_loc[0]; | ||
|
||
for loc in [pair_loc[0].add(vector * -1, &grid), pair_loc[1].add(vector, &grid)] | ||
.into_iter() | ||
.flatten() | ||
{ | ||
let byte = grid.get(loc).unwrap(); | ||
if byte == b'.' { | ||
matches += 1; | ||
} | ||
} | ||
} | ||
} | ||
|
||
// turns out it is the number of location-frequency pairs not unique locations??? | ||
matches | ||
} | ||
|
||
pub fn p2_naive(input: String) -> u32 { | ||
let grid = GridView::new(input.into_bytes()); | ||
|
||
let mut freqs = FxHashMap::<u8, Vec<u32>>::default(); | ||
for (index, &ch) in grid.input.iter().enumerate() { | ||
if ch != b'.' && ch != b'\n' { | ||
freqs.entry(ch).or_default().push(index as u32); | ||
} | ||
} | ||
|
||
let mut output = vec![false; grid.input.len()]; | ||
|
||
for indices in freqs.values() { | ||
for pair_index in disjoint_pairs(indices) { | ||
let pair_loc = | ||
pair_index.map(|&index| grid.shape.index_to_loc(index as usize).unwrap()); | ||
let vector = pair_loc[1] - pair_loc[0]; | ||
|
||
let iter_map_fn_builder = { | ||
let shape = grid.shape; | ||
move |loc: GridLoc, factor: i32| move |step| loc.add(vector * step * factor, shape) | ||
}; | ||
|
||
for map_fn in | ||
[iter_map_fn_builder(pair_loc[0], -1), iter_map_fn_builder(pair_loc[1], 1)] | ||
{ | ||
let mut loc_iter = (0..).map(map_fn); | ||
|
||
while let Some(Some(loc)) = loc_iter.next() { | ||
output[grid.shape.loc_to_index(loc) as usize] = true; | ||
} | ||
} | ||
} | ||
} | ||
|
||
output.iter().map(|&b| if b { 1 } else { 0 }).sum() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,76 @@ | ||
use std::path::PathBuf; | ||
use std::{env, fs, io}; | ||
|
||
use anyhow::Context; | ||
use clap::{Parser, ValueEnum}; | ||
|
||
pub mod all; | ||
pub use all::{load_input, run, Args, JsonString, Mode, Parse}; | ||
pub use all::run; | ||
|
||
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, ValueEnum)] | ||
pub enum Mode { | ||
Sample, | ||
Private, | ||
} | ||
|
||
#[derive(Parser)] | ||
pub struct Args { | ||
mode: Mode, | ||
day: u32, | ||
part: u32, | ||
#[clap(default_value = "")] | ||
variant: String, | ||
} | ||
|
||
pub fn load_input(mode: Mode, day: u32) -> anyhow::Result<String> { | ||
let dir = env::var("CARGO_MANIFEST_DIR").context("need cargo run")?; | ||
let path = PathBuf::from(dir).join("input").join(format!( | ||
"d{day}.{}.input.txt", | ||
match mode { | ||
Mode::Sample => "sample", | ||
Mode::Private => "private", | ||
} | ||
)); | ||
|
||
if let Mode::Private = mode { | ||
let exists = | ||
fs::exists(&path).with_context(|| format!("test {} existence", path.display()))?; | ||
if !exists { | ||
eprintln!("Downloading day {day} input"); | ||
|
||
let session_cookie = env::var("AOC_SESSION") | ||
.context("private file missing and AOC_SESSION env var missing")?; | ||
|
||
let client = reqwest::blocking::Client::new(); | ||
let data = client | ||
.get(format!("https://adventofcode.com/2024/day/{day}/input")) | ||
.header("Cookie", format!("session={session_cookie}")) | ||
.send() | ||
.context("request aoc private input")?; | ||
let input = io::read_to_string(data).context("read aoc private input")?; | ||
fs::write(&path, &input).context("write aoc private input to cache")?; | ||
} | ||
} | ||
|
||
fs::read_to_string(&path).with_context(|| format!("read file {}", path.display())) | ||
} | ||
|
||
pub trait Parse: Clone { | ||
fn parse(input: &str) -> Self; | ||
} | ||
|
||
impl Parse for String { | ||
fn parse(input: &str) -> Self { input.to_string() } | ||
} | ||
|
||
#[derive(Clone)] | ||
pub struct JsonString(pub String); | ||
|
||
impl Parse for JsonString { | ||
fn parse(input: &str) -> Self { | ||
let value = simd_json::json!(input); | ||
Self(simd_json::to_string(&value).unwrap()) | ||
} | ||
} | ||
|
||
mod util; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
use clap::Parser; | ||
|
||
fn main() -> anyhow::Result<()> { | ||
let args = aoc2024::all::Args::parse(); | ||
let args = aoc2024::Args::parse(); | ||
aoc2024::run(args) | ||
} |
Oops, something went wrong.