def is_safe(report: list[int]) -> bool:
global removed
acceptable_range = [_ for _ in range(-3,4) if _ != 0]
diffs = []
if any([report.count(x) > 2 for x in report]):
return False
for i, num in enumerate(report[:-1]):
cur = num
next = report[i+1]
difference = cur - next
diffs.append(difference)
if difference not in acceptable_range:
return False
if len(diffs) > 1:
if diffs[-1] * diffs[-2] <= 0:
return False
return True
with open('input') as reports:
list_of_reports = reports.readlines()[:-1]
count = 0
failed_first_pass = []
failed_twice = []
for reportsub in list_of_reports:
levels = [int(l) for l in reportsub.split()]
original = levels.copy()
if is_safe(levels):
safe = True
count += 1
else:
failed_first_pass.append(levels)
for report in failed_first_pass:
print(report)
working_copy = report.copy()
for i in range(len(report)):
safe = False
working_copy.pop(i)
print("checking", working_copy)
if is_safe(working_copy):
count += 1
safe = True
break
else:
working_copy = report.copy()
print(count)
this post was submitted on 02 Dec 2024
43 points (97.8% liked)
Advent Of Code
1045 readers
4 users here now
An unofficial home for the advent of code community on programming.dev!
Advent of Code is an annual Advent calendar of small programming puzzles for a variety of skill sets and skill levels that can be solved in any programming language you like.
AoC 2024
Solution Threads
M | T | W | T | F | S | S |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 |
Rules/Guidelines
- Follow the programming.dev instance rules
- Keep all content related to advent of code in some way
- If what youre posting relates to a day, put in brackets the year and then day number in front of the post title (e.g. [2024 Day 10])
- When an event is running, keep solutions in the solution megathread to avoid the community getting spammed with posts
Relevant Communities
Relevant Links
Credits
Icon base by Lorc under CC BY 3.0 with modifications to add a gradient
console.log('Hello World')
founded 2 years ago
MODERATORS
load more comments
(1 replies)
Uiua
Took me a bit longer to get this one but still quite simple overall.
Spent quite some time on getting to know the try
and assert
operators better.
Run with example input here
# Get the indices matching the ascending/
# descending criteria
CheckAsc β β‘Β°β‘β(β£(βΈβ€.ββ.)β£(βΈβ€.βββ.)0)
# Get the indices matching the distance criteria
CheckDist β β‘Β°β‘β(β£(βΈβ€.β 1β:0)0Γββ₯β€1,3β΅β§-)
Split β β(β½β 1)β½,,
PartOne β (
&rs β &fo "input-2.txt"
β(β‘βββ @ .)β @\n.
CheckAsc.
β½
CheckDist
β§»β
)
PartTwo β (
&rs β &fo "input-2.txt"
β(β‘βββ @ .)β @\n.
CheckAsc.
Split
CheckDist.
Split
β(β)
β§»
:
β(β‘(β½:Β°β)βΒ€ββ:β 1β=.β‘β§».)
β‘(β§»βCheckDistβ½CheckAsc.Β°β‘)
+β§»β΄β
)
&p "Day 2:"
&pf "Part 1: "
&p PartOne
&pf "Part 2: "
&p PartTwo
python
solution
import re
import aoc
def setup():
return (aoc.get_lines(2), 0)
def safe(data):
order = 0 if data[0] < data[1] else 1
for i in range(0, len(data) - 1):
h = data[i]
t = data[i + 1]
d = abs(h - t)
if d not in [1, 2, 3] or (order == 0 and h >= t) or (
order == 1 and h <= t):
return False
return True
def one():
lines, acc = setup()
for l in lines:
if safe([int(x) for x in re.findall(r'\d+', l)]):
acc += 1
print(acc)
def two():
lines, acc = setup()
for l in lines:
data = [int(x) for x in re.findall(r'\d+', l)]
for i in range(len(data)):
if safe(data[:i] + data[i + 1:]):
acc += 1
break
print(acc)
one()
two()
Rust
Turned out alright, I am looking forward to seeing what 2d coordinate grid code I can cannibalize from last year's solutions π
load more comments
(2 replies)
R (R-Wasm)
input = file('input2024day2.txt',open='r')
lines = readLines(input)
library(stringr)
safe = 0
safe2 = 0
for (ln in lines){
vals = as.numeric(unlist(str_split(ln,' ')))
diffs = diff(vals)
cond1 = min(diffs) > 0 || max(diffs) < 0
cond2 = max(abs(diffs)) < 4
if (cond1 && cond2){
safe = safe + 1
}
else { #Problem Dampener
dampen = FALSE
for (omit in -1:-length(vals)){
diffs = diff(vals[omit])
cond1 = min(diffs) > 0 || max(diffs) < 0
cond2 = max(abs(diffs)) < 4
if (cond1 && cond2){
dampen = TRUE
}
}
if (dampen){
safe2 = safe2 + 1}
}
}
print (safe) #Part 1
print (safe + safe2) #Part 2