aoc-2019-c

git clone https://git.sinitax.com/sinitax/aoc-2019-c
Log | Files | Refs | README | sfeed.txt

commit 3ef16bed60d82e4d87ba93d90c891615d4078d87
parent a9998acb73c9d2a482d00643687fd3aae187c57c
Author: Louis Burda <quent.burda@gmail.com>
Date:   Tue,  2 Nov 2021 18:01:48 +0100

Add day 4

Diffstat:
Msrc/day4/main.c | 94+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++--
Asrc/day4/part2 | 22++++++++++++++++++++++
Asrc/day4/test.input | 1+
3 files changed, 115 insertions(+), 2 deletions(-)

diff --git a/src/day4/main.c b/src/day4/main.c @@ -3,14 +3,104 @@ #include <stdlib.h> #include <stdio.h> +int +check_pass_1(int *state) +{ + int i; + + for (i = 0; i < 5; i++) + if (state[i] == state[i+1]) + break; + + if (i != 5) debug("%i %i %i %i %i %i\n", state[0], state[1], + state[2], state[3], state[4], state[5]); + + return i != 5; +} + +int +check_pass_2(int *state) +{ + int i; + + for (i = 0; i < 5; i++) { + if (state[i] == state[i+1] && (i == 0 || state[i] != state[i-1]) + && (i == 4 || state[i] != state[i+2])) + break; + } + + if (i != 5) debug("%i %i %i %i %i %i\n", state[0], state[1], + state[2], state[3], state[4], state[5]); + + return i != 5; +} + void -part1() +count_valid(int *state, int *end, int pinit, int pend, + int index, int *count, int (*check_pass)(int*)) { + int i; + + if (index == 6) { + *count += check_pass(state); + return; + } + + if (index && pinit && state[index-1] > state[index]) { + pinit = 0; + state[index] = state[index-1]; + } else if (index && !pinit) { + state[index] = state[index-1]; + } + + for (i = state[index]; i <= (pend ? end[index] : 9); i++) { + state[index] = i; + count_valid(state, end, pinit, pend && (i == end[index]), + index+1, count, check_pass); + } + state[index] = 0; } void -part2() +read_state(char *str, int *state) { + int i; + + for (i = 0; i < 6; i++) + state[i] = str[i] - '0'; +} + +void +part1(void) +{ + int start[6], end[6]; + char *tok; + int count; + + tok = CHKP(strchr(aoc.input, '-')); + read_state(aoc.input, start); + read_state(tok+1, end); + + count = 0; + count_valid(start, end, 1, 1, 0, &count, check_pass_1); + + aoc.answer = aprintf("%i", count); +} + +void +part2(void) +{ + int start[6], end[6]; + char *tok; + int count; + + tok = CHKP(strchr(aoc.input, '-')); + read_state(aoc.input, start); + read_state(tok+1, end); + + count = 0; + count_valid(start, end, 1, 1, 0, &count, check_pass_2); + aoc.answer = aprintf("%i", count); } diff --git a/src/day4/part2 b/src/day4/part2 @@ -0,0 +1,22 @@ +--- Part Two --- + +An Elf just remembered one more important detail: the two adjacent matching digits are not +part of a larger group of matching digits. + +Given this additional criterion, but still ignoring the range rule, the following are now true: + + + - 112233 meets these criteria because the digits never decrease and all repeated digits are exactly +two digits long. + + - 123444 no longer meets the criteria (the repeated 44 is part of a larger group of +444). + + - 111122 meets the criteria (even though 1 is repeated more than twice, it still contains a double +22). + + +How many different passwords within the range given in your puzzle input meet all of +the criteria? + + diff --git a/src/day4/test.input b/src/day4/test.input @@ -0,0 +1 @@ +111002-111122