mirror of
https://github.com/wezm/advent-of-code.git
synced 2024-12-18 10:19:55 +00:00
Add day 1 solution
This commit is contained in:
commit
a89cdfc860
6 changed files with 72 additions and 0 deletions
2
2017/day/1/.gitignore
vendored
Normal file
2
2017/day/1/.gitignore
vendored
Normal file
|
@ -0,0 +1,2 @@
|
|||
/target/
|
||||
**/*.rs.bk
|
4
2017/day/1/Cargo.lock
generated
Normal file
4
2017/day/1/Cargo.lock
generated
Normal file
|
@ -0,0 +1,4 @@
|
|||
[[package]]
|
||||
name = "day1"
|
||||
version = "0.1.0"
|
||||
|
6
2017/day/1/Cargo.toml
Normal file
6
2017/day/1/Cargo.toml
Normal file
|
@ -0,0 +1,6 @@
|
|||
[package]
|
||||
name = "day1"
|
||||
version = "0.1.0"
|
||||
authors = ["Wesley Moore <wes@wezm.net>"]
|
||||
|
||||
[dependencies]
|
1
2017/day/1/input
Normal file
1
2017/day/1/input
Normal file
|
@ -0,0 +1 @@
|
|||
878938232157342756754254716586975125394865297349321236586574662994429894259828536842781199252169182743449435231194436368218599463391544461745472922916562414854275449983442828344463893618282425242643322822916857935242141636187859919626885791572268272442711988367762865741341467274718149255173686839265874184176985561996454253165784192929453678326937728571781212155346592432874244741816166328693958529938367575669663228335566435273484331452883175981955679335327231995452231118936393192583338222595982522833468533262224874637449624644318418748617949417939228988293391941457722641936417456243894182668197174255786445994567477582715692336249243254711653529871336129825735249667425238573952339922948214218872417858525199642194588448543565474847272984232637466664695217176358283788781843171636841215675851778984619377575696447366844854289534215286959727688419731976631323833892247438149829975856161755122857643731945913335556288817112993911694972667656914238999291831997163412548977649491227219477796124134958527843213824792685117696631512141241496451845758655276186597724748432996276498527911292531185292149948139724345841584782352214921634858734671118495424143437282979243347831258285851259579133433182387444656386679831584933397915132785411686688447731696776459621924821667112751789884987883991845818513249994767543526169463766975791464756526911587399764736557959464923353896921342944821833991457125256329564489631352268722457628514564128231487382111682976886838192412996932924373337524262135399256658638418515239876732866596731888779532573243713128238419234963195589987539467221517535272384899524386267268959484881379944796392255419838743164714275463459351741296586465213689853743856518583451849661592844879264196761867481258778393623584884535246239794178981387632311238115362178576899121425428114696158652976277392224226268242332589546757477683398264294929442592131949398261884548427951472128841328376819241955153423452531538413492577262348369581399925647624623868299468436859667152463974949436359589931136236247929554899679139746162554183855278713574244211854227829969443151478986413333429144796664423754818256172862812877688675514142265239992529776262844329188218189254491238956497568
|
9
2017/day/1/problem.txt
Normal file
9
2017/day/1/problem.txt
Normal file
|
@ -0,0 +1,9 @@
|
|||
The captcha requires you to review a sequence of digits (your puzzle input) and find the sum of all digits that match the next digit in the list. The list is circular, so the digit after the last digit is the first digit in the list.
|
||||
|
||||
For example:
|
||||
|
||||
1122 produces a sum of 3 (1 + 2) because the first digit (1) matches the second digit and the third digit (2) matches the fourth digit.
|
||||
1111 produces 4 because each digit (all 1) matches the next.
|
||||
1234 produces 0 because no digit matches the next.
|
||||
91212129 produces 9 because the only digit that matches the next one is the last digit, 9.
|
||||
|
50
2017/day/1/src/main.rs
Normal file
50
2017/day/1/src/main.rs
Normal file
|
@ -0,0 +1,50 @@
|
|||
use std::io::Read;
|
||||
use std::fs::File;
|
||||
|
||||
fn main() {
|
||||
let mut input = Vec::new();
|
||||
let mut file = File::open("input").expect("unable to open input file");
|
||||
file.read_to_end(&mut input).expect("error reading input");
|
||||
|
||||
println!("{}", captcha(&input));
|
||||
}
|
||||
|
||||
fn captcha(input: &[u8]) -> u64 {
|
||||
let digits = input.iter()
|
||||
.take_while(|&&chr| chr >= 0x30 && chr <= 0x39)
|
||||
.collect::<Vec<_>>();
|
||||
|
||||
let mut sum = 0;
|
||||
for idx in 0..digits.len() {
|
||||
if digits[idx] == digits[(idx + 1) % digits.len()] {
|
||||
sum += *digits[idx] as u64 - 0x30;
|
||||
}
|
||||
}
|
||||
|
||||
sum
|
||||
}
|
||||
|
||||
// 1122 produces a sum of 3 (1 + 2) because the first digit (1) matches the second digit and the
|
||||
// third digit (2) matches the fourth digit.
|
||||
#[test]
|
||||
fn test_example1() {
|
||||
assert_eq!(captcha(b"1122"), 3);
|
||||
}
|
||||
|
||||
// 1111 produces 4 because each digit (all 1) matches the next.
|
||||
#[test]
|
||||
fn test_example2() {
|
||||
assert_eq!(captcha(b"1111"), 4);
|
||||
}
|
||||
|
||||
// 1234 produces 0 because no digit matches the next.
|
||||
#[test]
|
||||
fn test_example3() {
|
||||
assert_eq!(captcha(b"1234"), 0);
|
||||
}
|
||||
|
||||
// 91212129 produces 9 because the only digit that matches the next one is the last digit, 9.
|
||||
#[test]
|
||||
fn test_example4() {
|
||||
assert_eq!(captcha(b"91212129"), 9);
|
||||
}
|
Loading…
Reference in a new issue