more successes.
This commit is contained in:
@@ -5,3 +5,4 @@ edition = "2024"
|
||||
|
||||
[dependencies]
|
||||
core = { path = "../core" }
|
||||
chrono = "0.4"
|
||||
@@ -32,3 +32,4 @@ fn main() {
|
||||
}
|
||||
println!("Found {num_good} good boxes? / Checksum {}", num_two * num_three);
|
||||
}
|
||||
// Checksum 5434
|
||||
@@ -0,0 +1,111 @@
|
||||
use core::read_data;
|
||||
|
||||
pub const WORLD_WIDTH: u32 = 1000;
|
||||
pub const WORLD_HEIGHT: u32 = 1000;
|
||||
pub const WORLD_SIZE: u32 = WORLD_WIDTH * WORLD_HEIGHT;
|
||||
|
||||
#[derive(Debug)]
|
||||
struct Point {
|
||||
pub x: u32,
|
||||
pub y: u32
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
struct Region {
|
||||
pub elf: u32,
|
||||
pub start: Point,
|
||||
pub end: Point,
|
||||
pub size: Point
|
||||
}
|
||||
|
||||
impl Region {
|
||||
fn new_with_size(start: Point, size: Point) -> Region {
|
||||
Region {
|
||||
elf: 0,
|
||||
end: Point { x: start.x + size.x, y: start.y + size.y },
|
||||
start,
|
||||
size
|
||||
}
|
||||
}
|
||||
|
||||
// fn new_with_end(start: Point, end: Point) -> Region {
|
||||
//
|
||||
// }
|
||||
}
|
||||
|
||||
fn apply_region(to_claim: &Region, world: &mut [u32; WORLD_SIZE as usize]) {
|
||||
println!("Claiming from {}x{} to {}x{}",
|
||||
to_claim.start.x,
|
||||
to_claim.start.y,
|
||||
to_claim.end.x,
|
||||
to_claim.end.y
|
||||
);
|
||||
|
||||
for y in to_claim.start.y..to_claim.end.y {
|
||||
for x in to_claim.start.x..to_claim.end.x {
|
||||
let offset = y * WORLD_WIDTH + x;
|
||||
world[offset as usize] += 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/// convert a single line of input into a region claim.
|
||||
fn line_to_region(input: &str) -> Region {
|
||||
// [[#<elf> @ <start.x, start.y>: <width.x, width.y>
|
||||
let (elf, balance) = input.split_once("@").unwrap();
|
||||
let (_, elf_id) = elf.split_once('#').unwrap();
|
||||
let (start_point, size_point) = balance.split_once(": ").unwrap();
|
||||
let (start_x, start_y) = start_point.trim().split_once(',').unwrap();
|
||||
let (size_x, size_y) =size_point.trim().split_once('x').unwrap();
|
||||
let start = Point {
|
||||
x: start_x.parse().unwrap(),
|
||||
y: start_y.parse().unwrap()
|
||||
};
|
||||
let size = Point {
|
||||
x: size_x.parse().unwrap(),
|
||||
y: size_y.parse().unwrap()
|
||||
};
|
||||
let end = Point {
|
||||
x: start.x + size.x,
|
||||
y: start.y + size.y,
|
||||
};
|
||||
|
||||
println!("ELF: [[{elf_id}]] START_POINT: [[{start_x}x{start_y}]] SIZE: [[{size_x}x{size_y}]] END: [[{}x{}]]",
|
||||
end.x, end.y
|
||||
);
|
||||
|
||||
let mut working = Region {
|
||||
elf: elf_id.trim().parse().unwrap(),
|
||||
start,
|
||||
end,
|
||||
size,
|
||||
};
|
||||
println!("Decoded [[{input}]] to [[{working:?}]]");
|
||||
working
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let binding = read_data("2018_03_data.txt");
|
||||
let lines = binding.lines();
|
||||
let mut world = [0x00u32; WORLD_SIZE as usize];
|
||||
|
||||
for line in lines {
|
||||
apply_region(
|
||||
&line_to_region(
|
||||
line
|
||||
), &mut world
|
||||
);
|
||||
}
|
||||
// now count the areas with a value >=2
|
||||
let mut num_multiclaimed = 0;
|
||||
for offset in 0..WORLD_SIZE {
|
||||
if world[offset as usize] >= 2 {
|
||||
num_multiclaimed += 1;
|
||||
}
|
||||
}
|
||||
|
||||
println!("There are {num_multiclaimed} multi-claimed squares.");
|
||||
}
|
||||
|
||||
// 120419 too high
|
||||
@@ -0,0 +1,89 @@
|
||||
use std::collections::HashMap;
|
||||
use chrono::{NaiveDateTime, Timelike};
|
||||
use core::read_data;
|
||||
use crate::WorkEvent::*;
|
||||
|
||||
#[derive(Debug)]
|
||||
enum WorkEvent {
|
||||
// [1518-09-07 00:00] Guard #2383 begins shift
|
||||
StartShift(u32),
|
||||
// [1518-08-20 00:17] falls asleep
|
||||
FallAsleep,
|
||||
// [1518-05-09 00:58] wakes up
|
||||
WakeUp
|
||||
}
|
||||
|
||||
fn parse_line(input: &str) -> (WorkEvent, NaiveDateTime) {
|
||||
let (mut date, balance) = input.split_once("] ").unwrap();
|
||||
let mut event = FallAsleep;
|
||||
|
||||
if balance.contains('#') {
|
||||
let (_, guard_id_plus_junk) = balance.split_once("#").unwrap();
|
||||
let (guard_id, _) = guard_id_plus_junk.split_once(" ").unwrap();
|
||||
event = StartShift(guard_id.parse().unwrap());
|
||||
} else {
|
||||
if !balance.contains("falls") {
|
||||
event = WakeUp
|
||||
}
|
||||
}
|
||||
|
||||
let parsed = NaiveDateTime::parse_from_str(date.strip_prefix('[').unwrap(), "%Y-%m-%d %H:%M").unwrap();
|
||||
(event, parsed)
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let binding = read_data("2018_04_data.txt");
|
||||
let lines = binding.lines();
|
||||
|
||||
let mut worklog = HashMap::new();
|
||||
|
||||
for line in lines {
|
||||
// println!("-- LINE = __{line}__ --");
|
||||
let (event, when) = parse_line(line);
|
||||
worklog.insert(when, event);
|
||||
// println!("--");
|
||||
}
|
||||
println!("Loaded {} work events. Sorting...", worklog.len());
|
||||
let mut items: Vec<_> = worklog.iter().collect();
|
||||
items.sort_by(|a, b| a.0.cmp(& b.0));
|
||||
|
||||
let mut sleeping_guard = 0;
|
||||
let mut sleep_time = NaiveDateTime::default();
|
||||
let mut working_guard = 0;
|
||||
let mut guard_sleep_log = HashMap::new();
|
||||
let mut minute_sleep_log: HashMap<u32, HashMap<i32, i32>> = HashMap::new();
|
||||
for index in 0..=60 {
|
||||
minute_sleep_log.insert(index, HashMap::new());
|
||||
}
|
||||
|
||||
for (k, v) in items {
|
||||
println!("K: {k} V: {v:?}");
|
||||
match v {
|
||||
StartShift(guard_id) => {
|
||||
working_guard = *guard_id;
|
||||
// println!("Guard {guard_id} started at {k:?}");
|
||||
}
|
||||
FallAsleep => {
|
||||
// println!("Guard {working_guard} is now sleeping at {k:?}");
|
||||
sleeping_guard = working_guard;
|
||||
sleep_time = *k;
|
||||
}
|
||||
WakeUp => {
|
||||
let sleep_duration = k.signed_duration_since(sleep_time);
|
||||
// println!("Guard {sleeping_guard} is now waking up at {k:?}, was sleeping for {} minutes.", sleep_duration.num_minutes());
|
||||
*guard_sleep_log.entry(sleeping_guard).or_insert(0) += sleep_duration.num_minutes();
|
||||
sleeping_guard = 0;
|
||||
// println!("Sleep from {} to {} for {}", sleep_time.minute(), k.minute(), working_guard);
|
||||
for minute in sleep_time.minute()..=k.minute() {
|
||||
println!("Marking sleep at {minute} for {working_guard}");
|
||||
minute_sleep_log.entry(minute).or_insert(HashMap::from((working_guard, 1)));
|
||||
println!("MSL has {} entries.", minute_sleep_log.len());
|
||||
}
|
||||
}
|
||||
}
|
||||
// println!("--");
|
||||
}
|
||||
|
||||
println!("Guard Sleep Log:");
|
||||
println!("{guard_sleep_log:?}");
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
fn process(input: &str) -> (String, u32) {
|
||||
let num_changes = 0;
|
||||
let mut peekables = input.chars().peekable();
|
||||
while let Some(current_char) = peekables.next() {
|
||||
println!("Checking {current_char} and {}", peekables.next().unwrap());
|
||||
}
|
||||
|
||||
(String::new(), num_changes)
|
||||
}
|
||||
|
||||
fn process_recursively(input: &str) -> String {
|
||||
let mut working_str = input;
|
||||
println!("RECURSIVE START: {}", input.len());
|
||||
let mut need_again = true;
|
||||
while need_again {
|
||||
let (working, last_count) = process(working_str);
|
||||
if last_count > 0 { need_again = true; }
|
||||
let w2 = working.clone();
|
||||
working_str = w2.as_str();
|
||||
}
|
||||
"dabCBAcaDA".to_string()
|
||||
}
|
||||
|
||||
fn main() {
|
||||
assert_eq!(process_recursively("dabAcCaCBAcCcaDA"), "dabCBAcaDA");
|
||||
}
|
||||
Reference in New Issue
Block a user