Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions 2023/09/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/target
6 changes: 6 additions & 0 deletions 2023/09/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[package]
name = "y2023d09"
version = "0.1.0"
edition = "2024"

[dependencies]
3 changes: 3 additions & 0 deletions 2023/09/data/example1
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
0 3 6 9 12 15
1 3 6 10 15 21
10 13 16 21 30 45
200 changes: 200 additions & 0 deletions 2023/09/data/input

Large diffs are not rendered by default.

5 changes: 5 additions & 0 deletions 2023/09/src/data.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#[cfg(test)]
pub const EXAMPLE1: &'static str = include_str!("../data/example1");

#[allow(unused)]
pub const INPUT: &'static str = include_str!("../data/input");
55 changes: 55 additions & 0 deletions 2023/09/src/history.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
use std::{num::ParseIntError, str::FromStr};

#[derive(Debug)]
pub struct ParseError;

impl From<ParseIntError> for ParseError {
fn from(_value: ParseIntError) -> Self {
Self
}
}

#[derive(Debug)]
pub struct History(pub Box<[i64]>);

impl FromStr for History {
type Err = ParseError;

fn from_str(s: &str) -> Result<Self, Self::Err> {
Ok(Self(
s.split_ascii_whitespace()
.map(|s| s.parse())
.collect::<Result<_, _>>()?,
))
}
}

impl History {
pub fn next_value(&self) -> i64 {
if self.0.len() <= 1 {
return 0;
}
let diffs = History(self.0.windows(2).map(|s| s[1] - s[0]).collect());
self.0[self.0.len() - 1] + diffs.next_value()
}

pub fn prev_value(&self) -> i64 {
if self.0.len() <= 1 {
return 0;
}
let diffs = History(self.0.windows(2).map(|s| s[1] - s[0]).collect());
self.0[0] - diffs.prev_value()
}
}

#[test]
fn next_value() {
let h = History(Box::new([0, 3, 6, 9, 12, 15]));
assert_eq!(h.next_value(), 18);
}

#[test]
fn prev_value() {
let h = History(Box::new([10, 13, 16, 21, 30, 45]));
assert_eq!(h.prev_value(), 5);
}
11 changes: 11 additions & 0 deletions 2023/09/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
mod data;
mod history;
mod part1;
mod part2;
mod puzzle;

fn main() {
use data::INPUT;
println!("Part 1: {}", part1::run(INPUT));
println!("Part 2: {}", part2::run(INPUT));
}
17 changes: 17 additions & 0 deletions 2023/09/src/part1/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
use crate::puzzle::Puzzle;

pub fn run(input: &str) -> i64 {
let puzzle: Puzzle = input.parse().expect("parse failed");
puzzle.histories.iter().map(|h| h.next_value()).sum()
}

#[cfg(test)]
mod test {
use super::*;
use crate::data::EXAMPLE1;

#[test]
fn test1() {
assert_eq!(run(EXAMPLE1), 114);
}
}
21 changes: 21 additions & 0 deletions 2023/09/src/part2/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
use crate::puzzle::Puzzle;

pub fn run(input: &str) -> i64 {
let puzzle: Puzzle = input.parse().expect("parse failed");
puzzle
.histories
.iter()
.map(|h| h.prev_value())
.sum()
}

#[cfg(test)]
mod test {
use super::*;
use crate::data::EXAMPLE1;

#[test]
fn test1() {
assert_eq!(run(EXAMPLE1), 2);
}
}
27 changes: 27 additions & 0 deletions 2023/09/src/puzzle.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
use std::str::FromStr;

use crate::history::History;

#[derive(Debug)]
pub struct ParseError;

impl From<<History as FromStr>::Err> for ParseError {
fn from(_value: <History as FromStr>::Err) -> Self {
Self
}
}

#[derive(Debug)]
pub struct Puzzle {
pub histories: Box<[History]>,
}

impl FromStr for Puzzle {
type Err = ParseError;

fn from_str(s: &str) -> Result<Self, Self::Err> {
Ok(Self {
histories: s.lines().map(|s| s.parse()).collect::<Result<_, _>>()?,
})
}
}
1 change: 1 addition & 0 deletions 2023/10/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/target
10 changes: 10 additions & 0 deletions 2023/10/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[package]
name = "y2023d10"
version = "0.1.0"
edition = "2024"

