Skip to content

Commit

Permalink
Add day 3
Browse files Browse the repository at this point in the history
  • Loading branch information
m1so committed Dec 3, 2020
1 parent f97bd4c commit d90853a
Show file tree
Hide file tree
Showing 6 changed files with 393 additions and 0 deletions.
1 change: 1 addition & 0 deletions day-03/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/target
5 changes: 5 additions & 0 deletions day-03/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions day-03/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[package]
name = "day-03"
version = "0.1.0"
authors = ["Michal Baumgartner <[email protected]>"]
edition = "2018"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
323 changes: 323 additions & 0 deletions day-03/input/input1.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,323 @@
.##.............##......#.....#
.#.#................#..........
...#..#.##..#.#......#.#.#.#..#
..#......#........#..#..#.#.#..
.......#....#..#..#.......#....
..#..#..##.#...#........#.###..
..#...#..#.....#.##....#.......
....#..###.#......#.##..#...##.
..#..........#.##.#...#........
#...#............##....#..##...
.......##....#.....##..#.#..#.#
..#..#..#...#....#....#....#...
.#...#.##........#####........#
..#..#......#.....##...#.......
....#......##....#.#....#.#..##
#.#.##....##..#.........#.###..
##..###..#..#.......###.......#
...#.#......#.........#....#...
.....#..........#.....##..#.#..
....##......#.#..#....#.#......
..#.....#..##.......##......#..
.........##.##.#..##...........
....#...#.....#....#.#.###....#
.##.#..#...##..#.......#......#
##..#..#..####..#.#..#...#.....
..###..#..#..#.###..#....#.##..
......#...###.#.#.....#........
.....#...#.#...#.......#.....#.
#........#..##...........#..#..
.#.##.##...#.....#.#....#..#...
..##.##....#.....#....#....##..
#.........##...##..#.....#..#..
........#.####....#...##.....#.
.#.#...#..#..#.#......##.....#.
..#..........##..#.#.#....#...#
#.......#...#...#.....#.##.#...
..#.....#..#.....####.#..#.#.##
...#.#..#...#.....#...#.#.#.#.#
.#..##....##.....#..#....###...
....#......##.#.#.....#......#.
..#.#...#......#.....##.......#
..#...###...#..#.#...#..#.....#
#..............#.....#....##..#
.#...#.......#.............#...
..###....#.##........#.#.......
#.##.......#..#............###.
#...#..##.#.#............######
..##..#....#.#.###...#..##.##..
.#...#.###.#....#...#....#...#.
#...#.......#...........#...##.
##.#......#####.............#..
....#..#......##..#..........#.
#.....#.....#.#.......#...#...#
....#...#.#..##........#.#..##.
..##.....##............#.#.###.
#.........#........#..###......
............#..................
.#.###...####...#.#..#......#..
...##.###.#....##.#..####..#.##
..#####.#.##...#.#...##.##....#
........##...#...#....##.....##
#...........###...#.#...##.#...
##......#...#.......###........
..#..#.##.#..###....#..#.###.#.
...#.#.#...#....#.##..#...#....
........#.##...##.#.....##...##
.#..........##..#..#..#.#...#..
#.#...#.##.#....#.##..#........
.#.#.#....##..##.####.....###..
..#....##....#..#..#..##.......
..##...#.......#...##.#....#...
...####.#.#...........#.#...#..
....####.....#.#.....#....##.##
..#.....#.#.............##....#
#.#....#####.##..####.#...#.#..
#.#....#.##.#.#.##..#.#...#....
......#.......#.......#.....#..
..#.....#....###.###..#..#..#..
#..#....##.###...##.....#......
..#..#...#..#.##..........#....
...#.#.#......#....#.##..#..##.
....##.#....#..#...##....###...
##.#.###.....#..#.#.#..#.....##
##..#.#........#...#..#.#......
....#.#.......##.#...........#.
.......##...#...#...#.....#....
.....#....#..#..###.#...#......
............#.#..#......#.#....
...##..#.##....##..##.#......#.
#.#.#......#.#.....#.#.#..#.#.#
...###..........#..#..#.##.....
......#......#......###..##....
#...##...#....#....#..#...#.#..
.......#..#......##.......#....
...#..#..#.....#.....#......##.
..#....###..........##..#...#..
..........#..#.#...#......#..#.
#...#....#.##.........#.#.#...#
.#.#.#...#.#...#.#..#..#....#.#
#.##....#..#.........#.##.##..#
..#.#..##.#....#.###.#...#....#
.#.......#...#.#.........#....#
.......#...#..........#.#..#...
...#.....##..#....#...###...#.#
#....##.##..........#.......#..
.##..##......#...#....#.##....#
....#.....##...##.#..#.........
...#.#..##.#.#..#.......#....#.
.#...#.#.#.#..#..#.##.......#..
..#..##...#.#..#.......#.#####.
.......#.#...........#....#.#..
.#.###..........#...#..#...#...
..#.#............##......##....
...##..#...###...##....#.#.##..
....#.##...#..#.#.#...........#
....#..#....##.....#.##.#.....#
..##......##.#.........#....#.#
###...#...#..#.#...#...........
.####.....#.....#.#....#..#....
.#....#..#..#..#...##.....###.#
#....##.#....#.##..#......##..#
.........#..#.#.....#.#....##.#
.....#.#...#....#.#...#....#..#
.#...#.#.....#.#......#.#......
#....##.......#.............#..
#..#...#........##..#..#......#
####..##.#..##..#.........####.
.#.##..#.#..#.#.#.##...#..#.#.#
.##.#..#............#......#...
###....#.##....#..#...#........
.....#..###..........#..#......
.##..##.....#..##....#...#.....
#...##...........#..#...###..#.
#..##....#...#.##.##....#......
...#...#..#.#.......##.......##
....#.....#..#...#.........#.#.
.#...##.#......#.#..#..#...##..
...##...##.##...##...#..#......
#..##.#..#..#............#...#.
..#.....#.........#........#.#.
#.#...#...#......#.#......#....
.##.....##.#.#....#.##...##.#..
.##..##.#.#....#.##............
.#.##.....##.#...#...###....#..
.#..............#.#....###.###.
....#..#...#.#.#..........#.#.#
.#.#..#.#.#...###..#...##......
.#.#.....###......#..........#.
........#.##...............#.#.
...#.#.#......#..#..##........#
..#.##......#.......#..#......#
...#...#...#...#..#..#........#
..#....#.....#....#..##........
.....#..#...##....#......##....
...##..##..#..........##....#.#
..#....#..#...#.##..#.....##...
###...#.#....#........#.......#
......#...#..#....###.........#
..###.#...#...#...#.#..###.#...
.##.#.......#.#..#..#......#.#.
...##...........#.#..#.#..#....
.......#.....####.#.....#...#.#
......##......##.#.#.#...#.#..#
..###.#####..#....#.#...#..##..
.....#..#......#........#......
#..##....#.#.##....#....#......
.#....#.##.####.##..#..#......#
#...##...#.#...##.#.##.##...#..
........#.#........#.#...#..#..
.#....###..#......#.##.###..#..
.#..#..#..#..#...#.#.........##
....#...#..#..............#....
........#...##.....#.......#...
..#......#.#..#.#..#.#.#...###.
....#...####....###....#......#
#...#.#...................#.##.
..#.#.###...#....##....##......
#..##..#.........#....#....####
.#....###...#.#...#......#...#.
......#..#.#..#.##...#.#.#..#..
.#...#.#.....#..##......#..#...
##.#..##.....##.#.#.......##...
.##.##.##..#...#.#.##.##.......
.#.#......#.....#...#.#..#.....
...#...........#..#.##..##..#..
.....#...##......#........#.#..
....#..............##.........#
..####.#....##..##......##.#.#.
.#.#..#...#..........#...###..#
....#.#.##.....###.#..#.##.....
.......##.#.#..#...#...##.#....
...#.##.....#....#...#...#.....
##.#.##..#..#.#.....#.#..#.....
..#..##........#....###..#..#..
..#.........##.....#......#...#
...##..........##......#.#.#...
#.....#..#..#......#......#....
.##...#..##....#.......##..#.#.
.#...##...##......####.##.#....
.....#.........#.#.####......#.
...#.....#.#.........##..#.....
##.#.###.#..#.#..#............#
...##..#.#....#....#..#........
..#.###......#...#.#.....#...#.
....##.##..#.....#...#.#.#....#
.......#.#..#...........#.#....
.#.#..##.#.......#.#..#.....###
...#.#.....#.#..#.##..#...#.#..
...#......##....#..............
......#...#....................
..#........#...##.##.#..#.#.#..
.#.###.#.##..##..#....##....#..
.....#..#.#...#.#..#..#.......#
..........#.##.#..##..####.....
............#.#......#.........
.#....#..#......#.....##.......
.....#........#.....##.#..#.#..
#..#.##...#.#.....#...#.####...
......#...#....#.##..##.#...#..
.#.#.##......##....#.#....#.##.
#.#.#....#.###....##....##.....
.##..#...#.##......#..#..#...##
...#....###....#...........#.#.
#.#.##.##...##....#....##.#...#
.#.#######.......#......#......
#......#...#.#.#.###....#.##..#
......##..#..##.........##.#.##
....##...#.#....##.....#.....#.
..#.#........##........#.#..##.
.....#..#.##.....#.....#..#.#..
.#..............#.......#......
.............#..#..........#...
.#..#.##....##.#..#...##.......
...........#..#.......#.#....#.
.#..#..........##...#.#.#...#..
......#....#..###....#......#..
.#...#...##..#..#..##..#..#.#..
#.#.........#....#..........##.
...##..#..##...#....##...##.##.
..#....#.####.........#.....##.
.....#.#...#.#...#.##.#...##..#
#...#.....#..#.......#...#..#..
..#.......#..##.#.....#....#...
.#.....#..##.#.....#...#.#...#.
.....#.##..........##....#...#.
...#....#...#........##...#...#
....##...#....#..........#.....
...#....##..#..####..##.#...#.#
#...###.###..#....##.#.........
.#.......#......#.........#....
..#..##..#.........##..........
#......#.#.##...#...#####......
......#.....####......#....#...
.........#..#..#...#....#.#....
....#........#...##....#.......
...##.#...#..........#....#....
........#.......#.##..#..#...#.
....#..##...........#.....#..#.
#....#...............#.#....#..
.#........#....#.#...#.......#.
#.......##..........#.......#..
...#....#...##.#..#.......#....
#..#.##...#.#...#...#...#....#.
###...#...#....#....#....#...#.
##......#.#.......#....#..#....
......#......#....#.#.#..###..#
.#.#.##.....#..#..........#....
##...#.#.#..##....#.....#.#....
#.##...#...#.#...####..#.......
.....##..#.#.#....#..##..#.#...
....###.#.........##.....#.....
......##...........#........#.#
.#.........##..................
.........##...#.............#.#
......##...#...#.........#..##.
#..#.......#..##.......###.....
....#.#.....#............##....
.....#..#......#....#.....##...
##......##...................#.
#....#............#.#.###.##...
.#.....#........#.....#...#....
......##.......######......##..
.#....##....#..###....#.......#
..............##.#..#.......#.#
.#..#..........#..#.##.........
......##.#..#......#.#....##.#.
#.....#.##...#.....#...#..#...#
.#....#..##.....#.....#.#.#....
..#......#.##..#.........#.#.#.
.#..##...#...#.....#..#..#.#..#
#.#.##.##.................#.#.#
.#..#.#..##.#.......#.......##.
#...#...#..##...#...##...#...#.
....#......#..#...#.....##..#..
..............##......#...#.#..
..##..#.......#..#..###.#.#....
.#..#..#...#.......#...#...##.#
.#...#.......###..#.##.###.....
##.#...#......#.....#..#.......
##....##............#.....#..#.
.....#...##......##.....#....##
#...##..#....#..##....###.#...#
.....#..#.#.....#.##..##....#..
.#.....#.#........#...#.#......
......#....#.#........#.#......
.##..#...............###...##.#
.......###.#.#......###.....#..
.......#..##...#....#.##..#.##.
..#.......##.......#.....#....#
.#......#....#..##..#.#.#..##..
###......#...#..#.............#
.#....#..#.#......##...........
.#....#.##.....#..#.......#..##
....#...#...#..#.....#..##..#.#
#.#.#.......##.#..#.#....#.....
##.#.......#...#...#.#......##.
#....#.#...........#######.....
...#.#.##.#......##..###.......
..#.#....#..#.................#
........#..##..#.....#....#.##.
...#.#..#..#..#..............##
.##.......###.#......#....#..##
..##.##.#......#....#..#...#..#
11 changes: 11 additions & 0 deletions day-03/input/input_sample.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
..##.........##.........##.........##.........##.........##.......
#...#...#..#...#...#..#...#...#..#...#...#..#...#...#..#...#...#..
.#....#..#..#....#..#..#....#..#..#....#..#..#....#..#..#....#..#.
..#.#...#.#..#.#...#.#..#.#...#.#..#.#...#.#..#.#...#.#..#.#...#.#
.#...##..#..#...##..#..#...##..#..#...##..#..#...##..#..#...##..#.
..#.##.......#.##.......#.##.......#.##.......#.##.......#.##.....
.#.#.#....#.#.#.#....#.#.#.#....#.#.#.#....#.#.#.#....#.#.#.#....#
.#........#.#........#.#........#.#........#.#........#.#........#
#.##...#...#.##...#...#.##...#...#.##...#...#.##...#...#.##...#...
#...##....##...##....##...##....##...##....##...##....##...##....#
.#..#...#.#.#..#...#.#.#..#...#.#.#..#...#.#.#..#...#.#.#..#...#.#
44 changes: 44 additions & 0 deletions day-03/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#![feature(bool_to_option)]

