2017: Day 01

This commit is contained in:
2017-12-01 08:30:05 +01:00
parent f16b76d841
commit f8873234a0
4 changed files with 74 additions and 0 deletions

4
2017/01/Cargo.lock generated Normal file
View File

@@ -0,0 +1,4 @@
[[package]]
name = "day-01"
version = "0.1.0"

6
2017/01/Cargo.toml Normal file
View File

@@ -0,0 +1,6 @@
[package]
name = "day-01"
version = "0.1.0"
authors = ["logaritmisk <anders.e.olsson@gmail.com>"]
[dependencies]

2
2017/01/input.txt Normal file
View File

@@ -0,0 +1,2 @@
3893445835429722678558456317563893861752455542588369533636585887178232467588827193173595918648538852463974393264428538856739259399322741844613957229674619566966921656443476317729968764183945899765294481327998956154956571467872487576314549468261122281384513266834769436913544431258253346374641589492728885222652146158261225296144835682556133922436438188211288458692217737145834468534829945993366314375465767468939773939978272968388546791547526366348163672162245585168892858977723516752284597322176349412485116173844733679871253985762643852151748396593275274582481295864991886985988427966155944392352248314629138972358467959614279553511247863869663526823326467571462371663396188951696286916979923587358992127741723727623235238531991996999181976664226274715591531566495345212849683589582225465555847312199122268773923175183128124556249916458878785361322713513153175157855597289482439449732469754748544437553251412476225415932478849961897299721228198262823515159848941742786272262236888514421279147329383465929358896761449135917829473321834267122759371247338155787774952626616791265889922959653887288735233291968146648533754958199821789499914763279869931218136266492627818972334549751282191883558361871277375851259751294611921756927694394977764633932938573132221389861617195291742156362494769521829599476753198422283287735888197584327719697758442462886311961723849326959213928195182293316227334998926839139915138472514686689887874559367524254175582135318545912361877139367538434683933333264146289842238921989275112323681356256979576948644489986951538689949884787173194457523474156229389465725473817651516136514446513436419126533875125645855223921197481833434658264655912731133356464193251635637423222227273192628825165993827511625956856754776849919858414375874943572889154281862749595896438581889424559988914658387293414662361364793844213298677236787998677166743945812899526292132465751582925131262933636228593134861363493849168168765261647652342891576445292462341171477487223253795935253493869317616741963486473

62
2017/01/src/main.rs Normal file
View File

@@ -0,0 +1,62 @@
use std::env;
use std::fs;
use std::io::Read;
fn captcha(input: &str, step: usize) -> u32 {
let digits = input
.chars()
.flat_map(|c| c.to_digit(10))
.collect::<Vec<_>>();
let len = digits.len();
digits
.iter()
.enumerate()
.filter(|&(i, digit)| *digit == digits[(i + step) % len])
.map(|(_, digit)| digit)
.sum::<u32>()
}
fn main() {
let file_name = env::args().nth(1).expect("path to input file");
let mut input = String::new();
fs::File::open(file_name)
.and_then(|mut file| file.read_to_string(&mut input))
.expect("faild to read file");
for line in input
.lines()
.map(|line| line.trim())
.filter(|line| !line.is_empty())
{
println!("part-01: {}", captcha(line, 1));
println!("part-02: {}", captcha(line, line.len() / 2));
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn part_1() {
assert_eq!(captcha("1122", 1), 3);
assert_eq!(captcha("1111", 1), 4);
assert_eq!(captcha("1234", 1), 0);
assert_eq!(captcha("91212129", 1), 9);
}
#[test]
fn part_2() {
assert_eq!(captcha("1212", 2), 6);
assert_eq!(captcha("1221", 2), 0);
assert_eq!(captcha("123425", 3), 4);
assert_eq!(captcha("123123", 3), 12);
assert_eq!(captcha("12131415", 4), 4);
}
}