generated from janezicmatej/aoc-template
Compare commits
No commits in common. "0000030011b728f02b8fdfef0b824164ba4bd583" and "00000280ae944fddbd9e4eeae490b32e74cab22d" have entirely different histories.
0000030011
...
00000280ae
|
@ -1,10 +0,0 @@
|
|||
O....#....
|
||||
O.OO#....#
|
||||
.....##...
|
||||
OO.#O....O
|
||||
.O.....O#.
|
||||
O.#..O.#.#
|
||||
..O..#O..O
|
||||
.......O..
|
||||
#....###..
|
||||
#OO..#....
|
|
@ -1 +0,0 @@
|
|||
rn=1,cm-,qp=3,cm=2,qp-,pc=4,ot=9,ab=5,pc-,pc=6,ot=7
|
115
src/bin/14.rs
115
src/bin/14.rs
|
@ -1,115 +0,0 @@
|
|||
use std::{collections::HashMap, iter::once};
|
||||
|
||||
enum Tilt {
|
||||
North,
|
||||
West,
|
||||
South,
|
||||
East,
|
||||
}
|
||||
|
||||
fn get_load(floor: &[Vec<char>]) -> usize {
|
||||
floor
|
||||
.iter()
|
||||
.enumerate()
|
||||
.flat_map(|(i, x)| x.iter().map(move |y| (i, y)))
|
||||
.filter(|x| *x.1 == 'O')
|
||||
.map(|x| floor.len() - x.0)
|
||||
.sum()
|
||||
}
|
||||
|
||||
fn swap<T: Copy>(floor: &mut [Vec<T>], from: (usize, usize), to: (usize, usize)) {
|
||||
let a = floor[from.0][from.1];
|
||||
let b = floor[to.0][to.1];
|
||||
floor[from.0][from.1] = b;
|
||||
floor[to.0][to.1] = a;
|
||||
}
|
||||
|
||||
fn tilt(floor: &mut Vec<Vec<char>>, tilt: Tilt) {
|
||||
let (inner, outer) = match tilt {
|
||||
Tilt::North | Tilt::South => (floor[0].len(), floor.len()),
|
||||
Tilt::West | Tilt::East => (floor.len(), floor[0].len()),
|
||||
};
|
||||
let inx_n = |(i, j)| (j, i);
|
||||
let inx_s = |(i, j)| (inner - 1 - j, i);
|
||||
let inx_w = |(i, j)| (i, j);
|
||||
let inx_e = |(i, j)| (i, inner - 1 - j);
|
||||
for i in 0..outer {
|
||||
let mut ptr = 0;
|
||||
for j in 0..inner {
|
||||
let ((ii, jj), (pi, pj)) = match tilt {
|
||||
Tilt::North => (inx_n((i, j)), inx_n((i, ptr))),
|
||||
Tilt::South => (inx_s((i, j)), inx_s((i, ptr))),
|
||||
Tilt::East => (inx_e((i, j)), inx_e((i, ptr))),
|
||||
Tilt::West => (inx_w((i, j)), inx_w((i, ptr))),
|
||||
};
|
||||
match floor[ii][jj] {
|
||||
'O' => {
|
||||
swap(floor, (ii, jj), (pi, pj));
|
||||
ptr += 1;
|
||||
}
|
||||
'#' => ptr = j + 1,
|
||||
_ => (),
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn tilt_cycle(floor: &mut Vec<Vec<char>>) {
|
||||
use Tilt::*;
|
||||
tilt(floor, North);
|
||||
tilt(floor, West);
|
||||
tilt(floor, South);
|
||||
tilt(floor, East);
|
||||
}
|
||||
|
||||
pub fn part_one(input: &str) -> Option<usize> {
|
||||
let mut f: Vec<Vec<_>> = input.lines().map(|x| x.chars().collect()).collect();
|
||||
tilt(&mut f, Tilt::North);
|
||||
Some(get_load(&f))
|
||||
}
|
||||
|
||||
pub fn part_two(input: &str) -> Option<usize> {
|
||||
let mut f: Vec<Vec<_>> = input.lines().map(|x| x.chars().collect()).collect();
|
||||
let mut memo: HashMap<String, usize> = HashMap::new();
|
||||
|
||||
for i in 1.. {
|
||||
tilt_cycle(&mut f);
|
||||
let repr = f
|
||||
.iter()
|
||||
.flat_map(|x| x.iter().chain(once(&'\n')))
|
||||
.collect::<String>();
|
||||
|
||||
if let Some(ii) = memo.insert(repr, i) {
|
||||
let m = i - ii;
|
||||
let shift = (1_000_000_000 - ii) % m;
|
||||
|
||||
for _ in 0..shift {
|
||||
tilt_cycle(&mut f);
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
Some(get_load(&f))
|
||||
}
|
||||
|
||||
aoc::solution!(14);
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
#[test]
|
||||
fn test_part_one() {
|
||||
assert_eq!(
|
||||
part_one(&aoc::template::read_file("examples", 14)),
|
||||
Some(136)
|
||||
);
|
||||
}
|
||||
#[test]
|
||||
fn test_part_two() {
|
||||
assert_eq!(
|
||||
part_two(&aoc::template::read_file("examples", 14)),
|
||||
Some(64)
|
||||
);
|
||||
}
|
||||
}
|
|
@ -1,68 +0,0 @@
|
|||
fn hash(s: &str) -> usize {
|
||||
let mut total = 0;
|
||||
for c in s.chars().map(|x| x as usize) {
|
||||
total += c;
|
||||
total *= 17;
|
||||
total %= 256;
|
||||
}
|
||||
total
|
||||
}
|
||||
|
||||
pub fn part_one(input: &str) -> Option<usize> {
|
||||
Some(input.lines().flat_map(|x| x.split(',')).map(hash).sum())
|
||||
}
|
||||
|
||||
pub fn part_two(input: &str) -> Option<usize> {
|
||||
let mut map = vec![Vec::<(&str, usize)>::new(); 256];
|
||||
|
||||
for instruction in input.lines().flat_map(|x| x.split(',')) {
|
||||
let (label, n) = instruction.split_once(|x| x == '-' || x == '=').unwrap();
|
||||
let hash = hash(label);
|
||||
let indexed_map = map.get_mut(hash).unwrap();
|
||||
|
||||
if instruction.contains('-') {
|
||||
if let Some((i, _)) = indexed_map.iter().enumerate().find(|(_, &x)| x.0 == label) {
|
||||
indexed_map.remove(i);
|
||||
}
|
||||
} else {
|
||||
let nbr = n.parse().unwrap();
|
||||
if let Some((i, _)) = indexed_map.iter().enumerate().find(|(_, &x)| x.0 == label) {
|
||||
indexed_map[i] = (label, nbr);
|
||||
} else {
|
||||
indexed_map.push((label, nbr))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Some(
|
||||
map.into_iter()
|
||||
.enumerate()
|
||||
.flat_map(|(i, x)| {
|
||||
x.into_iter()
|
||||
.enumerate()
|
||||
.map(move |(j, y)| (i + 1) * (j + 1) * y.1)
|
||||
})
|
||||
.sum(),
|
||||
)
|
||||
}
|
||||
|
||||
aoc::solution!(15);
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
#[test]
|
||||
fn test_part_one() {
|
||||
assert_eq!(
|
||||
part_one(&aoc::template::read_file("examples", 15)),
|
||||
Some(1320)
|
||||
);
|
||||
}
|
||||
#[test]
|
||||
fn test_part_two() {
|
||||
assert_eq!(
|
||||
part_two(&aoc::template::read_file("examples", 15)),
|
||||
Some(145)
|
||||
);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue