39
π¦ - 2024 DAY 2 SOLUTIONS -π¦
(programming.dev)
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)
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()
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
G'MIC solution
spoiler
it day2
crop. 0,0,0,{h#-1-2}
split. -,{_'\n'}
foreach { replace_str. " ",";" ({t}) rm.. }
safe_0,safe_1=0
foreach {
({h}) a[-2,-1] y
num_of_attempts:=da_size(#-1)+1
store temp
repeat $num_of_attempts {
$temp
if $> eval da_remove(#-1,$>-1) fi
eval "
safe=1;
i[#-1,1]>i[#-1,0]?(
for(p=1,p<da_size(#-1),++p,
if(!inrange(i[#-1,p]-i[#-1,p-1],1,3,1,1),safe=0;break(););
);
):(
for(p=1,p<da_size(#-1),++p,
if(!inrange(i[#-1,p-1]-i[#-1,p],1,3,1,1),safe=0;break(););
);
);
safe;"
rm
if $>
if ${} safe_1+=1 break fi
else
if ${} safe_0,safe_1+=1 break fi
fi
}
}
echo Day" "2:" "${safe_0}" :: "${safe_1}
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
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.
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 |
Icon base by Lorc under CC BY 3.0 with modifications to add a gradient
console.log('Hello World')