[dependencies]
anyhow = "1.0.99"
game-grid.workspace = true
thiserror = "2.0.15"
direction.workspace = true
5 changes: 5 additions & 0 deletions 2023/10/data/example1
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
.....
.S-7.
.|.|.
.L-J.
.....
5 changes: 5 additions & 0 deletions 2023/10/data/example2
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
..F7.
.FJ|.
SJ.L7
|F--J
LJ...
9 changes: 9 additions & 0 deletions 2023/10/data/example3
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
...........
.S-------7.
.|F-----7|.
.||.....||.
.||.....||.
.|L-7.F-J|.
.|..|.|..|.
.L--J.L--J.
...........
10 changes: 10 additions & 0 deletions 2023/10/data/example4
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
.F----7F7F7F7F-7....
.|F--7||||||||FJ....
.||.FJ||||||||L7....
FJL7L7LJLJ||LJ.L-7..
L--J.L7...LJS7F-7L7.
....F-J..F7FJ|L7L7L7
....L7.F7||L7|.L7L7|
.....|FJLJ|FJ|F7|.LJ
....FJL-7.||.||||...
....L---J.LJ.LJLJ...
10 changes: 10 additions & 0 deletions 2023/10/data/example5
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
FF7FSF7F7F7F7F7F---7
L|LJ||||||||||||F--J
FL-7LJLJ||||||LJL-77
F--JF--7||LJLJ7F7FJ-
L---JF-JLJ.||-FJLJJ7
|F|F-JF---7F7-L7L|7|
|FFJF7L7F-JF7|JL---7
7-L-JL7||F7|L7F-7F7|
L.L7LFJ|||||FJL7||LJ
L7JLJL-JLJLJL--JLJ.L
140 changes: 140 additions & 0 deletions 2023/10/data/input

Large diffs are not rendered by default.

17 changes: 17 additions & 0 deletions 2023/10/src/data.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#[cfg(test)]
pub const EXAMPLE1: &'static str = include_str!("../data/example1");

#[cfg(test)]
pub const EXAMPLE2: &'static str = include_str!("../data/example2");

#[cfg(test)]
pub const EXAMPLE3: &'static str = include_str!("../data/example3");

#[cfg(test)]
pub const EXAMPLE4: &'static str = include_str!("../data/example4");

#[cfg(test)]
pub const EXAMPLE5: &'static str = include_str!("../data/example5");

#[allow(unused)]
pub const INPUT: &'static str = include_str!("../data/input");
12 changes: 12 additions & 0 deletions 2023/10/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
mod data;
mod part1;
mod part2;
mod pipe;
mod position;
mod puzzle;

fn main() {
use data::INPUT;
println!("Part 1: {}", part1::run(INPUT));
println!("Part 2: {}", part2::run(INPUT));
}
23 changes: 23 additions & 0 deletions 2023/10/src/part1/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
use crate::*;
use puzzle::Puzzle;

pub fn run(input: &str) -> usize {
let puzzle: Puzzle = input.parse().expect("parse failed");
puzzle.iter().count() / 2
}

#[cfg(test)]
mod test {
use super::*;
use crate::data;

#[test]
fn test1() {
assert_eq!(run(data::EXAMPLE1), 4);
}

#[test]
fn test2() {
assert_eq!(run(data::EXAMPLE2), 8);
}
}
96 changes: 96 additions & 0 deletions 2023/10/src/part2/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
use direction::Direction;

use crate::position::Position;
use crate::puzzle::Puzzle;

fn shoelace(mut iter: impl Iterator<Item = Position>) -> i32 {
let start = iter.next().expect("shoelace: iter must not be empty");
let mut prev = start;
let mut area = 0i32;
for pos in iter {
area += prev.x * pos.y - pos.x * prev.y;
prev = pos;
}
area += prev.x * start.y - start.x * prev.y;
area / 2
}

fn left_offset(r#in: Direction, out: Direction) -> (i32, i32) {
use Direction::*;
match (r#in, out) {
(N, E) | (E, N) => (-1, -1),
(N, W) | (W, N) => (-1, 1),
(S, E) | (E, S) => (1, -1),
(S, W) | (W, S) => (1, 1),
(N, N) => (-1, 0),
(S, S) => (1, 0),
(E, E) => (0, -1),
(W, W) => (0, 1),
_ => unreachable!(),
}
}

fn left_area(puzzle: &Puzzle) -> i32 {
shoelace(puzzle.iter().map(|state| {
state.position * 2
+ (1, 1)
+ left_offset(
state
.pipe
.next_direction(state.direction.reverse())
.expect("bad pipe / direction combo")
.reverse(),
state.direction,
)
})) / 4
}

fn right_area(puzzle: &Puzzle) -> i32 {
shoelace(puzzle.iter().map(|state| {
state.position * 2 + (1, 1)
- left_offset(
state
.pipe
.next_direction(state.direction.reverse())
.expect("bad pipe / direction combo")
.reverse(),
state.direction,
)
})) / 4
}

pub fn run(input: &str) -> usize {
let puzzle: Puzzle = input.parse().expect("parse failed");
left_area(&puzzle)
.abs()
.min(right_area(&puzzle).abs())
.try_into()
.expect("abs() returned a negative number")
}

#[cfg(test)]
mod test {
use super::*;
use crate::data;

#[test]
fn test1() {
assert_eq!(run(data::EXAMPLE1), 1);
}
#[test]
fn test2() {
assert_eq!(run(data::EXAMPLE2), 1);
}
#[test]
fn test3() {
assert_eq!(run(data::EXAMPLE3), 4);
}
#[test]
fn test4() {
assert_eq!(run(data::EXAMPLE4), 8);
}
#[test]
fn test5() {
assert_eq!(run(data::EXAMPLE5), 10);
}
}
Loading