refactor to use listcomp, also do oneliner, also do ruby
This commit is contained in:
parent
cc3975bdcd
commit
3d7a36ec6f
17
06/06.rb
Normal file
17
06/06.rb
Normal file
|
@ -0,0 +1,17 @@
|
||||||
|
require 'set'
|
||||||
|
|
||||||
|
groups = File.read("input.txt").split("\n\n")
|
||||||
|
|
||||||
|
puts groups
|
||||||
|
.map{|g| g.gsub("\n","")}
|
||||||
|
.map{|g| g.chars}
|
||||||
|
.map{|a| a.to_set}
|
||||||
|
.map{|s| s.size}
|
||||||
|
.reduce(0, :+)
|
||||||
|
puts groups
|
||||||
|
.map{|g| g.split("\n")}
|
||||||
|
.map{|g| g.map{|a| a.chars}}
|
||||||
|
.map{|a| a.map{|a| a.to_set}}
|
||||||
|
.map{|s| s.reduce(:&)}
|
||||||
|
.map{|s| s.size}
|
||||||
|
.reduce(0, :+)
|
8
06/06_minimal.py
Normal file
8
06/06_minimal.py
Normal file
|
@ -0,0 +1,8 @@
|
||||||
|
with open("input.txt","r") as f:
|
||||||
|
groups = f.read().split('\n\n')
|
||||||
|
print(
|
||||||
|
(
|
||||||
|
sum(map(len, [set(list(group.replace('\n',''))) for group in groups])),
|
||||||
|
sum(map(len,[set.intersection(*map(set, map(list,group.split('\n')))) for group in groups]))
|
||||||
|
)
|
||||||
|
)
|
21
06/06_refactored.py
Normal file
21
06/06_refactored.py
Normal file
|
@ -0,0 +1,21 @@
|
||||||
|
def read_file():
|
||||||
|
with open("input.txt","r") as f:
|
||||||
|
return f.read().split('\n\n')
|
||||||
|
|
||||||
|
def part1(groups):
|
||||||
|
"""Anyone"""
|
||||||
|
groupsets = [set(list(group.replace('\n',''))) for group in groups]
|
||||||
|
return sum(map(len, groupsets))
|
||||||
|
|
||||||
|
def part2(groups):
|
||||||
|
"""Everyone"""
|
||||||
|
allset = [set.intersection(*map(set, map(list,group.split('\n')))) for group in groups]
|
||||||
|
return sum(map(len,allset))
|
||||||
|
|
||||||
|
def main():
|
||||||
|
groups = read_file()
|
||||||
|
print(f'{part1(groups)=}')
|
||||||
|
print(f'{part2(groups)=}')
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main()
|
Loading…
Reference in a new issue