Day 2: Red-Nosed Reports
Megathread guidelines
- Keep top level comments as only solutions, if you want to say something other than a solution put it in a new post. (replies to comments can be whatever)
- You can send code in code blocks by using three backticks, the code, and then three backticks or use something such as https://blocks.programming.dev/ if you prefer sending it through a URL
FAQ
- What is this?: Here is a post with a large amount of details: https://programming.dev/post/22323136
- Where do I participate?: https://adventofcode.com/
- Is there a leaderboard for the community?: We have a programming.dev leaderboard with the info on how to join in this post: https://programming.dev/post/6631465
C
First went through the input in one pass, number by number, but unfortunately that wouldn’t fly for part 2.
Code
#include "common.h" static int issafe(int *lvs, int n, int skip) { int safe=1, asc=0,prev=0, ns=0,i; for (i=0; safe && i<n; i++) { if (i == skip) { ns = 1; continue; } if (i-ns > 0) safe = safe && lvs[i] != prev && lvs[i] > prev-4 && lvs[i] < prev+4; if (i-ns == 1) asc = lvs[i] > prev; if (i-ns > 1) safe = safe && (lvs[i] > prev) == asc; prev = lvs[i]; } return safe; } int main(int argc, const char **argv) { char buf[64], *rest, *tok; int p1=0,p2=0, lvs[16],n=0, i; if (argc > 1) DISCARD(freopen(argv[1], "r", stdin)); while ((rest = fgets(buf, sizeof(buf), stdin))) { for (n=0; (tok = strsep(&rest, " ")); n++) { assert(n < (int)LEN(lvs)); lvs[n] = (int)strtol(tok, NULL, 10); } for (i=-1; i<n; i++) if (issafe(lvs, n, i)) { p1 += i == -1; p2++; break; } } printf("02: %d %d\n", p1, p2); }
What is this coding style? The function type, name and open brace placement made me think GNU at first, but the code in the body doesn’t look like GCS at all.
BSD more or less. Mostly K&R except for function declarations.
TypeScript
Solution
import { AdventOfCodeSolutionFunction } from "./solutions"; /** * this function evaluates the * @param levels a list to check * @returns -1 if there is no errors, or the index of where there's an unsafe event */ export function EvaluateLineSafe(levels: Array<number>) { // this loop is the checking every number in the line let isIncreasing: boolean | null = null; for (let levelIndex = 1; levelIndex < levels.length; levelIndex++) { const prevLevel = levels[levelIndex - 1]; // previous const level = levels[levelIndex]; // current const diff = level - prevLevel; // difference const absDiff = Math.abs(diff); // absolute difference // check if increasing too much or not at all if (absDiff == 0 || absDiff > 3) return levelIndex; // go to the next report // set increasing if needed if (isIncreasing === null) { isIncreasing = diff > 0; continue; // compare the next numbers } // check if increasing then decreasing if (!(isIncreasing && diff > 0 || !isIncreasing && diff < 0)) return levelIndex; // go to the next report } return -1; } export const solution_2: AdventOfCodeSolutionFunction = (input) => { const reports = input.split("\n"); let safe = 0; let safe_damp = 0; // this loop is for every line main: for (let i = 0; i < reports.length; i++) { const report = reports[i].trim(); if (!report) continue; // report is empty const levels = report.split(" ").map((v) => Number(v)); const evaluation = EvaluateLineSafe(levels); if(evaluation == -1) { safe++; continue; } // search around where it failed for (let offset = evaluation - 2; offset <= evaluation + 2; offset++) { // delete an evaluation in accordance to the offset let newLevels = [...levels]; newLevels.splice(offset, 1); const newEval = EvaluateLineSafe(newLevels); if(newEval == -1) { safe_damp++; continue main; } } } return `Part 1: ${safe} Part 2: ${safe + safe_damp}`; }
God, I really wish my solutions weren’t so convoluted. Also, this is an O(N^3) solution…
I don’t think your solution is O(N^3). Can you explain your reasoning?
3 nested for loops
It’s not as simple as that. You can have 20 nested for loops with complexity of O(1) if all of them only ever finish one iteration.
Or you can have one for loop that iterates 2^N times.
What do you think my complexity is?
I think it could be maybe O(n^2) because the other for loop which tries elements around the first error will only execute a constant of 5 times in the worst case? I’m unsure.
It’s O(n).
If you look at each of the levels of all reports, you will access it a constant number of times: at most twice in each call to
EvaluateLineSafe
, and you will callEvaluateLineSafe
at most six times for each report.It really depends on what your parameter n is. If the only relevant size is the number of records (let’s say that is n), then this solution takes time in O(n), because it loops over records only once at a time. This ignores the length of records by considering it constant.
If we also consider the maximum length of records (let’s call it m), then your solution, and most others I’ve seen in this thread, has a time complexity in O(n * m^2) for part 2.
Uiua
Uiua is still developing very quickly, and this code uses the experimental
tuples
function, hence the initial directive.# Experimental! "7 6 4 2 1\n1 2 7 8 9\n9 7 6 2 1\n1 3 2 4 5\n8 6 4 4 1\n1 3 6 7 9" ⊜(⊜⋕⊸≠@\s)⊸≠@\n # Partition at \n, then at space, parse ints. IsSorted ← +⊃(≍⇌⍆.|≍⍆.) # Compare with sorted array. IsSmall ← /××⊃(>0|<4)⌵↘¯1-↻1. # Copy offset by 1, check diffs. IsSafe ← ×⊃IsSmall IsSorted # Safe if Small steps and Ordered. IsSafer ← ±/+≡IsSafe ⧅<-1⧻. # Choose 4 from 5, check again. &p/+≡IsSafe . # Part1 : Is each row safe? &p/+≡(±+⊃IsSafe IsSafer) # Part2 : Is it safe or safer?
How do you write this, not conceptually but physically. Do you have a char picker open at all times?
This looks so alien! Does it work with the full set? The comment says 5, choose 4, but I guess it’s written as n, choose n-1?