Operation Early Finish: Higher Edition

The doors are locked and only code will open them. Solve puzzles in Python, check your answers with check.py, collect stars.

How it works. Write a program that prints the answer. Then run check.py, type the puzzle ID (e.g. A1) and your answer. Every puzzle has a small example you can test your code on first. After 3 wrong answers you get a hint.

Room 1: Staff Room

Warm-up. Open 4 of 5 to unlock the next room.

A1Fire drill sums

The fire alarm goes off and the door only opens for the right number. The rule on the sign: add up every whole number from 1 to 1000 that is a multiple of 7 or a multiple of 13, but is not a multiple of 5.

Your task: Use a loop and MOD to build the total. Be careful with the brackets around your OR.

Test your code on this first: From 1 to 40 the numbers are 7, 13, 14, 21, 26, 28, 39 (35 is a multiple of 5, so it is out) → total 148.
Answer format: A whole number
Skills: fixed loop, MOD, AND / OR with brackets, running total

A2Mirror, mirror

The staff room door has a mirror lock. It wants the number of palindromes (words that read the same forwards and backwards, ignoring capitals) in this sentence. Single letters and two-letter words do not count: a palindrome here has 3 or more letters.
Anna and Bob saw a level racecar near the radar while Hannah noted that Otto painted a kayak and a madam asked about the refer button on the noon timetable but Eve ignored the stats

Your task: Store the sentence in a variable, split it into words and test each one. Write the palindrome test as a function that takes a word and returns True or False.

Test your code on this first: Anna saw a racecar has 2 palindromes (Anna and racecar; saw is not one and a is too short).
Answer format: A whole number
Skills: string handling, split, slicing / reversing, function with a parameter and a return value

A3The late bell

The bell system has crashed. The times of the day’s bells are in data/bells.txt (24-hour times like 09:40, one per line, in order).

Your task: Find the longest gap between two bells next to each other in the file. Give the gap in minutes and the time of the bell where that gap starts.

Test your code on this first: Bells 09:00, 09:50, 10:05, 11:15, 12:00 have gaps of 50, 15, 70 and 45 minutes → 70-10:05
Answer format: minutes-time, e.g. 70-10:05
Skills: reading a file into a 1-D array, parsing text into numbers, comparing neighbouring elements, find the maximum
Files: data/bells.txt

A4Combination dial

The dial only turns for numbers whose digits add up to exactly 20. For example 389 works (3 + 8 + 9 = 20) but 390 does not.

Your task: Write a function digit_sum(number) that returns the sum of the digits of a number. Then use it to look at every whole number from 1 to 5000. Give how many numbers have a digit sum of 20 and the largest one.

Test your code on this first: Test digit_sum(1234) gives 10. For a digit sum of 10 between 1 and 100 you get 19, 28, 37, 46, 55, 64, 73, 82, 91 → 9-91
Answer format: count-largest, e.g. 9-91
Skills: sub-program with a parameter and return value, loop, MOD and integer division, count, maximum

A5Urn temperature

The staff room tea urn has been logging its temperature in data/urn.txt (48 readings in °C, one per line, each with one decimal place). Somebody needs to know how bad it got.

Your task: Find the highest reading, the lowest reading and how many readings are above the average (mean) of all 48.

Test your code on this first: Readings 70.0, 90.5, 65.0, 80.5 have a mean of 76.5, so 90.5 and 80.5 are above it → 90.5-65.0-2
Answer format: highest-lowest-count, with the temperatures to 1 decimal place, e.g. 90.5-65.0-2
Skills: reading a file, converting text to real numbers, standard algorithms (max, min, running total), counting
Files: data/urn.txt

Room 2: Server Room

Data files, records and sub-programs. Open 3 of 5 to unlock the Vault.

B1Staff records

HR has dumped the staff list into data/staff.csv (90 staff). The first line is a heading. Each line is staff_id,department,years_service,salary.

Your task: Store each member of staff as a record in a 1-D array of records. Only staff with 5 or more years’ service count. Which department has the highest total salary for those staff, and what is that total?

Test your code on this first: S1 maths 10 years 30000, S2 art 2 years 90000, S3 maths 6 years 20000, S4 art 7 years 45000 → maths-50000 (maths 30000 + 20000 = 50000; art only counts S4 = 45000)
Answer format: department-total in lower case, e.g. maths-50000
Skills: records, reading a CSV file into an array of records, selection, totals per category, find the maximum
Files: data/staff.csv

B2Password patrol

The IT office has dumped 132 passwords into data/passwords.txt (one per line, a few contain spaces). A password is valid only if it follows every rule:

Your task: Count how many passwords are valid. Write one function that checks a single password and returns True or False, then call it for each line.

Test your code on this first: Harbour#2024 is valid. Not valid: harbour#2024 (no capital), Harbour2024x (no symbol), Harbour #2024 (space), MyPassWord#12 (contains “password”), Tiger#24 (too short).
Answer format: A whole number
Skills: validation, sub-program with a parameter returning a Boolean, string handling, counting
Files: data/passwords.txt

B3Intruder log