use std::{error::Error, fs::File, io::BufReader, iter, io::prelude::*};

fn main() -> Result<(), Box<dyn Error>> {
let file = File::open("input/input1.txt")?;
let reader = BufReader::new(file);
let tree_char = '#';

let tree_count = reader.lines().skip(1).enumerate().filter_map(|(idx, line)| {
let row = line.ok()?;
let offset = ((idx+1) * 3) % row.len();

(row.chars().nth(offset)? == tree_char).then_some(())
}).count();

println!("Part 1: {}", tree_count);

let file = File::open("input/input1.txt")?;
let reader = BufReader::new(file);

let offset_bases: Vec<(usize, usize)> = vec![(1, 1), (1, 3), (1, 5), (1, 7), (2, 1)];
let mut matches: Vec<usize> = iter::repeat(0).take(offset_bases.len()).collect();

for (idx, line) in reader.lines().skip(1).enumerate() {
let row = line?;
let row_len = row.len();
let current_row = idx + 1; // since we skip the first row

for (offset_idx, (row_peek, column_peek)) in offset_bases.iter().enumerate() {
if current_row % row_peek != 0 { continue; }
let offset = ((current_row / row_peek) * column_peek) % row_len;

if row.chars().nth(offset).unwrap() == tree_char {
matches[offset_idx] += 1;
}
}
}

println!("Part 2: {:#?}", matches);
println!("Part 2: {}", matches.iter().fold(1, |a, b| a * b));

Ok(())
}

0 comments on commit d90853a

Please sign in to comment.