Skip to content

Commit

Permalink
2023 day 12 part 1
Browse files Browse the repository at this point in the history
  • Loading branch information
taw10 committed Dec 16, 2023
1 parent f3f9048 commit 64ee0d1
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
34 changes: 34 additions & 0 deletions 2023/day12-hot-springs/day12.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
function parsespring(line)
return split(line)[1], map(x->parse(Int,x),
split(split(line)[2], ","))
end

function issolution(str, lengths)
springs = split(strip(str, ['.']), '.', keepempty=false)
length(springs) == length(lengths) && all(map((spring,sprlen)->length(spring)==sprlen,
springs, lengths))
end

function solvesprings(springs, lengths)
ndamage = count('?', springs)
solutions = Vector{AbstractString}()
for i in 0:2^ndamage-1
bit = 0
cand = map(springs) do ch
if ch == '?'
out = (i & 1<<bit != 0) ? '#' : '.'
bit += 1
out
else
ch
end
end
if issolution(String(cand), lengths)
push!(solutions, String(cand))
end
end
solutions
end

input = map(parsespring, eachline("input"))
println("Part 1: ", sum(map(length, map(x->solvesprings(x[1],x[2]), input))))
6 changes: 6 additions & 0 deletions 2023/day12-hot-springs/example
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
???.### 1,1,3
.??..??...?##. 1,1,3
?#?#?#?#?#?#?#? 1,3,1,6
????.#...#... 4,1,1
????.######..#####. 1,6,5
?###???????? 3,2,1

0 comments on commit 64ee0d1

Please sign in to comment.