Somebody has been guessing passwords on the school server. The log is data/access.log: a heading line, then lines like 07:15:02,10.0.4.23,FAIL (time, IP address, result). The result is OK or FAIL, but the logging software is sloppy: a failure can be written FAIL, Fail or fail.

Your task: Find the IP address with the most failed logins and say how many it had.

Test your code on this first: Failures: 10.0.1.5, 10.0.1.9, 10.0.1.5, plus one OK for 10.0.1.9 → 10.0.1.5-2
Answer format: ip-count, e.g. 10.0.1.5-2
Skills: reading a file, splitting lines, counting occurrences per category, case-insensitive comparison, find the maximum
Files: data/access.log

B4Locker lottery

150 lockers and their 4-digit combinations are in data/lockers.csv (lines look like L001,0517). Careful: some combinations start with a 0. A combination is rising if every digit is bigger than the digit before it, so 0358 and 1469 are rising but 1233 and 5731 are not.

Your task: Count the lockers with a rising combination, and give the locker number of the last one in the file. Write a function that takes a combination and returns True or False.

Test your code on this first: L001,1234   L002,1224   L003,0589   L004,9871 → 2-l003
Answer format: count-locker in lower case, e.g. 2-l003
Skills: reading a file, splitting strings, text vs number, comparing neighbouring characters, sub-program returning a Boolean
Files: data/lockers.csv

B5Tuck shop takings

The tuck shop has written its week into data/tuckshop.csv: a heading line, then item,category,price_pence,sold (an item that did not sell has 0 sold). Revenue for an item is price × number sold.

Your task: Which category made the most money in total, and how many pence was that? Write a function that takes the array of records and a category name and returns that category’s total revenue, then call it for each category.

Test your code on this first: juice drinks 90p × 8 = 720, cola drinks 100p × 3 = 300, crisps snacks 60p × 10 = 600, bar snacks 45p × 15 = 675 → snacks-1275 (drinks only reach 1020)
Answer format: category-pence in lower case, e.g. snacks-1275
Skills: records, parameter passing (array and string), totals per category, find the maximum
Files: data/tuckshop.csv

Room 3: The Vault

The stretch. Open all 5 for the final exit code.

C1Secret message

An encrypted message is in data/secret.txt. Every letter was shifted forward through the alphabet by an amount taken from a repeating keyword: VAULT. The first letter of the message moves by V (21 places, since A = 0), the second by A (0), the third by U (20), then L (11), T (19), and then the keyword starts again. Spaces and full stops are left alone and do not use up a keyword letter.

Your task: Decrypt the message. The passphrase is hidden inside it.

Test your code on this first: With keyword BAD, IEOMO decrypts to HELLO
Answer format: The passphrase in lower case
Skills: ASCII codes (ord and chr), modular arithmetic, keeping a separate position in the keyword, string building
Files: data/secret.txt

C2Sensor tape

A sensor on the vault wall records temperature changes as 8-bit two’s complement binary numbers. The tape is in data/sensor.txt (64 readings, one per line, such as 11111100). The leftmost bit is the sign bit.

Your task: Convert every reading to the signed denary number it stores. Give the total of all the readings and how many are negative. (Do not just use int(reading, 2) on its own: that treats the sign bit as an ordinary bit.)

Test your code on this first: 00001010 = 10, 11111100 = −4, 10000000 = −128, 01111111 = 127 → 5-2
Answer format: total-negatives, e.g. 5-2 (the total can be negative: write it as -12-30)
Skills: two’s complement, binary to denary conversion, reading a file, running total, counting
Files: data/sensor.txt

C3Prime suspects

The vault’s inner door only opens for twin primes: two prime numbers that differ by exactly 2, like 11 and 13.

Your task: Count the pairs of twin primes where both numbers are below 10000, and give the smaller number of the largest pair.

Test your code on this first: Both below 30: (3,5), (5,7), (11,13), (17,19) → 4-17 (29 and 31 do not count because 31 is not below 30)
Answer format: count-smaller, e.g. 4-17
Skills: sub-program to test for a prime, nested loops, efficient algorithm design

C4Minefield

data/board.txt is a 20 × 20 minesweeper board: * is a mine, . is a safe square.

Your task: Every safe square shows how many of its (up to 8) neighbours are mines. Give the total of all the numbers on safe squares and the largest single number on the board.

Test your code on this first:
*..
...
..*
shows 1,0 / 1,2,1 / 0,1 so the total is 6 and the largest is 2 → 6-2
Answer format: total-largest, e.g. 6-2
Skills: 2-D arrays, nested loops, boundary checking, standard algorithms
Files: data/board.txt

C5Pipework

The vault is full of old pipework. data/vault.txt is a 14 × 14 map: . is open pipe and # is a blockage. Water starts in the top-left corner and must reach the bottom-right corner, moving only right or down one square at a time, never through a #.

Your task: How many different routes can the water take?

Test your code on this first:
..#
...
#..
has 4 routes.
Answer format: A whole number (it will be big, so do not try to list every route)
Skills: 2-D arrays, nested loops, algorithm design (build up the answer square by square)
Files: data/vault.txt